Resolver
Configure resolvers for the fields of your GraphQL API. Amazon Serverless Application Model (Amazon SAM) supports JavaScript pipeline resolvers.
Syntax
To declare this entity in your Amazon Serverless Application Model (Amazon SAM) template, use the following syntax.
YAML
OperationType:LogicalId: Caching:CachingConfigCodeUri:StringFieldName:StringInlineCode:StringMaxBatchSize:IntegerPipeline:ListRuntime:RuntimeSync:SyncConfig
Properties
Caching-
The caching configuration for the resolver that has caching activated.
Type: CachingConfig
Required: No
Amazon CloudFormation compatibility: This property is passed directly to the
CachingConfigproperty of anAWS::AppSync::Resolverresource. CodeUri-
The resolver function code’s Amazon Simple Storage Service (Amazon S3) URI or path to a local folder.
If you specify a path to a local folder, Amazon CloudFormation requires that the file is first uploaded to Amazon S3 before deployment. You can use the Amazon SAM CLI to facilitate this process. For more information, see How Amazon SAM uploads local files at deployment.
If neither
CodeUriorInlineCodeare provided, Amazon SAM will generateInlineCodethat redirects the request to the first pipeline function and receives the response from the last pipeline function.Type: String
Required: No
Amazon CloudFormation compatibility: This property is passed directly to the
CodeS3Locationproperty of anAWS::AppSync::Resolverresource. FieldName-
The name of your resolver. Specify this property to override the
LogicalIdvalue.Type: String
Required: No
Amazon CloudFormation compatibility: This property is passed directly to the
FieldNameproperty of anAWS::AppSync::Resolverresource. InlineCode-
The resolver code that contains the request and response functions.
If neither
CodeUriorInlineCodeare provided, Amazon SAM will generateInlineCodethat redirects the request to the first pipeline function and receives the response from the last pipeline function.Type: String
Required: No
Amazon CloudFormation compatibility: This property is passed directly to the
Codeproperty of anAWS::AppSync::Resolverresource. LogicalId-
The unique name for your resolver. In a GraphQL schema, your resolver name should match the field name that its used for. Use that same field name for
LogicalId.Type: String
Required: Yes
Amazon CloudFormation compatibility: This property is unique to Amazon SAM and doesn’t have an Amazon CloudFormation equivalent.
MaxBatchSize-
The maximum number of resolver request inputs that will be sent to a single Amazon Lambda function in a
BatchInvokeoperation.Type: Integer
Required: No
Amazon CloudFormation compatibility: This property is passed directly to the
MaxBatchSizeproperty of anAWS::AppSync::Resolverresource. OperationType-
The GraphQL operation type that is associated with your resolver. For example,
Query,Mutation, orSubscription. You can nest multiple resolvers byLogicalIdwithin a singleOperationType.Type: String
Required: Yes
Amazon CloudFormation compatibility: This property is passed directly to the
TypeNameproperty of anAWS::AppSync::Resolverresource. Pipeline-
Functions linked with the pipeline resolver. Specify functions by logical ID in a list.
Type: List
Required: Yes
Amazon CloudFormation compatibility: This property is unique to Amazon SAM and doesn't have an Amazon CloudFormation equivalent. It is similar to the
PipelineConfigproperty of anAWS::AppSync::Resolverresource. Runtime-
The runtime of your pipeline resolver or function. Specifies the name and version to use.
Type: Runtime
Required: Yes
Amazon CloudFormation compatibility: This property is unique to Amazon SAM and doesn't have an Amazon CloudFormation equivalent. It is similar to the
Runtimeproperty of anAWS::AppSync::Resolverresource. Sync-
Describes a Sync configuration for a resolver.
Specifies which Conflict Detection strategy and Resolution strategy to use when the resolver is invoked.
Type: SyncConfig
Required: No
Amazon CloudFormation compatibility: This property is passed directly to the
SyncConfigproperty of anAWS::AppSync::Resolverresource.
Examples
Use the Amazon SAM generated resolver function code and save fields as variables
Here is the GraphQL schema for our example:
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 }
Here is a snippet of our Amazon SAM template:
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
In our Amazon SAM template, we don’t specify CodeUri or InlineCode.
At deployment, Amazon SAM automatically generates the following inline code for our
resolver:
export function request(ctx) { return {}; } export function response(ctx) { return ctx.prev.result; }
This default resolver code redirects the request to the first pipeline function and receives the response from the last pipeline function.
In our first pipeline function, we can use the provided args field to parse
the request object and create our variables. We can then use these variables within our
function. Here is an example of our preprocessPostItem function:
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; }