Resolver
Configure resolvers for the fields of your GraphQL API. AWS Serverless Application Model (AWS SAM) supports JavaScript pipeline resolvers.
Syntax
To declare this entity in your AWS Serverless Application Model (AWS 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
AWS 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, AWS CloudFormation requires that the file is first uploaded to Amazon S3 before deployment. You can use the AWS SAM CLI to facilitate this process. For more information, see How AWS SAM uploads local files at deployment.
If neither
CodeUriorInlineCodeare provided, AWS SAM will generateInlineCodethat redirects the request to the first pipeline function and receives the response from the last pipeline function.Type: String
Required: No
AWS 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
AWS 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, AWS SAM will generateInlineCodethat redirects the request to the first pipeline function and receives the response from the last pipeline function.Type: String
Required: No
AWS 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
AWS CloudFormation compatibility: This property is unique to AWS SAM and doesn’t have an AWS CloudFormation equivalent.
MaxBatchSize-
The maximum number of resolver request inputs that will be sent to a single AWS Lambda function in a
BatchInvokeoperation.Type: Integer
Required: No
AWS 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
AWS 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
AWS CloudFormation compatibility: This property is unique to AWS SAM and doesn't have an AWS 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
AWS CloudFormation compatibility: This property is unique to AWS SAM and doesn't have an AWS 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
AWS CloudFormation compatibility: This property is passed directly to the
SyncConfigproperty of anAWS::AppSync::Resolverresource.
Examples
Use the AWS 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 AWS 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 AWS SAM template, we don’t specify CodeUri or InlineCode.
At deployment, AWS 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; }