从控制台的“创建资源”功能开始,我们来讨论一下它是如何工作的。假设我们有这个模式。
type Comment {
eventId: ID!
commentId: String!
content: String
}
type CommentConnection {
items: [Comment]
nextToken: String
}
input CreateCommentInput {
eventId: ID!
commentId: String!
content: String
}
input CreateEventInput {
id: ID!
name: String
where: String
when: String
description: String
}
input DeleteCommentInput {
eventId: ID!
}
input DeleteEventInput {
id: ID!
}
type Event {
id: ID!
name: String
where: String
when: String
description: String
comments(limit: Int, nextToken: String): CommentConnection
}
type EventConnection {
items: [Event]
nextToken: String
}
type Mutation {
createEvent(input: CreateEventInput!): Event
updateEvent(input: UpdateEventInput!): Event
deleteEvent(input: DeleteEventInput!): Event
createComment(input: CreateCommentInput!): Comment
updateComment(input: UpdateCommentInput!): Comment
deleteComment(input: DeleteCommentInput!): Comment
}
type Query {
getEvent(id: ID!): Event
listEvents(first: Int, after: String): EventConnection
getComment(eventId: ID!): Comment
listComments(first: Int, after: String): CommentConnection
}
type Subscription {
onCreateEvent(
id: ID,
name: String,
where: String,
when: String,
description: String
): Event
@aws_subscribe(mutations: ["createEvent"])
onUpdateEvent(
id: ID,
name: String,
where: String,
when: String,
description: String
): Event
@aws_subscribe(mutations: ["updateEvent"])
onDeleteEvent(
id: ID,
name: String,
where: String,
when: String,
description: String
): Event
@aws_subscribe(mutations: ["deleteEvent"])
onCreateComment(eventId: ID, commentId: String, content: String): Comment
@aws_subscribe(mutations: ["createComment"])
onUpdateComment(eventId: ID, commentId: String, content: String): Comment
@aws_subscribe(mutations: ["updateComment"])
onDeleteComment(eventId: ID, commentId: String, content: String): Comment
@aws_subscribe(mutations: ["deleteComment"])
}
input UpdateCommentInput {
eventId: ID!
commentId: String
content: String
}
input UpdateEventInput {
id: ID!
name: String
where: String
when: String
description: String
}
schema {
query: Query
mutation: Mutation
subscription: Subscription
}
这就是在事件和评论类型上运行创建资源后架构应该是什么样子。当使用评论类型浏览“创建资源”流程时,您应该选择eventId作为表的哈希键和评论ID作为排序键。对于事件类型,您可以将“id”保留为单个哈希键。那么这对我们有什么作用呢?
首先,它创建了 2 个 DynamoDB 表来保存 Event 和 Comment 类型的对象。然后,它将这些表导入为 AppSync 数据源,并生成新的架构部分,包括输入对象、对象以及查询和突变字段,并将它们保存到架构中。它还连接了特定于您刚刚定义的新表的解析器,并将它们附加到新生成的查询和突变字段,这些字段实现了常见的 CRUD 模式。不幸的是,这还不能理解关系,所以我们必须自己添加这些关系。为此,我们首先按照您的要求进行突变以创建关系,为了完整性,我们还将进行查询。
正如您已经完成的那样,您将需要将类似的内容添加到您的架构中
type Mutation {
commentOnEvent(input: CommentOnEventInput!): Comment
}
input CommentOnEventInput {
eventId: ID!
content: String
}
保存架构,然后单击“附加”Mutation.commentOnEvent字段添加解析器。选择我们之前创建的 CommentTable 数据源,并从映射模板中输入:
{
"version" : "2017-02-28",
"operation" : "PutItem",
"key" : {
"eventId": $util.dynamodb.toDynamoDBJson($ctx.args.input.eventId),
"commentId": $util.dynamodb.toDynamoDBJson($util.autoId()),
},
"attributeValues" : $util.dynamodb.toMapValuesJson($ctx.args.input)
}
以及响应映射模板
$util.toJson($context.result)
单击保存。现在您应该能够运行如下查询:
mutation {
commentOnEvent(input: { eventId: "***", content: "A comment"}) {
eventId
content
}
}
现在让我们添加通过关系读取数据的功能。例如。我希望能够运行这样的查询:
query {
getEvent(id: "***") {
id
comments(first: 5) {
items {
content
}
}
}
}
为此,我们首先将以下部分添加到架构中。
type Event {
# add this to existing fields
comments(first: Int, after: String): CommentConnection
}
点击保存,然后点击“附加”活动.评论场地。再次选择 CommentTable 数据源,然后为请求映射模板提供以下内容。
# Event.comments.request.vtl
{
"version" : "2017-02-28",
"operation" : "Query",
"query" : {
"expression": "eventId = :eventId",
"expressionValues" : {
":eventId" : {
"S" : "${ctx.source.id}"
}
}
},
"limit": $util.defaultIfNull(${ctx.args.first}, 20),
"nextToken": $util.toJson($util.defaultIfNullOrBlank($ctx.args.after, null))
}
注意$ctx.source.id。既然我们正在解决活动.评论场、$ctx.source是我们正在解析评论的事件类型的实例。实际上,这使得我们在任何地方都可以包含{ comments { ... }
在选择集中Event类型,只会获取父事件的注释。然后您可以返回分页结果对象。
# Event.comments.response.vtl
# $ctx.result = { items: [...], nextToken: "..." }
$util.toJson($ctx.result)
这应该可以解决问题。现在您可以运行这两个查询并查看结果。
mutation {
commentOnEvent(input: { eventId: "***", content: "A comment"}) {
eventId
content
}
}
query {
getEvent(id: "***") {
id
comments(first: 5) {
items {
content
}
}
}
}
希望这可以帮助。