기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
다음 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
함수의 요청 핸들러는 AWS AppSync의 DynamoDB 모듈에서 제공하는 유틸리티를 사용하여를 키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>"
}
}
마지막으로, 해석기의 응답 핸들러는 결과를 직접 반환합니다.
오류 작업
요청 중에 함수에서 오류가 발생하면 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;
}