Search code examples
vue.jsvuejs2apolloapollo-clientvue-apollo

How to Import Apollo Client 3 for Vue Apollo?


The release note of Vue-Apollo 3.04 stated that there is now a Apollo Client 3 support. As the Docs of Apollo Client 3 declare ApolloClient, InMemoryCache, HttpLink, are now all in one @apollo/client package - not like previous in single packages.

https://github.com/vuejs/vue-apollo/releases/tag/v3.0.4

https://www.apollographql.com/docs/react/migrating/apollo-client-3-migration/

However there is no change in the doc´s of Vue-Apollo (apollo-client -> @apollo/client ?) on how to apply the new changes. Also the Apollo Client 3 Doc´s stated that for Vue you have to import the ApolloClient from @apollo/client/core

So i tried it my own way, but my imports seems to fail. I got these Error Message from npmm run serve. Seems he wants to import some reactstuff.

     ERROR  Failed to compile with 8 errors                                                                                                             20:25:20
This dependency was not found:

* react in ./node_modules/@apollo/client/react/context/ApolloConsumer.js, ./node_modules/@apollo/client/react/context/ApolloContext.js and 6 others

To install it, you can run: npm install --save react

vue-apollo.js (from old project that worked)


import Vue from 'vue'
import { ApolloClient } from 'apollo-client'
import { HttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'
import VueApollo from 'vue-apollo'

vue-apollo.js (with new apollo client 3)


import Vue from 'vue'
import { ApolloClient } from '@apollo/client/core'
import { InMemoryCache } from '@apollo/client/cache'
import { HttpLink } from '@apollo/client'
import VueApollo from 'vue-apollo'

package.json

     "dependencies": {
    "@apollo/client": "^3.1.3",
    "core-js": "^3.6.5",
    "graphql": "^15.3.0",
    "js-cookie": "^2.2.1",
    "subscriptions-transport-ws": "^0.9.18",
    "vue": "^2.6.11",
    "vue-apollo": "^3.0.4"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "babel-eslint": "^10.1.0",
    "eslint": "^6.7.2",
    "eslint-plugin-vue": "^6.2.2",
    "extract-text-webpack-plugin": "^4.0.0-beta.0",
    "vue-template-compiler": "^2.6.11",
    "webpack-bundle-tracker": "^1.0.0-alpha.1"
  },

Question

How to correct import the new apollo client 3 with vue apollo?


Solution

  • Alright after a break i tried this and it worked.

    vue-apollo.js


    import Vue from 'vue'
    import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client/core';
    import VueApollo from 'vue-apollo'
    

    Now the compiler finds everything. Although in the Apollo Client 3 Doc i could only find one reference with the new Client 3 and Vue, it seems all react stuff comes from @apollo/client while Vue and other from @apollo/client/core.