在中使用HTTP解析器 AWS AppSync - AWS AppSync

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

在中使用HTTP解析器 AWS AppSync

注意

我们现在主要支持 APPSYNC _JS 运行时及其文档。请考虑在此处使用 APPSYNC _JS 运行时及其指南。

AWS AppSync 允许您使用支持的数据源(即亚马逊 DynamoDB AWS Lambda、Amazon Service 或 OpenSearch Amazon Aurora)执行各种操作,此外还可以使用HTTP任何任意终端节点来解析 GraphQL 字段。HTTP终端节点可用后,您可以使用数据源连接到这些终端节点。然后,您可以在架构中配置一个解析器以执行 GraphQL 操作(如查询、变更和订阅)。本教程将引导您了解一些常见示例。

在本教程中,您将使用 RESTAPI(使用亚马逊API网关和 Lambda 创建)和 GraphQL AWS AppSync 终端节点。

一键设置

如果您想在配置了终端节点的情况下自动设置 GraphQL 终端节点( AWS AppSync 使用 Amazon G API ateway 和 Lambda),则可以使用以下模板:HTTP AWS CloudFormation

Blue button labeled "Launch Stack" with an arrow icon indicating an action to start.

创建一个 REST API

您可以使用以下 AWS CloudFormation 模板来设置适用于本教程的REST端点:

Blue button labeled "Launch Stack" with an arrow icon indicating an action to start.

AWS CloudFormation 堆栈执行以下步骤:

  1. 设置 Lambda 函数,其中包含您的微服务的业务逻辑。

  2. RESTAPI使用以下端点/方法/内容类型组合设置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

DELETE

application/json

创建你的 GraphQL API

要在下面创建 GraphQLAPI,请执行以下操作: AWS AppSync

  • 打开 AWS AppSync 控制台并选择创建API

  • 在API名称中键入UserData

  • 选择自定义架构

  • 选择创建

AWS AppSync 控制台使用API密钥身份验证模式API为您创建一个新的 GraphQL。在本教程的其余部分中,您可以使用控制台设置 GraphQL 的其余部分API并在其上运行查询。

创建 GraphQL 架构

现在你已经有一个 GraphQL 了API,让我们来创建一个 GraphQL 架构。在 AWS 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选项卡上,选择 “新建”,然后键入数据源的友好名称(例如HTTP)。

  • 数据源类型中,选择HTTP

  • 将终端节点设置为创建的API网关终端节点。确保不将阶段名称作为终端节点的一部分包含在内。

注意:目前仅支持公共终端节点 AWS AppSync。

注意:有关 AWS AppSync 服务认可的认证机构的更多信息,请参阅HTTPS终端节点认可的证书颁发机构 (CA)。 AWS AppSync

配置解析器

在此步骤中,将 http 数据源连接到getUser查询。

设置解析器:

  • 选择架构选项卡。

  • 查询类型下方右侧的数据类型窗格中,找到相应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" } } }
  • 选择架构选项卡。

  • 在 “突变” 下方右侧的 “数据类型” 窗格中,找到相应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" } } }

调用服务 AWS

您可以使用HTTP解析器为服务设置 G API raphQL 接口 AWS 。HTTP请求 AWS 必须使用签名版本 4 流程进行签名,这样 AWS 才能识别是谁发送的。 AWS AppSync 当您将IAM角色与HTTP数据源关联时,代表您计算签名。

您还提供了两个额外的组件来使用HTTP解析器调用 AWS 服务:

  • 具有调用 AWS 服务的权限的IAM角色 APIs

  • 数据来源中的签名配置

例如,如果您想与HTTP解析者一起调用该ListGraphqlApis 操作,则首先创建一个附加以下策略的IAM角色: AWS AppSync

{ "Version": "2012-10-17", "Statement": [ { "Action": [ "appsync:ListGraphqlApis" ], "Effect": "Allow", "Resource": "*" } ] }

接下来,为创建HTTP数据源 AWS AppSync。在此示例中,您在美国西部(俄勒冈)区域拨打电话 AWS AppSync 。在名为的文件中设置以下HTTP配置http.json,其中包括签名区域和服务名称:

{ "endpoint": "https://appsync.us-west-2.amazonaws.com/", "authorizationConfig": { "authorizationType": "AWS_IAM", "awsIamConfig": { "signingRegion": "us-west-2", "signingServiceName": "appsync" } } }

然后,使用创建具有关联角色的数据源,如下所示: AWS CLI

aws appsync create-data-source --api-id <API-ID> \ --name AWSAppSync \ --type HTTP \ --http-config file:///http.json \ --service-role-arn <ROLE-ARN>

当您将解析器附加到架构中的字段时,请使用以下请求映射模板进行调用 AWS AppSync:

{ "version": "2018-05-29", "method": "GET", "resourcePath": "/v1/apis" }

当您对该数据源运行 GraphQL 查询时,使用您提供的角色对请求进行 AWS AppSync 签名,并在请求中包含签名。该查询会返回您在该区域的账户APIs中的 AWS AppSync GraphQL 列表。 AWS