最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

使用GraphQL设置订阅问题

运维笔记admin7浏览0评论

使用GraphQL设置订阅问题

使用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."
  }
]
回答如下:

字段定义是包含以下属性的对象: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了解更多示例。

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论