javascript - 未处理的 GraphQL 订阅错误 [错误:无法解构“未定义”的属性“数据”,因为它未定义。]
问题描述
我尝试subscribeToMore
在 react-native 中使用我的 graphql 查询,但出现以下问题:
ERROR: Unhandled GraphQL subscription error [Error: Cannot destructure property 'data' of 'undefined' as it is undefined.]
我的订阅看起来像
// MESSAGE_SUBSCRIPTION
import { gql } from '@apollo/client'
export const TOPIC_MESSAGE_SUBSCRIPTION = gql`
subscription MessageSubscription($data: SubscriptionInput!) {
messageSubscription(data: $data) {
_id
text
}
}
`
我的组件
...
subscribeToMore({
document: MESSAGE_SUBSCRIPTION,
variables: {
data: {
_id: params._id,
},
},
updateQuery: (prev, { subscriptionData }) => {
if (!subscriptionData.data) return prev
console.log(prev, subscriptionData)
return prev
},
})
...
当我在操场上尝试订阅时,一切正常。
解决方案
我的 apolloClient 的配置缺少 websocket 连接。
我还需要将授权添加为参数,以将其作为上下文
const wsLink = new WebSocketLink({
uri: Platform.select({
ios: 'ws://localhost:4000/',
android: 'ws://10.0.2.2:4000/',
}),
options: {
reconnect: true,
connectionParams: async () => {
const result = await getToken()
return {
authorization: result ? `Bearer ${result.password}` : '',
}
},
},
})
....
export const client = new ApolloClient({
link: split(
({ query }) => {
const definition = getMainDefinition(query)
return (
definition.kind === 'OperationDefinition' &&
definition.operation === 'subscription'
)
},
authLink.concat(wsLink as any),
authLink.concat(httpLink as any)
),
cache: new InMemoryCache(),
})
推荐阅读
- java - Kubernetes 容器中的 Spark 看不到本地文件
- python - 使 Python 模块导入明确
- python - Django - Docker python:无法打开文件'manage.py':[Errno 2]没有这样的文件或目录
- javascript - 如何将 Google Sheet 中的唯一值传递给 HTML 电子邮件模板
- android - Firebase 电话号码 OTP 身份验证可用于在 Flutter 的 Firestore 数据库中创建用户(注册/注册)吗?如果是,如何?(安卓)
- python - 从 select 语句 mysql.connector 中获取数据
- python - 如何修复 NotImplementedError?
- vba - 根据字符串中的分隔路径将项目附加到电子邮件
- android - 每次我导航到新片段时都会分配内存
- spring-boot - Spring boot with lettuce:无法连接,事件执行器组被终止