Tak*_*ano 4 amazon-web-services graphql aws-appsync
我一直在尝试运行一个突变来创建与两个不同类型的关系,但没有太大成功。
** 架构 **
(我已经使用“创建资源”在 DynamoDB 中创建表)
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
commentOnEvent(input: commentOnEventInput!): Comment
}
type Query {
fetchEvent(id: ID!): Event
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
}
input commentOnEventInput {
eventId: ID!
content: String
}
schema {
query: Query
mutation: Mutation
subscription: Subscription
}
Run Code Online (Sandbox Code Playgroud)
** 突变 **
突变#1:
mutation {
createEvent(input: {
id: "id8888"
name: "some event"
where: "Tokyo"
when: "tomorrow"
description: "desc for event"
})
{
id
name
}
}
Run Code Online (Sandbox Code Playgroud)
突变 #1 给出:
{
"data": {
"createEvent": {
"id": "id8888",
"name": "some event"
}
}
}
Run Code Online (Sandbox Code Playgroud)
突变#2:
mutation {
commentOnEvent(input : {
eventId: "id8888"
commentId: "id2222"
content: "some content"
})
{
commentId
content
}
}
Run Code Online (Sandbox Code Playgroud)
突变 #2 给出:
{
"data": {
"commentOnEvent": null
}
}
Run Code Online (Sandbox Code Playgroud)
在 AWS AppSync 创建的 React 示例中,会自动创建 commentId,但我无法在手动创建的架构和资源中重新创建它。
我想知道如何在不同的类型上建立关系并查询它们。有没有人成功做到这一点??
从控制台的“创建资源”功能开始,让我们谈谈它是如何工作的。假设我们有这个模式。
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
}
Run Code Online (Sandbox Code Playgroud)
这就是在 Event 和 Comment 类型上运行 Create Resources 后架构的样子。在使用 Comment 类型浏览“创建资源”流程时,您应该选择eventId作为表的散列键,选择commentId作为排序键。对于事件类型,您可以将“id”作为单个散列键。那么这对我们有什么帮助呢?
首先,它创建了 2 个 DynamoDB 表来保存我们的 Event 和 Comment 类型的对象。然后将这些表作为 AppSync 数据源导入并生成新的架构部分,包括输入对象、对象以及查询和变异字段,并将它们保存到架构中。它还连接了特定于您刚刚定义的新表的解析器,并将它们附加到新生成的实现常见 CRUD 模式的查询和变异字段。不幸的是,这还不能理解关系,所以我们必须自己添加这些关系。为此,让我们首先按照您的要求进行更改以创建关系,并且为了完整性,我们还将进行查询。
正如您已经完成的那样,您将需要将这样的内容添加到您的架构中
type Mutation {
commentOnEvent(input: CommentOnEventInput!): Comment
}
input CommentOnEventInput {
eventId: ID!
content: String
}
Run Code Online (Sandbox Code Playgroud)
保存架构,然后在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)
}
Run Code Online (Sandbox Code Playgroud)
和响应映射模板
$util.toJson($context.result)
Run Code Online (Sandbox Code Playgroud)
点击保存。现在您应该能够运行这样的查询:
mutation {
commentOnEvent(input: { eventId: "***", content: "A comment"}) {
eventId
content
}
}
Run Code Online (Sandbox Code Playgroud)
现在让我们添加通过关系读取数据。EG 我希望能够运行这样的查询:
query {
getEvent(id: "***") {
id
comments(first: 5) {
items {
content
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
为此,让我们首先将以下部分添加到架构中。
type Event {
# add this to existing fields
comments(first: Int, after: String): CommentConnection
}
Run Code Online (Sandbox Code Playgroud)
单击保存,然后单击Event.comments字段上的“附加” 。再次选择 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))
}
Run Code Online (Sandbox Code Playgroud)
注意$ctx.source.id。由于我们正在解析Event.comments字段,因此$ctx.source是我们正在解析注释的事件类型的实例。实际上,这使得我们{ comments { ... }
在事件类型的选择集中包含的任何地方,只会获取父事件的评论。然后你可以返回分页的结果对象。
# Event.comments.response.vtl
# $ctx.result = { items: [...], nextToken: "..." }
$util.toJson($ctx.result)
Run Code Online (Sandbox Code Playgroud)
这应该可以解决问题。现在您可以运行这两个查询并查看结果。
mutation {
commentOnEvent(input: { eventId: "***", content: "A comment"}) {
eventId
content
}
}
query {
getEvent(id: "***") {
id
comments(first: 5) {
items {
content
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
归档时间: |
|
查看次数: |
3695 次 |
最近记录: |