【问题标题】:Issue setting up subscription with GraphQL使用 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."
  }
]

【问题讨论】:

    标签: node.js graphql graphql-js graphql-subscriptions


    【解决方案1】:

    字段定义是包含以下属性的对象:typeargsdescriptiondeprecationReasonresolve。除了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 的实例。
    • 您需要为架构中的所有字段指定类型,即chatCreatedchatConnected 等。
    猜你喜欢
    • 2019-05-17
    • 2020-11-17
    • 2017-09-01
    • 2023-03-03
    • 2020-12-17
    • 1970-01-01
    • 2017-09-19
    • 2019-12-02
    • 2021-10-09
    相关资源
    最近更新 更多