使用 GraphQL 设置订阅问题
Posted
技术标签:
【中文标题】使用 GraphQL 设置订阅问题【英文标题】:Issue setting up subscription with GraphQL 【发布时间】:2019-07-09 17:23:12 【问题描述】:美好的一天:
我正在尝试为订阅设置我的 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."
]
【问题讨论】:
【参考方案1】:字段定义是包含以下属性的对象: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,
,
)
);
查看the docs 了解更多示例。
【讨论】:
@试过了,但是,得到:Schema must contain unique named types but contains multiple types named "undefined"
不确定它是否与该错误有关,但ChatConnectedSubscription
也是一个普通对象,而不是 GraphQLObjectType 的实例。
您需要为架构中的所有字段指定类型,即chatCreated
、chatConnected
等。以上是关于使用 GraphQL 设置订阅问题的主要内容,如果未能解决你的问题,请参考以下文章