Skip to content

Instantly share code, notes, and snippets.

@jason-c-child
Last active December 14, 2023 20:19
Show Gist options
  • Save jason-c-child/e9ebf988ab0ef740a03d221b0c702567 to your computer and use it in GitHub Desktop.
Save jason-c-child/e9ebf988ab0ef740a03d221b0c702567 to your computer and use it in GitHub Desktop.
An example CloudFlare Function for fetching data from Stargaze GraphQL API
import { error, json, Router, createCors } from 'itty-router';
import { ApolloClient, InMemoryCache, gql } from '@apollo/client';
const { preflight, corsify } = createCors();
const client = new ApolloClient({
uri: 'https://graphql.mainnet.stargaze-apis.com/graphql',
cache: new InMemoryCache(),
});
export const getTraitsAndOwner = (collectionAddr, tokenId) =>
client.query({
query: gql`
query Query {
token(collectionAddr: "${collectionAddr}", tokenId: "${tokenId}") {
traits {
name
value
}
}
}
`,
});
const router = Router();
router.all('*', preflight);
router.get('/check/:collectionAddr/:tokenId', async (request) => {
const { collectionAddr, tokenId } = request.params;
const response = await getTraitsAndOwner(collectionAddr, tokenId);
return corsify(json(response));
});
export default {
fetch: (req, env, ctx) => router.handle(req, env, ctx).then(json).catch(error),
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment