nuxt-modules / apollo

Nuxt.js module to use Vue-Apollo. The Apollo integration for GraphQL.
https://apollo.nuxtjs.org
MIT License
955 stars 198 forks source link

query in nuxtServerInit results in " XMLHttpRequest is not defined" since nuxt 2.15 #420

Open dadaphl opened 2 years ago

dadaphl commented 2 years ago

I am using the nuxt/apollo in the nuxtServerInit function to fetch data server-side. This used to work in nuxt 2.14. Updating to nuxt 2.15. results in a exception with error "XMLHttpRequest is not defined"

expected behavior: get apollo query result server side and populate the store

store/index.js

...
export const actions = {
  async nuxtServerInit ({ commit }, { req, app }) {
    try {
      const client = app.apolloProvider.defaultClient
      const res = await client.query({
        query: indexQuery
      }).then(({ data }) => data && data.setting)
      ...
    } catch (e) {
      console.log(e)
      /* handle error */
    }
  }
}

Error in the window of yarn dev

Network error: XMLHttpRequest is not defined                                                                                                           12:55:23

  at new ApolloError (server.js:11182:26)
  at server.js:12678:34
  at server.js:13098:15
  at Set.forEach (<anonymous>)
  at server.js:13096:26
  at Map.forEach (<anonymous>)
  at QueryManager.broadcastQueries (server.js:13094:20)
  at server.js:12573:29