在 Amazon AppSync 中使用 HTTP 解析器
注意
我们现在主要支持 APPSYNC_JS 运行时系统及其文档。请考虑使用 APPSYNC_JS 运行时系统和此处的指南。
除了使用任意 HTTP 终端节点解析 GraphQL 字段以外,Amazon AppSync 还允许您使用支持的数据来源(即 Amazon Lambda、Amazon DynamoDB、Amazon OpenSearch Service 或 Amazon Aurora)执行各种操作。在您的 HTTP 终端节点可用后,您可以使用数据来源连接它们。然后,您可以在架构中配置一个解析器以执行 GraphQL 操作(如查询、变更和订阅)。本教程将引导您了解一些常见示例。
在本教程中,您在 Amazon AppSync GraphQL 终端节点中使用 REST API(是使用 Amazon API Gateway 和 Lambda 创建的)。
一键设置
如果要在配置了 HTTP 终端节点的 Amazon AppSync 中自动设置 GraphQL 终端节点(使用 Amazon API Gateway 和 Lambda),您可以使用以下 Amazon CloudFormation 模板:
创建 REST API
您可以使用以下 Amazon CloudFormation 模板来设置适用于本教程的 REST 终端节点:
Amazon CloudFormation 堆栈将执行以下步骤:
-
设置 Lambda 函数,其中包含您的微服务的业务逻辑。
-
使用以下终端节点/方法/内容类型组合设置一个 API Gateway REST API:
API 资源路径 | HTTP 方法 | 支持的内容类型 |
---|---|---|
/v1/users |
POST |
application/json |
/v1/users |
GET |
application/json |
/v1/users/1 |
GET |
application/json |
/v1/users/1 |
PUT |
application/json |
/v1/users/1 |
删除 |
application/json |
创建您的 GraphQL API
要在 Amazon AppSync 中创建 GraphQL API,请执行以下操作:
-
打开 Amazon AppSync 控制台,然后选择创建 API。
-
对于 API 名称,请键入
UserData
。 -
选择自定义架构。
-
选择创建。
Amazon AppSync 控制台使用 API 密钥身份验证模式为您创建新的 GraphQL API。您可以根据本教程后面的说明,使用控制台设置 GraphQL API 的其余部分,并针对它运行查询。
创建 GraphQL 架构
现在,您有一个 GraphQL API,让我们创建一个 GraphQL 架构。从 Amazon AppSync 控制台的架构编辑器中,确保您的架构与以下架构匹配:
schema { query: Query mutation: Mutation } type Mutation { addUser(userInput: UserInput!): User deleteUser(id: ID!): User } type Query { getUser(id: ID): User listUser: [User!]! } type User { id: ID! username: String! firstname: String lastname: String phone: String email: String } input UserInput { id: ID! username: String! firstname: String lastname: String phone: String email: String }
配置您的 HTTP 数据来源
要配置 HTTP 数据来源,请执行以下操作:
-
在 DataSources (数据来源) 选项卡上,选择 New (新建),然后为数据来源键入一个友好名称(例如,
HTTP
)。 -
在 Data source type (数据来源类型) 中,选择 HTTP。
-
将终端节点设置为创建的 API 网关终端节点。确保不将阶段名称作为终端节点的一部分包含在内。
注意:Amazon AppSync 目前仅支持公有终端节点。
注意:有关 Amazon AppSync 服务识别的认证机构的更多信息,请参阅 Amazon AppSync 识别的 HTTPS 终端节点证书颁发机构 (CA)。
配置解析器
在此步骤中,您将 HTTP 数据来源连接到 getUser 查询。
设置解析器:
-
选择架构选项卡。
-
在右侧的数据类型窗格中,在 Query 类型下面找到 getUser 字段,然后选择附加。
-
在 Data source name (数据来源名称) 中,选择 HTTP。
-
在 Configure the request mapping template (配置请求映射模板) 中,粘贴以下代码:
{ "version": "2018-05-29", "method": "GET", "params": { "headers": { "Content-Type": "application/json" } }, "resourcePath": $util.toJson("/v1/users/${ctx.args.id}") }
-
在 Configure the response mapping template (配置响应映射模板) 中,粘贴以下代码:
## return the body #if($ctx.result.statusCode == 200) ##if response is 200 $ctx.result.body #else ##if response is not 200, append the response to error block. $utils.appendError($ctx.result.body, "$ctx.result.statusCode") #end
-
选择 Query (查询) 选项卡,然后运行以下查询:
query GetUser{ getUser(id:1){ id username } }
此查询应返回以下响应:
{ "data": { "getUser": { "id": "1", "username": "nadia" } } }
-
选择架构选项卡。
-
在右侧的数据类型窗格中,在 Mutation 下面找到 addUser 字段,然后选择附加。
-
在 Data source name (数据来源名称) 中,选择 HTTP。
-
在 Configure the request mapping template (配置请求映射模板) 中,粘贴以下代码:
{ "version": "2018-05-29", "method": "POST", "resourcePath": "/v1/users", "params":{ "headers":{ "Content-Type": "application/json", }, "body": $util.toJson($ctx.args.userInput) } }
-
在 Configure the response mapping template (配置响应映射模板) 中,粘贴以下代码:
## Raise a GraphQL field error in case of a datasource invocation error #if($ctx.error) $util.error($ctx.error.message, $ctx.error.type) #end ## if the response status code is not 200, then return an error. Else return the body ** #if($ctx.result.statusCode == 200) ## If response is 200, return the body. $ctx.result.body #else ## If response is not 200, append the response to error block. $utils.appendError($ctx.result.body, "$ctx.result.statusCode") #end
-
选择 Query (查询) 选项卡,然后运行以下查询:
mutation addUser{ addUser(userInput:{ id:"2", username:"shaggy" }){ id username } }
此查询应返回以下响应:
{ "data": { "getUser": { "id": "2", "username": "shaggy" } } }
调用 Amazon 服务
您可以使用 HTTP 解析器为 Amazon 服务设置 GraphQL API 接口。发送到 Amazon 的 HTTP 请求必须使用签名版本 4 流程进行签名,以使 Amazon 可以识别发送者。Amazon在将 IAM 角色与 HTTP 数据来源关联时,AppSync 代表您计算签名。
您提供两个额外的组件以使用 HTTP 解析器调用 Amazon 服务:
-
有权调用 Amazon 服务 API 的 IAM 角色
-
数据来源中的签名配置
例如,如果要使用 HTTP 解析器调用 ListGraphqlApis 操作,您需要先创建由 Amazon AppSync 担任的 IAM 角色并附加以下策略:
{ "Version": "2012-10-17", "Statement": [ { "Action": [ "appsync:ListGraphqlApis" ], "Effect": "Allow", "Resource": "*" } ] }
接下来,为 Amazon AppSync 创建 HTTP 数据来源。在该示例中,您在美国西部(俄勒冈州)区域中调用 Amazon AppSync。在名为 http.json
文件中设置以下 HTTP 配置,其中包括签名区域和服务名称:
{ "endpoint": "https://appsync.us-west-2.amazonaws.com/", "authorizationConfig": { "authorizationType": "AWS_IAM", "awsIamConfig": { "signingRegion": "us-west-2", "signingServiceName": "appsync" } } }
然后,使用 Amazon CLI 创建具有关联角色的数据来源,如下所示:
aws appsync create-data-source --api-id <API-ID> \ --name AWSAppSync \ --type HTTP \ --http-config file:///http.json \ --service-role-arn <ROLE-ARN>
在将一个解析器附加到架构中的字段时,请使用以下请求映射模板调用 Amazon AppSync:
{ "version": "2018-05-29", "method": "GET", "resourcePath": "/v1/apis" }
在为该数据来源运行 GraphQL 查询时,Amazon AppSync 使用您提供的角色对请求进行签名,并将签名包含在请求中。该查询返回您的账户在该 Amazon 区域中的 Amazon AppSync GraphQL API 列表。