vuejs / apollo

🚀 Apollo/GraphQL integration for VueJS
http://apollo.vuejs.org
MIT License
6.02k stars 522 forks source link

[Vue warn]: Error in created hook: "TypeError: Cannot read property 'watchQuery' of undefined" #472

Open baloodevil opened 5 years ago

baloodevil commented 5 years ago

Following the manual instructions and creating a simple HelloApollo.vue component, I get the following when trying to run it...

webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:602 [Vue warn]: Error in created hook: "TypeError: Cannot read property 'watchQuery' of undefined"

found in

---> at src/components/HelloApollo.vue

at src/App.vue
Akryum commented 5 years ago

Please provide a minimal reproduction in repository or codesandbox

joshnuss commented 5 years ago

I ran into this error too.

It happens if you accidentally define your apolloProvider without a defaultClient.

Incorrect way:

const apolloProvider = new VueApollo(defaultClient); // missing { }

Correct way:

const apolloProvider = new VueApollo({ defaultClient });
annymosse commented 4 years ago

getting same problem with a fresh vue app and i have the following problems

export default { apollo: { articles: gql query { articles } }, data: () => ({ articles: [] }) }

the problem was when i exported the plug-in 
```js
// wrong
export const apolloClient = new ApolloClient({
  link: httpLink,
  cache
})

// correct
const apolloClient = new ApolloClient({
  link: httpLink,
  cache
});
export default apolloClient;
fujifilmfan commented 3 years ago

joshnuss wrote, "It happens if you accidentally define your apolloProvider without a defaultClient."

This also happens if you intentionally define your apolloProvider without a defaultClient 😄 :

const apolloProvider = new VueApollo({
    clients: {
        dataciteApolloClient,
        githubApolloClient,
        gitlabApolloClient,
    },
    // defaultClient: dataciteApolloClient,
    errorHandler (error) {
        console.log('Global error handler')
        console.error(error)
    },
});

I don't want a default client because I want the client explicitly set alongside the query. It's not a big deal to add the default, and I can still use the default client explicitly. It just won't be enforced.