Using GraphQL client in server routes / hiding API tokens from frontend #459
Unanswered
lewebsimple
asked this question in
Q&A
Replies: 1 comment
-
When using Nuxt 2 and the previous version of the module, I would just define my own Apollo clients to use in server API routes import {
ApolloClient,
ApolloLink,
InMemoryCache,
HttpLink,
} from '@apollo/client/core'
import fetch from 'cross-fetch'
const {
SHOPIFY_DOMAIN,
SHOPIFY_API_VERSION,
SHOPIFY_ADMIN_API_ACCESS_TOKEN,
} = process.env
const AdminLink = new HttpLink({
uri: `https://${SHOPIFY_DOMAIN}/admin/api/${SHOPIFY_API_VERSION}/graphql`,
headers: {
accept: 'application/json',
'X-Shopify-Access-Token': SHOPIFY_ADMIN_API_ACCESS_TOKEN,
},
fetch,
})
// Log query complexity and rate limit info
const ForwardExtensionsLink = new ApolloLink((operation, forward) => {
return forward(operation).map((response) => {
const { extensions } = response
// Only triggers for queries with `fetchPolicy: 'no-cache'`
if (process.env.NODE_ENV !== 'production' && extensions) {
console.dir(extensions)
}
return response
})
})
export const shopifyAdminClient = new ApolloClient({
link: ForwardExtensionsLink.concat(AdminLink),
cache: new InMemoryCache(),
}) You can then import it and use it in server routes, e.g. import { shopifyAdminClient } from 'path/to/client.js`
shopifyAdminClient.mutate({
// ...
}) From what I understand this module isn't meant to be used in server routes, although I may have misunderstood or it might have changed in the newest version with Composition API. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
When trying to execute a query from an API server route in my Nuxt 3 application, I get the following error:
useAsyncQuery
is called from auseCustomers
composable which works just fine in pages and components.For the record, I'm using multiple endpoints (Shopify Storefront API and Admin API) and I would like to hide the Admin API from the frontend since I don't want my token to leak publicly. Is this possible with Nuxt Apollo?
Beta Was this translation helpful? Give feedback.
All reactions