

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Amazon DynamoDB を使用したパイプラインリゾルバーの例
<a name="writing-code"></a>

次の GraphQL クエリを使用して、Amazon DynamoDB データソースから `Post` 型を返す `getPost(id:ID!)` という名前のフィールドにパイプラインリゾルバーをアタッチするとします。

```
getPost(id:1){
    id
    title
    content
}
```

まず、以下のコードで簡単なリゾルバーを `Query.getPost` にアタッチします。これは単純なリゾルバーコードの例です。リクエストハンドラーにはロジックは定義されておらず、レスポンスハンドラーは最後の関数の結果を返すだけです。

```
/**
 * Invoked **before** the request handler of the first AppSync function in the pipeline.
 * The resolver `request` handler allows to perform some preparation logic
 * before executing the defined functions in your pipeline.
 * @param ctx the context object holds contextual information about the function invocation.
 */
export function request(ctx) {
  return {}
}

/**
 * Invoked **after** the response handler of the last AppSync function in the pipeline.
 * The resolver `response` handler allows to perform some final evaluation logic
 * from the output of the last function to the expected GraphQL field type.
 * @param ctx the context object holds contextual information about the function invocation.
 */
export function response(ctx) {
  return ctx.prev.result
}
```

次に、データソースから投稿項目を取得する関数 `GET_ITEM` を定義します。

```
import { util } from '@aws-appsync/utils'
import * as ddb from '@aws-appsync/utils/dynamodb'

/**
 * Request a single item from the attached DynamoDB table datasource
 * @param ctx the context object holds contextual information about the function invocation.
 */
export function request(ctx) {
	const { id } = ctx.args
	return ddb.get({ key: { id } })
}

/**
 * Returns the result
 * @param ctx the context object holds contextual information about the function invocation.
 */
export function response(ctx) {
	const { error, result } = ctx
	if (error) {
		return util.appendError(error.message, error.type, result)
	}
	return ctx.result
}
```

リクエスト中にエラーが発生した場合、関数のレスポンスハンドラーは、呼び出し元のクライアントに返されるエラーを GraphQL レスポンスに追加します。`GET_ITEM` 関数をリゾルバー関数リストに追加します。クエリを実行すると、`GET_ITEM`関数のリクエストハンドラーは DynamoDB モジュールが提供する utils AWS AppSyncを使用して、 をキー`id`として使用して`DynamoDBGetItem`リクエストを作成します。 は適切な`GetItem`オペレーション`ddb.get({ key: { id } })`を生成します。

```
{
    "operation" : "GetItem",
    "key" : {
        "id" : { "S" : "1" }
    }
}
```

AWS AppSync は リクエストを使用して Amazon DynamoDB からデータを取得します。データが返されると、`GET_ITEM` 関数のレスポンスハンドラーによって処理されます。レスポンスハンドラーはエラーをチェックして結果を返します。

```
{
  "result" : {
    "id": 1,
    "title": "hello world",
    "content": "<long story>"
  }
}
```

最後に、リゾルバーのレスポンスハンドラーは結果を直接返します。

## エラーの使用
<a name="working-with-errors"></a>

リクエスト中に関数でエラーが発生した場合、そのエラーは `ctx.error` の関数レスポンスハンドラーで利用できるようになります。`util.appendError` ユーティリティを使用して GraphQL レスポンスにエラーを追加できます。stash を使用すると、パイプライン内の他の関数がエラーを利用できるようになります。次の例を参照してください。

```
/**
 * Returns the result
 * @param ctx the context object holds contextual information about the function invocation.
 */
export function response(ctx) {
  const { error, result } = ctx;
  if (error) {
    if (!ctx.stash.errors) ctx.stash.errors = []
    ctx.stash.errors.push(ctx.error)
    return util.appendError(error.message, error.type, result);
  }
  return ctx.result;
}
```