2

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."
  }
]
Dean
  • 887
  • 4
  • 16
  • 42

1 Answers1

2

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.

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • @tried that but, getting: `Schema must contain unique named types but contains multiple types named "undefined"` – Dean Feb 15 '19 at 20:01
  • Not sure if it's relevant to that error, but `ChatConnectedSubscription` is also a plain object and not an instance of GraphQLObjectType. – Daniel Rearden Feb 15 '19 at 20:04
  • You'll need to specify types for all fields in your schema, i.e. `chatCreated`, `chatConnected` and so on. – Daniel Rearden Feb 15 '19 at 20:06