node.js - 使用 GraphQL 设置订阅问题
问题描述
再会:
我正在尝试为订阅设置我的 graphql 服务器。这是我的 schema.js
const ChatCreatedSubscription = new GraphQLObjectType({
name: "ChatCreated",
fields: () => ({
chatCreated: {
subscribe: () => pubsub.asyncIterator(CONSTANTS.Websocket.CHANNEL_CONNECT_CUSTOMER)
}
})
});
const ChatConnectedSubscription = {
chatConnected: {
subscribe: withFilter(
(_, args) => pubsub.asyncIterator(`${args.id}`),
(payload, variables) => payload.chatConnect.id === variables.id,
)
}
}
const subscriptionType = new GraphQLObjectType({
name: "Subscription",
fields: () => ({
chatCreated: ChatCreatedSubscription,
chatConnected: ChatConnectedSubscription
})
});
const schema = new GraphQLSchema({
subscription: subscriptionType
});
但是,当我尝试运行订阅服务器时出现此错误:
ERROR introspecting schema: [
{
"message": "The type of Subscription.chatCreated must be Output Type but got: undefined."
},
{
"message": "The type of Subscription.chatConnected must be Output Type but got: undefined."
}
]
解决方案
字段定义是包含以下属性的对象:type
、args
、description
和。所有这些属性都是可选的,除了. 字段映射中的每个字段都必须是这样的对象——您不能只是将字段设置为您正在做的类型。deprecationReason
resolve
type
不正确:
const subscriptionType = new GraphQLObjectType({
name: "Subscription",
fields: () => ({
chatCreated: ChatCreatedSubscription,
chatConnected: ChatConnectedSubscription
})
});
正确的:
const subscriptionType = new GraphQLObjectType({
name: "Subscription",
fields: () => ({
chatCreated: {
type: ChatCreatedSubscription,
},
chatConnected: {
type: ChatConnectedSubscription,
},
})
});
检查文档以获取更多示例。
推荐阅读
- python - 使用 python 和 postgres 更快地运行一组查询
- python - 'QuerySet' 对象没有属性 'likes'
- javascript - 如何修复 Uncaught TypeError: $(...).owlCarousel 不是 Owl carousel 的函数
- javascript - 如何在前端多选下拉(或)多选格式化程序中获取后端数据
- xml - XSLT 选择多个同名属性
- node.js - 创建docker镜像时如何在应用程序路径上添加文件
- java - 在两个 json 文档之间创建合并补丁时出现 Nullpointer 异常
- typescript - Nuxt TypeScript:无法分配给“名称”,因为它是只读属性
- docker - 关于 docker -v 不起作用的问题 --> /srv/monero/chain:/home/monero/.bitmonero
- amazon-web-services - Terraform 语法有助于将来自两个实例的实例 ID 包含到单个数组中