

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

# 解析程序
<a name="sam-property-graphqlapi-resolver"></a>

为 GraphQL API 的字段配置解析器。 AWS Serverless Application Model (AWS SAM) 支持[JavaScript 管道解析器](https://docs.aws.amazon.com/appsync/latest/devguide/resolver-reference-overview-js.html)。

## 语法
<a name="sam-property-graphqlapi-resolver-syntax"></a>

要在 AWS Serverless Application Model (AWS SAM) 模板中声明此实体，请使用以下语法。

### YAML
<a name="sam-property-graphqlapi-resolver-syntax-yaml"></a>

```
OperationType:
  LogicalId:
    Caching: [CachingConfig](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-appsync-resolver-cachingconfig.html)
    CodeUri: String
    FieldName: String
    InlineCode: String 
    MaxBatchSize: Integer
    Pipeline: List
    Runtime: Runtime
    Sync: [SyncConfig](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-appsync-resolver-syncconfig.html)
```

## Properties
<a name="sam-property-graphqlapi-resolver-properties"></a>

`Caching`  <a name="sam-graphqlapi-resolver-caching"></a>
激活了缓存的解析程序的缓存配置。  
*类型*：[CachingConfig](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-appsync-resolver-cachingconfig.html)  
*必需*：否  
*CloudFormation 兼容性*：此属性直接传递给`AWS::AppSync::Resolver`资源的`[CachingConfig](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-resolver.html#cfn-appsync-resolver-cachingconfig)`属性。

`CodeUri`  <a name="sam-graphqlapi-resolver-codeuri"></a>
解析程序函数代码的 Amazon Simple Storage Service (Amazon S3) URI 或本地文件夹路径。  
如果您指定本地文件夹的路径，则 CloudFormation 要求在部署之前先将文件上传到 Amazon S3。您可以使用 AWS SAM CLI 来简化此过程。有关更多信息，请参阅 [如何在 AWS SAM 部署时上传本地文件](deploy-upload-local-files.md)。  
如果两者均未提供`CodeUri`或`InlineCode`，则 AWS SAM 将生成`InlineCode`将请求重定向到第一个管道函数并接收来自最后一个管道函数的响应。  
*类型*：字符串  
*必需*：否  
*CloudFormation 兼容性*：此属性直接传递给`AWS::AppSync::Resolver`资源的`[CodeS3Location](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-resolver.html#cfn-appsync-resolver-codes3location)`属性。

`FieldName`  <a name="sam-graphqlapi-resolver-fieldname"></a>
解析程序的名称。指定此属性以覆盖 `LogicalId` 值。  
*类型*：字符串  
*必需*：否  
*CloudFormation 兼容性*：此属性直接传递给`AWS::AppSync::Resolver`资源的`[FieldName](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-resolver.html#cfn-appsync-resolver-fieldname)`属性。

`InlineCode`  <a name="sam-graphqlapi-resolver-inlinecode"></a>
包含请求和响应函数的解析程序代码。  
如果两者均未提供`CodeUri`或`InlineCode`，则 AWS SAM 将生成`InlineCode`将请求重定向到第一个管道函数并接收来自最后一个管道函数的响应。  
*类型*：字符串  
*必需*：否  
*CloudFormation 兼容性*：此属性直接传递给`AWS::AppSync::Resolver`资源的`[Code](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-resolver.html#cfn-appsync-resolver-code)`属性。

`LogicalId`  <a name="sam-graphqlapi-resolver-logicalid"></a>
解析程序的唯一名称。在 GraphQL 架构中，解析程序名称应与其使用的字段名称相匹配。对`LogicalId` 也使用这个字段名称。  
*类型*：字符串  
*是否必需*：是  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

`MaxBatchSize`  <a name="sam-graphqlapi-resolver-maxbatchsize"></a>
向 `BatchInvoke` 操作中单个 AWS Lambda 函数发送的解析程序请求输入的最大数量。  
*类型*：整数  
*必需*：否  
*CloudFormation 兼容性*：此属性直接传递给`AWS::AppSync::Resolver`资源的`[MaxBatchSize](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-resolver.html#cfn-appsync-resolver-maxbatchsize)`属性。

`OperationType`  <a name="sam-graphqlapi-resolver-operationtype"></a>
与解析程序关联的 GraphQL 操作类型。例如，`Query`、`Mutation` 或 `Subscription`。您可以按`LogicalId` 将多个解析程序嵌套在单个`OperationType`中。  
*类型*：字符串  
*是否必需*：是  
*CloudFormation 兼容性*：此属性直接传递给`AWS::AppSync::Resolver`资源的`[TypeName](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-resolver.html#cfn-appsync-resolver-typename)`属性。

`Pipeline`  <a name="sam-graphqlapi-resolver-pipeline"></a>
与管道解决程序关联的函数。在列表中按逻辑 ID 指定函数。  
*类型*：列表  
*是否必需*：是  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。它类似于 `AWS::AppSync::Resolver` 资源的 `[PipelineConfig](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-resolver.html#cfn-appsync-resolver-pipelineconfig)` 属性。

`Runtime`  <a name="sam-graphqlapi-resolver-runtime"></a>
管道解析程序或函数的运行时间。指定要使用的名称和版本。  
*类型*：[运行时](sam-property-graphqlapi-resolver-runtime.md)  
*是否必需*：是  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。它类似于 `AWS::AppSync::Resolver` 资源的 `[Runtime](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-resolver.html#cfn-appsync-resolver-runtime)` 属性。

`Sync`  <a name="sam-graphqlapi-resolver-sync"></a>
描述解析程序的同步配置。  
指定在调用解析程序时要使用的冲突检测策略和解决策略。  
*类型*：[SyncConfig](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-appsync-resolver-syncconfig.html)  
*必需*：否  
*CloudFormation 兼容性*：此属性直接传递给`AWS::AppSync::Resolver`资源的`[SyncConfig](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-resolver.html#cfn-appsync-resolver-syncconfig)`属性。

## 示例
<a name="sam-property-graphqlapi-resolver-examples"></a>

### 使用 AWS SAM 生成的解析器函数代码并将字段另存为变量
<a name="sam-property-graphqlapi-resolver-examples-example1"></a>

以下是我们的示例使用的 GraphQL 架构：

```
schema {
  query: Query
  mutation: Mutation
}

type Query {
  getPost(id: ID!): Post
}

type Mutation {
  addPost(author: String!, title: String!, content: String!): Post!
}

type Post {
  id: ID!
  author: String
  title: String
  content: String
}
```

以下是我们 AWS SAM 模板的片段：

```
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
...
Resources:
  MyGraphQLApi:
    Type: AWS::Serverless::GraphQLApi
    Properties:
      ...
      Functions:
        preprocessPostItem:
          ...
        createPostItem:
          ...
      Resolvers:
        Mutation:
          addPost:
            Runtime:
              Name: APPSYNC_JS
              Version: 1.0.0
            Pipeline:
            - preprocessPostItem
            - createPostItem
```

在我们的 AWS SAM 模板中，我们没有指定`CodeUri`或`InlineCode`。部署时， AWS SAM 会自动为我们的解析器生成以下内联代码：

```
export function request(ctx) {
  return {};
}

export function response(ctx) {
  return ctx.prev.result;
}
```

这个默认的解析程序代码会将请求重定向到第一个管道函数，并接收来自最后一个管道函数的响应。

在第一个管道函数中，可以使用提供的 `args` 字段来解析请求对象并创建变量。然后就可以在函数中使用这些变量。以下是 `preprocessPostItem` 函数的示例：

```
import { util } from "@aws-appsync/utils";

export function request(ctx) {
  const author = ctx.args.author;
  const title = ctx.args.title;
  const content = ctx.args.content;
  
  // Use variables to process data
  
}

export function response(ctx) {
  return ctx.result;
}
```

# 运行时
<a name="sam-property-graphqlapi-resolver-runtime"></a>

管道解析程序或函数的运行时间。指定要使用的名称和版本。

## 语法
<a name="sam-property-graphqlapi-resolver-runtime-syntax"></a>

要在 AWS Serverless Application Model (AWS SAM) 模板中声明此实体，请使用以下语法。

### YAML
<a name="sam-property-graphqlapi-resolver-runtime-syntax-yaml"></a>

```
Name: String
Version: String
```

## Properties
<a name="sam-property-graphqlapi-resolver-runtime-properties"></a>

`Name`  <a name="sam-graphqlapi-resolver-runtime-name"></a>
要使用的运行时系统的名称。当前，允许的唯一值为 `APPSYNC_JS`。  
*类型*：字符串  
*是否必需*：是  
*CloudFormation 兼容性*：此属性直接传递给`AWS::AppSync::Resolver AppSyncRuntime`对象的`[Name](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-appsync-resolver-appsyncruntime.html#cfn-appsync-resolver-appsyncruntime-name)`属性。

`Version`  <a name="sam-graphqlapi-resolver-runtime-version"></a>
要使用的运行时系统的版本。当前允许的唯一版本为 `1.0.0`。  
*类型*：字符串  
*是否必需*：是  
*CloudFormation 兼容性*：此属性直接传递给`AWS::AppSync::Resolver AppSyncRuntime`对象的`[RuntimeVersion](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-appsync-resolver-appsyncruntime.html#cfn-appsync-resolver-appsyncruntime-runtimeversion)`属性。