突变以在AWS AppSync上创建关系 [英] mutation to create relations on AWS AppSync

查看:90
本文介绍了突变以在AWS AppSync上创建关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图进行一种变异,以创建与两种不同类型的关系,但收效甚微.

I have been trying to run a mutation to create relations to two separate Types with not much success.

**模式**

(我已使用创建资源"在DynamoDB中创建表)

(I have used "Create Resources" to create tables in 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
}

**突变**

变异#1:

mutation {
    createEvent(input: {
    id: "id8888"
        name: "some event"
    where: "Tokyo"
        when: "tomorrow"
        description: "desc for event"
  })
  {
    id
        name
    }
}

突变#1给出:

{
  "data": {
    "createEvent": {
      "id": "id8888",
      "name": "some event"
    }
  }
}

第2种变异:

mutation {
  commentOnEvent(input : {
    eventId: "id8888"
    commentId: "id2222"
    content: "some content"
  })
  {
    commentId
    content
  }
}

第2种变异给出:

{
  "data": {
    "commentOnEvent": null
  }
}

在由AWS AppSync创建的React示例中,自动创建commentId,但是我无法在手动创建的模式和资源中重新创建它.

In the React sample created by AWS AppSync creates commentId automatically but I can't recreate that in manually created schema and resource.

我想知道如何在不同的类型上建立关系并对其进行查询.有人成功做到了吗?

I would like to know how I can establish relations on separate types and query them. Has anybody successfully done this??

推荐答案

从控制台的创建资源"功能入手,可以讨论其工作原理.假设我们有这个模式.

Starting from the console's "Create resources" functionality lets talk through how this works. Assume we have this schema.

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 作为表的哈希键,并选择 commentId 作为排序键.对于事件类型,您可以将"id"保留为单个哈希键.那对我们有什么作用?

This is what the schema should look like after running Create Resources on the Event and Comment types. When going through the "Create Resources" flow with the Comment type you should choose the eventId as the table's hash key and the commentId as the sort key. For the Event type you can leave "id" as the single hash key. So what did that do for us?

首先,它创建了2个DynamoDB表来保存我们类型为Event和Comment的对象.然后,将这些表导入为AppSync数据源,并生成包括输入对象,对象以及查询和突变字段在内的新架构部分,并将其保存到架构中.它还将特定于您刚刚定义的新表的解析器连接起来,并将其附加到新生成的实现通用CRUD模式的查询和变异字段中.不幸的是,这还不了解关系,因此我们必须自己添加这些关系.为此,我们首先进行突变以按照您的要求创建关系,并且为了完整起见,我们还将进行查询.

First it created 2 DynamoDB tables to hold our objects of type Event and Comment. It then imported those tables as AppSync data sources and generated new schema parts including input objects, objects, and query and mutation fields and saved them to the schema. It also wired up resolvers specific to the new table you just defined and attached them to the newly generated query and mutation fields that implement common CRUD patterns. Unfortunately this does not yet understand relations so we have to add those ourselves. To do that let's first make the mutation to create relations as you have asked about and for completeness we will do a query as well.

已经完成,您将需要在架构中添加类似的内容

As you have already done, you are going to need to add something like this to your schema

type Mutation {
  commentOnEvent(input: CommentOnEventInput!): Comment
}
input CommentOnEventInput {
  eventId: ID!
  content: String
}

保存架构,然后在 Mutation.commentOnEvent 字段上单击附加"以添加解析器.选择我们之前创建的CommentTable数据源,然后从映射模板中添加以下内容:

Save the schema and then click "Attach" on the Mutation.commentOnEvent field to add a resolver. Select the CommentTable data source we created earlier and from the mapping template put this:

{
    "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)

点击保存.现在您应该可以运行这样的查询:

Click save. Now you should be able to run a query like this:

mutation {
  commentOnEvent(input: { eventId: "***", content: "A comment"}) {
    eventId
    content
  }
}

现在让我们添加一个通过关系读取数据的方法.例如.我希望能够运行这样的查询:

Let's now add away to read data via a relation. E.G. I want to be able to run a query like this:

query {
  getEvent(id: "***") {
    id
    comments(first: 5) {
      items {
        content
      }
    }
  }
}

为此,我们首先将以下部分添加到架构中.

To do this lets first add the following parts to the schema.

type Event {
  # add this to existing fields
  comments(first: Int, after: String): CommentConnection
}

点击保存,然后点击 Event.comments 字段上的附加".再次选择CommentTable数据源,然后为请求映射模板提供以下内容.

Click save then click "Attach" on the Event.comments field. Select the CommentTable data source again and then provide the following for the request mapping template.

# 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 .由于我们要解析 Event.comments 字段,因此 $ ctx.source 是我们要为其解析注释的Event类型的实例.实际上,这使得在 Event 类型的选择集中包含{ comments { ... }的任何地方,仅会提取父事件的注释.然后,您可以返回分页的结果对象.

Notice the $ctx.source.id. Since we are resolving the Event.comments field, the $ctx.source is the instance of the Event type that we are resolving comments for. In effect this makes it so that anywhere we include { comments { ... } in a selection set on the Event type, only comments for the parent event will be fetched. And then you can return the paginated result object.

# Event.comments.response.vtl
# $ctx.result = { items: [...], nextToken: "..." }
$util.toJson($ctx.result)

这应该可以解决问题.现在,您可以运行这两个查询并查看结果.

This should do the trick. Now you can run both these queries and see the results.

mutation {
  commentOnEvent(input: { eventId: "***", content: "A comment"}) {
    eventId
    content
  }
}


query {
  getEvent(id: "***") {
    id
    comments(first: 5) {
      items {
        content
      }
    }
  }
}

希望这会有所帮助.

这篇关于突变以在AWS AppSync上创建关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆