简体   繁体   中英

Issue setting up subscription with GraphQL

Good day:

I"m trying to setup my graphql server for a subscription. This is my 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
});

However, I'm getting this error when I try to run my subscription server:

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."
  }
]

A field definition is an object that includes these properties: type , args , description , deprecationReason and resolve . All these properties are optional except type . Each field in your field map must be an object like this -- you cannot just set the field to a type like you're doing.

Incorrect:

const subscriptionType = new GraphQLObjectType({
  name: "Subscription",
  fields: () => ({
    chatCreated: ChatCreatedSubscription,
    chatConnected: ChatConnectedSubscription
  })
});

Correct:

const subscriptionType = new GraphQLObjectType({
  name: "Subscription",
  fields: () => ({
    chatCreated: {
      type: ChatCreatedSubscription,
    },
    chatConnected: {
      type: ChatConnectedSubscription,
    },
  })
});

Check the docs for additional examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM