AppSync Resolvers

Wrapper for handling AWS AppSync resolvers with AWS Lambda in Go. See appsync-resolvers-example for an example project and how to set up a complete GraphQL API using the Serverless Application Model.
Usage
Installation
$ > go get github.com/sbstjn/appsync-resolvers
Routing
import (
"github.com/sbstjn/appsync-resolvers"
)
func resolverA() (interface{}, error) {
return nil, fmt.Errorf("Nothing here in resolver A: %s", args.Foo)
}
func resolverB(args struct {
Bar string `json:"bar"`
}) (interface{}, error) {
return nil, fmt.Errorf("Nothing here in resolver B: %s", args.Bar)
}
var (
r = resolvers.New()
)
func init() {
r.Add("fieldA", resolverA)
r.Add("fieldB", resolverB)
}
func main() {
lambda.Start(r.Handle)
}
AppSync Configuration
Resolver lookup is based on a field property in your RequestMappingTemplate, which can be configured using the AWS Console or CloudFormation as well. This approach works fine with the recommended AWS setup for multiple custom resolvers and AWS Lambda data sources:
AppSyncDataSource:
Type: AWS::AppSync::DataSource
Properties:
ApiId: !GetAtt [ AppSyncAPI, ApiId ]
Name: resolver
Type: AWS_LAMBDA
LambdaConfig:
LambdaFunctionArn: !GetAtt [ Lambda, Arn ]
ServiceRoleArn: !GetAtt [ Role, Arn ]
AppSyncResolverA:
Type: AWS::AppSync::Resolver
Properties:
ApiId: !GetAtt [ AppSyncAPI, ApiId ]
TypeName: Query
FieldName: fieldA
DataSourceName: !GetAtt [ AppSyncDataSource, Name ]
RequestMappingTemplate: '{ "version" : "2017-02-28", "operation": "Invoke", "payload": { "field": "fieldA", "arguments": $utils.toJson($context.arguments) } }'
ResponseMappingTemplate: $util.toJson($context.result)
AppSyncResolverB:
Type: AWS::AppSync::Resolver
Properties:
ApiId: !GetAtt [ AppSyncAPI, ApiId ]
TypeName: Query
FieldName: fieldB
DataSourceName: !GetAtt [ AppSyncDataSource, Name ]
RequestMappingTemplate: '{ "version" : "2017-02-28", "operation": "Invoke", "payload": { "field": "fieldB", "arguments": $utils.toJson($context.arguments) } }'
ResponseMappingTemplate: $util.toJson($context.result)
See appsync-resolvers-example for an example project and how to set up a serverless GraphQL API with AWS AppSync using the Serverless Application Model.
License
Feel free to use the code, it's released using the MIT license.
Contribution
You are welcome to contribute to this project! 😘
To make sure you have a pleasant experience, please read the code of conduct. It outlines core values and beliefs and will make working together a happier experience.