如何为 Vue Apollo 导入 Apollo Client 3?
Posted
技术标签:
【中文标题】如何为 Vue Apollo 导入 Apollo Client 3?【英文标题】:How to Import Apollo Client 3 for Vue Apollo? 【发布时间】:2020-12-06 23:49:56 【问题描述】:Vue-Apollo 3.04 的发行说明声明现在支持 Apollo Client 3。正如 Apollo Client 3 的文档所声明的那样,ApolloClient
、InMemoryCache
、HttpLink
现在都在一个 @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,您必须从 @apollo/client/core
导入 ApolloClient
所以我尝试了自己的方式,但我的导入似乎失败了。我从npmm run serve
收到这些错误消息。似乎他想导入一些react
stuff。
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正确导入新的apollo client 3?
【问题讨论】:
【参考方案1】:好吧,休息后我尝试了这个,它成功了。
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 和其他来自 @apollo/client/core
。
【讨论】:
以上是关于如何为 Vue Apollo 导入 Apollo Client 3?的主要内容,如果未能解决你的问题,请参考以下文章
如何为 Apollo 的 React HOC 定义 props 接口?
Apollo 客户端:codegen 如何为@client 指令生成类型?