

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# Contoh pipeline resolver dengan Amazon DynamoDB
<a name="writing-code"></a>

Misalkan Anda ingin melampirkan resolver pipeline pada bidang bernama `getPost(id:ID!)` yang mengembalikan `Post` tipe dari sumber data Amazon DynamoDB dengan kueri GraphQL berikut:

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

Pertama, lampirkan resolver sederhana `Query.getPost` dengan kode di bawah ini. Ini adalah contoh kode resolver sederhana. Tidak ada logika yang didefinisikan dalam penangan permintaan, dan penangan respons hanya mengembalikan hasil dari fungsi terakhir.

```
/**
 * 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
}
```

Selanjutnya, tentukan fungsi `GET_ITEM` yang mengambil postitem dari sumber data Anda:

```
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
}
```

Jika ada kesalahan selama permintaan, penangan respons fungsi menambahkan kesalahan yang akan dikembalikan ke klien pemanggil dalam respons GraphQL. Tambahkan `GET_ITEM` fungsi ke daftar fungsi resolver Anda. Saat Anda menjalankan kueri, penangan permintaan `GET_ITEM` fungsi menggunakan utilitas yang disediakan oleh modul AWS AppSync DynamoDB untuk membuat `DynamoDBGetItem` permintaan menggunakan sebagai kunci. `id` `ddb.get({ key: { id } })`menghasilkan `GetItem` operasi yang sesuai:

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

AWS AppSync menggunakan permintaan untuk mengambil data dari Amazon DynamoDB. Setelah data dikembalikan, itu ditangani oleh penangan respons `GET_ITEM` fungsi, yang memeriksa kesalahan dan kemudian mengembalikan hasilnya. 

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

Akhirnya, handler respon resolver mengembalikan hasilnya secara langsung.

## Bekerja dengan kesalahan
<a name="working-with-errors"></a>

Jika terjadi kesalahan dalam fungsi Anda selama permintaan, kesalahan akan tersedia di pengendali respons fungsi Anda di`ctx.error`. Anda dapat menambahkan kesalahan ke respons GraphQL Anda menggunakan utilitas. `util.appendError` Anda dapat membuat kesalahan tersedia untuk fungsi lain dalam pipeline dengan menggunakan simpanan. Lihat contoh di bawah ini:

```
/**
 * 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;
}
```