

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

# Esempio di risolutore di pipeline con Amazon DynamoDB
<a name="writing-code"></a>

Supponiamo di voler collegare un resolver di pipeline a un campo denominato `getPost(id:ID!)` che restituisce un `Post` tipo da un'origine dati Amazon DynamoDB con la seguente query GraphQL:

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

Innanzitutto, collega un semplice resolver a con il codice seguente. `Query.getPost` Questo è un esempio di codice resolver semplice. Non esiste una logica definita nel gestore della richiesta e il gestore della risposta restituisce semplicemente il risultato dell'ultima funzione.

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

Quindi, definisci la funzione `GET_ITEM` che recupera un postitem dalla tua fonte di dati:

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

Se si verifica un errore durante la richiesta, il gestore di risposte della funzione aggiunge un errore che verrà restituito al client chiamante nella risposta GraphQL. Aggiungi la `GET_ITEM` funzione all'elenco delle funzioni del resolver. Quando si esegue la query, il gestore delle richieste della `GET_ITEM` funzione utilizza gli strumenti forniti dal modulo AWS AppSync DynamoDB per creare una `DynamoDBGetItem` richiesta utilizzando come chiave. `id` `ddb.get({ key: { id } })`genera l'operazione appropriata: `GetItem`

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

AWS AppSync utilizza la richiesta per recuperare i dati da Amazon DynamoDB. Una volta restituiti, i dati vengono gestiti dal gestore delle risposte della `GET_ITEM` funzione, che verifica la presenza di errori e quindi restituisce il risultato. 

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

Infine, il gestore di risposte del resolver restituisce direttamente il risultato.

## Lavorare con gli errori
<a name="working-with-errors"></a>

Se si verifica un errore nella funzione durante una richiesta, l'errore verrà reso disponibile nel gestore della risposta alla funzione in`ctx.error`. È possibile aggiungere l'errore alla risposta GraphQL utilizzando l'`util.appendError`utilità. È possibile rendere l'errore disponibile per altre funzioni della pipeline utilizzando lo stash. Vedere l'esempio di seguito.

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