

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 解析程式
<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)` 屬性。