本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
(可选)从 Amazon DynamoDB 导入
AmazonAppSync 可以自动创建 GraphQL 架构并将解析程序连接到现有的 Amazon DynamoDB 表。如果您有要通过 GraphQL 终端节点公开其数据的 DynamoDB 表,或者您更愿意先以数据库设计(而不是 GraphQL 架构)为起点,则导入功能很有用。
导入 DynamoDB 表
从Amazon在 AppSync 控制台中,转至数据源页面上,选择New键入数据源的友好名称,然后选择 Amazon DynamoDB 作为数据源类型。选择相应的表,然后选择 Automatically generate GraphQL (自动生成 GraphQL)。
以下两个代码编辑器显示 GraphQL 架构:
-
顶部编辑器-您可以使用此编辑器为类型指定自定义名称(例如:
type MYNAME {...}),当您运行查询或更改时,此类型将包含来自 DynamoDB 表的数据。还可以向类型添加字段,如 DynamoDB 非键属性(导入时无法检测)。 -
底部编辑器 – 使用此只读编辑器可以查看生成的 GraphQL 架构片段。它显示会将哪些类型、查询和更改合并到您的架构中。如果您在顶部编辑器中编辑类型,底部编辑器中的内容会相应发生变化。
选择创建。您的架构会被合并,并创建解析程序。此过程完成之后,您可以运行更改和查询,如使用您的 API 中所述。
注意:A GraphQLinput类型是为已创建的架构的参数创建的。例如,如果您导入一个名为 Books 的表,则输入类型可能如下所示:
input CreateBooksInput { ISBN: String! Author: String Title: String Price: String }
要在 GraphQL 查询或更改中使用此类型,执行以下操作:
mutation add { createBooks(input:{ ISBN:2349238 Author:"Nadia Bailey" Title:"Running in the park" Price:"10" }){ ISBN Author } }
导入的示例架构
假设您有一个采用以下格式的 DynamoDB 表:
{ Table: { AttributeDefinitions: [ { AttributeName: 'authorId', AttributeType: 'S' }, { AttributeName: 'bookId', AttributeType: 'S' }, { AttributeName: 'title', AttributeType: 'S' } ], TableName: 'BookTable', KeySchema: [ { AttributeName: 'authorId', KeyType: 'HASH' }, { AttributeName: 'title', KeyType: 'RANGE' } ], TableArn: 'arn:aws:dynamodb:us-west-2:012345678910:table/BookTable', LocalSecondaryIndexes: [ { IndexName: 'authorId-bookId-index', KeySchema: [ { AttributeName: 'authorId', KeyType: 'HASH' }, { AttributeName: 'bookId', KeyType: 'RANGE' } ], Projection: { ProjectionType: 'ALL' }, IndexSizeBytes: 0, ItemCount: 0, IndexArn: 'arn:aws:dynamodb:us-west-2:012345678910:table/BookTable/index/authorId-bookId-index' } ], GlobalSecondaryIndexes: [ { IndexName: 'title-authorId-index', KeySchema: [ { AttributeName: 'title', KeyType: 'HASH' }, { AttributeName: 'authorId', KeyType: 'RANGE' } ], Projection: { ProjectionType: 'ALL' }, IndexArn: 'arn:aws:dynamodb:us-west-2:012345678910:table/BookTable/index/title-authorId-index' } ] } }
顶部的类型编辑器中显示以下内容:
type Book { # Key attributes. Changing these may result in unexpected behavior. authorId: String! title: String! # Index attributes. Changing these may result in unexpected behavior. bookId: String # Add additional non-key attributes below. isPublished: Boolean }
此顶部编辑器是可写入的,您需要添加底部编辑器中显示的非键属性(例如,类似)isPublished) 手动因为它们无法从 DynamoDB 自动推断出来。例如,如果 DynamoDB 表中的项目有另一个名为的属性:rating,您需要将其添加到isPublished将其填充到 GraphQL 架构中。在此示例中,底部编辑器中将显示以下建议的架构合并:
type Query { getBook(authorId: ID!, title: String!): Book listBooks(first: Int, after: String): BookConnection getBookByAuthorIdBookIdIndex(authorId: ID!, bookId: ID!): Book queryBooksByAuthorIdBookIdIndex(authorId: ID!, first: Int, after: String): BookConnection getBookByTitleAuthorIdIndex(title: String!, authorId: ID!): Book queryBooksByTitleAuthorIdIndex(title: String!, first: Int, after: String): BookConnection } type Mutation { createBook(input: CreateBookInput!): Book updateBook(input: UpdateBookInput!): Book deleteBook(input: DeleteBookInput!): Book } type Subscription { onCreateBook(authorId: ID, title: String, bookId: ID, isPublished: Boolean): Book @aws_subscribe(mutations: ["createBook"]) onUpdateBook(authorId: ID, title: String, bookId: ID, isPublished: Boolean): Book @aws_subscribe(mutations: ["updateBook"]) onDeleteBook(authorId: ID, title: String, bookId: ID, isPublished: Boolean): Book @aws_subscribe(mutations: ["deleteBook"]) } input CreateBookInput { authorId: ID! title: String! bookId: ID! isPublished: Boolean } input UpdateBookInput { authorId: ID! title: String! bookId: ID isPublished: Boolean } input DeleteBookInput { authorId: ID! title: String! } type BookConnection { items: [Book] nextToken: String }