Last active
December 14, 2023 20:19
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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