最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ApolloClient: Invariant Violation 1 – setup apollo client properly - Stack Overflow

programmeradmin2浏览0评论

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 pletely 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 = ''
  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 ) 00:05:36

Invariant Violation: Invariant Violation: 1 (see )

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: / But it seems quite plex and I am not sure, if this will even help me with anything...

Any help with this is much appreciated! Cheers m

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 pletely 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./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./apollographql/invariant-packages) 00:05:36

Invariant Violation: Invariant Violation: 1 (see https://github./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./docs/react/advanced/boost-migration/ But it seems quite plex and I am not sure, if this will even help me with anything...

Any help with this is much appreciated! Cheers m

Share Improve this question asked Aug 11, 2019 at 22:20 MercMerc 4,5708 gold badges58 silver badges87 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Your client configuration is missing a cache:

const client = new ApolloClient({
  link: authLink.concat(httpLink),
  cache: new InMemoryCache(), // <-- ADD ME
})

This is the actual error you're seeing:

In order to initialize Apollo Client, you must specify 'link' and 'cache' properties in the options object. These options are part of the upgrade requirements when migrating from Apollo Client 1.x to Apollo Client 2.x. For more information, please visit: https://www.apollographql./docs/tutorial/client.html#apollo-client-setup

Invariant errors are intentionally obfuscated in production. I'm not sure why you would be seeing that behavior locally but maybe Nuxt is setting NODE_ENV to production at some point before that code evaluates.

发布评论

评论列表(0)

  1. 暂无评论