可以使用GraphQL服务上的带别名的mutation操作来解决此问题。以下是一个示例:
首先,定义具有别名的mutation:
input CreatePostInput {
title: String!
content: String!
}
type Mutation {
createPost(input: CreatePostInput): Post!
@aws_iam
@primaryKey(fields: ["id"]) {
id: ID!
title: String!
content: String!
}
}
在此示例中,“createPost”操作具有“id”、“title”和“content”字段。现在,我们需要使用别名将它们重新命名,使它们与订阅中使用的名称匹配。以下是如何使用别名更改字段名称:
input CreatePostInput {
postTitle: String!
postContent: String!
}
type Mutation {
createPost(input: CreatePostInput): Post!
@aws_iam
@primaryKey(fields: ["id"]) {
id: ID!
title: String! @alias(name: "postTitle")
content: String! @alias(name: "postContent")
}
}
在此示例中,“createPost”操作现在具有“postTitle”和“postContent”字段的别名,以匹配订阅中使用的字段名称。
现在,我们可以使用带别名的操作创建订阅:
type Subscription {
onCreatePost(id: ID!, postTitle: String!, postContent: String!): Post
@aws_subscribe(mutations: ["createPost"])
}
在此示例中,“onCreatePost”订阅操作使用了与别名更改后的“createPost”操作相同的字段名称。
通过使用别名,我们可以确保订阅中使用的字段名称与mutation操作中使用的别名名称匹配,从而解决了AppSync中订阅省略别名字段的问题。
上一篇:Appsync:订阅数组中的元素