I am new to GraphQL and Apollo and I am really struggling with setting up an apolloClient in node.js.
First I want to mention that I sucessfully set up an apollo client within nuxt using nuxt's apollo module.
So I am completely sure that my endpoint and my token are working.
Now I wanted to setup an apollo client within a JavaScript file, which I want to run in node.js.
import fetch from 'node-fetch'
import gql from 'graphql-tag'
import { ApolloClient } from 'apollo-client'
import { createHttpLink } from 'apollo-link-http'
import { setContext } from 'apollo-link-context'
export default function generateRoutesFromData(
options = {
api: [],
query: '',
token: '',
bundle: '',
homeSlug: 'home',
errorPrefix: 'error-'
}
) {
const uri = 'https://example.com/api'
const token =
'...fPsvYqkheQXXmlWgb...'
const httpLink = createHttpLink({ uri, fetch })
const authLink = setContext((_, { headers }) => {
// return the headers to the context so httpLink can read them
return {
headers: {
...headers,
authorization: `Bearer ${token}`
}
}
})
const GET_PAGES = gql`
{
helloWorld
}
`
const client = new ApolloClient({
link: authLink.concat(httpLink),
fetch
})
client
.query({
query: GET_PAGES
})
.then(result => {
console.log('result: ', result)
})
.catch(error => {
console.log('error: ', error)
})
// for now just return array with a test string
// later on: return all uris fetched via graphql
return ['/test']
}
If I run this in node I get the following error:
FATAL Invariant Violation: 1 (see https://github.com/apollographql/invariant-packages) 00:05:36
Invariant Violation: Invariant Violation: 1 (see https://github.com/apollographql/invariant-packages)
at new InvariantError (packages/.../node_modules/ts-invariant/lib/invariant.js:16:28)
at new ApolloClient (packages/.../node_modules/apollo-client/bundle.umd.js:2483:55)
at generateRoutesFromData (packages/.../src/routes/generateRoutesFromData.js:41:18) at Object. (nuxt.config.js:158:10) at Generator.next ()
I googled for about 1.5h and did not make any progress... The error message is very cryptic and I don't know what I could do.
I must say the whole documentation arount apollo is pretty confusing. Apollo-boost and which wraps apollo client all other stuff. Different ways of authenticating etc.
I found this page: https://www.apollographql.com/docs/react/advanced/boost-migration/ But it seems quite complex and I am not sure, if this will even help me with anything...
Any help with this is much appreciated! Cheers m