import fetch from 'node-fetch'; import { createHttpLink } from 'apollo-link-http'; import { InMemoryCache, IntrospectionFragmentMatcher } from 'apollo-cache-inmemory'; import { ApolloClient } from 'apollo-client'; import { setContext } from 'apollo-link-context'; const httpLink = createHttpLink({ uri: 'https://graphql.datocms.com/' }); console.log('GATSBY_DATO_API_TOKEN', process.env.GATSBY_DATO_API_TOKEN); const authLink = setContext((_, {headers}) => { return { headers: { ...(headers || {}), 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': `Bearer ${process.env.GATSBY_DATO_API_TOKEN}` } }; }); // Hotfix to allow menu items to work through apollo const introspectionQueryResultData = { __schema: { types: [ { kind: 'UNION', name: 'SiteconfigModelMenuitemsField', possibleTypes: [ { name: 'MenuitemRecord' }, { name: 'ShopmenuitemRecord' }, { name: 'TextMenuItemRecord' } ] } ] } }; const fragmentMatcher = new IntrospectionFragmentMatcher({ introspectionQueryResultData }); export const api = new ApolloClient({ link: authLink.concat(httpLink), fetch, cache: new InMemoryCache({fragmentMatcher}), defaultOptions: { // @ts-ignore fetchPolicy: 'no-cache' } });