如何为Vue Apollo导入Apollo Client 3?

时间:2020-08-17 19:51:01

标签: vue.js vuejs2 apollo apollo-client vue-apollo

Vue-Apollo 3.04 的发行说明指出,现在有 Apollo Client 3 支持。随着 Apollo Client 3 的文档声明ApolloClientInMemoryCacheHttpLink现在都放在一个@apollo/client包中-不同于以前的单个包包。

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

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

但是 Vue-Apollo apollo-client-> @apollo/client吗?)的文档上没有应用新更改的更改。同样, Apollo Client 3 文档指出,对于 Vue ,您必须从ApolloClient

导入@apollo/client/core

所以我尝试了自己的方式,但是导入似乎失败了。我从npmm run serve得到了这些错误消息。似乎他想导入一些react东西。

     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 (来自起作用的旧项目)


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 (使用新的apollo客户端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"
  },

问题

如何使用 vue阿波罗正确导入新的 apollo客户端3

1 个答案:

答案 0 :(得分:4)

好一会儿,我尝试了一下,并且奏效了。

vue-apollo.js


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

现在,编译器可以找到所有内容。尽管在 Apollo Client 3 文档中,我只能使用新的Client 3和Vue找到一个引用,但似乎所有react的内容都来自@apollo/client,而Vue的其他内容都来自{ {1}}。