如何在 GraphQL 中正确地使用参数进行订阅?
How to make subscription with arguments correctly in 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
}
}
终于,我找到了解决办法。所有订阅参数必须作为返回类型中的字段存在。这意味着参数的类型也必须与返回对象中字段的类型相匹配。我将 roomId
字段添加到 CreateMessageOutput
类型。
type CreateMessageOutput {
roomId: String!
messageId: String!
messageCreatedDateTime: String!
messageText: String!
messageAuthor: String
}
在 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
}
}
终于,我找到了解决办法。所有订阅参数必须作为返回类型中的字段存在。这意味着参数的类型也必须与返回对象中字段的类型相匹配。我将 roomId
字段添加到 CreateMessageOutput
类型。
type CreateMessageOutput {
roomId: String!
messageId: String!
messageCreatedDateTime: String!
messageText: String!
messageAuthor: String
}