簡體   English   中英

使用 GraphQL 設置訂閱的問題

[英]Issue setting up subscription with 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,
    },
  })
});

檢查文檔以獲取其他示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM