How to make subscription with arguments correctly in GraphQL?

817 Views Asked by At

In AWS AppSync I tried to test my GraphQL schema. I'm a little confused and need help. My subscription called watchMessages doesn't work when there is an argument. If I remove the argument from schema and test it, it works. But I only need to receive messages from a specific room. What am I missing?

input CreateMessageInput {
    roomId: String!
    messageText: String
    messageAuthor: String
}

type CreateMessageOutput {
    messageId: String!
    messageCreatedDateTime: String!
    messageText: String!
    messageAuthor: String
}

type Mutation {
    createMessage(input: CreateMessageInput): CreateMessageOutput
}

type Subscription {
    watchMessages(roomId: String!): CreateMessageOutput
        @aws_subscribe(mutations: ["createMessage"])
}

I make such subscription query:

subscription MySubscription {
  watchMessages(roomId: "5d354323") {
    messageId
    messageCreatedDateTime
    messageText
    messageAuthor
  }
}

I make such mutation query:

mutation MyMutation {
createMessage(input: {roomId: "5d354323", messageAuthor: "Bob", messageText: "Hello!"}) {
    messageId
    messageCreatedDateTime
    messageText
    messageAuthor
  }
}
1

There are 1 best solutions below

0
On BEST ANSWER

Finally, I found the solution. All subscription parameters must exist as a field in the returning type. This means that the type of parameter must also match the type of the field in the returning object. I added roomId field to the CreateMessageOutput type.

type CreateMessageOutput {
    roomId: String!
    messageId: String!
    messageCreatedDateTime: String!
    messageText: String!
    messageAuthor: String
}