首页 > 解决方案 > 如何在 GraphQL 中正确地使用参数进行订阅?

问题描述

AWS AppSync中,我尝试测试我的GraphQL架构。我有点困惑,需要帮助。当有争论时,我的订阅watchMessages不起作用。如果我从模式中删除参数并对其进行测试,它就可以工作。但我只需要接收来自特定房间的消息。我错过了什么?

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

我做了这样的订阅查询

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

我做了这样的突变查询

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

标签: amazon-web-servicesgraphqlaws-appsync

解决方案


最后,我找到了解决方案。所有订阅参数都必须作为返回类型中的字段存在。这意味着参数的类型也必须与返回对象中的字段类型相匹配。我roomId在类型中添加了字段CreateMessageOutput

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

推荐阅读