javascript - 将 Apollo React onError 从 apollo-link-error 添加到链接 const
问题描述
我想添加onError
到我的index.js
Apollo 文件中。因此,该视频帮助我了解了一个非常基本的示例。但是由于我的项目中有更多链接,因此与那里显示的内容有些不同。
索引.js:
import { InMemoryCache } from 'apollo-cache-inmemory'
import { setContext } from 'apollo-link-context'
import { WebSocketLink } from 'apollo-link-ws'
import { split } from 'apollo-link'
import { onError } from "apollo-link-error";
const httpLink = createHttpLink({
uri: 'http://localhost:4000',
})
const authLink = setContext((_, { headers }) => {
const token = localStorage.getItem(AUTH_TOKEN)
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : '',
},
}
})
const wsLink = new WebSocketLink({
uri: `ws://localhost:4000`,
options: {
reconnect: true,
connectionParams: {
authToken: localStorage.getItem(AUTH_TOKEN),
},
},
})
const link = split(
({ query }) => {
const { kind, operation } = getMainDefinition(query)
return kind === 'OperationDefinition' && operation === 'subscription'
},
wsLink,
authLink.concat(httpLink),
)
const client = new ApolloClient({
link,
cache: new InMemoryCache(),
})
现在我想将其添加errorLink
到我的项目中以使用此代码跟踪错误:
const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.map(({ message, location, path }) =>
console.log(`[GraphQL error]: Message: ${message}, Location: ${location}, Path: ${path}`),
);
if (networkError) console.log(`[Network error]: ${networkError}`);
});
但我不确定如何将该新链接添加到link
const. 是用aconcat
还是其他东西完成的?
我已经查看了撰写链接部分。但这也与我的例子太不同了。
解决方案
split
正在返回一个新的ApolloLink
.
在这种情况下,doinglink: ApolloLink.from([errorLink, link])
应该起作用。它将ApolloLink
从ApolloLink
.
推荐阅读
- python - 在带有 random.sample 的 for 循环中没有重复
- mysql - MySQL 事件不起作用(插入语句)
- android - 如何在android导航中使用单顶
- c# - 将枚举从字符串转换为数据库中的 Int 然后返回字符串 - Entity Framework Core 3
- c# - 椭圆纹理没有做预期的事情
- postgresql - 使用 prisma 在本地连接 postgres 数据库
- google-pagespeed - “来自引用者 https://www.googleapis.com/ 的请求被阻止”错误
- django - django:Reverse for 'detail' 没有找不到参数。尝试了 1 种模式:['detail/(?P
[0-9]+)/$'] - f# - FSharp 计算表达式:无法在自定义操作中引用绑定值
- python - Python重新搜索二进制字符串