S3 AppSync Resolver
This article will walk through adding an AppSync Resolver to fetch objects from S3
Requirements
- General knowledge of GraphQL
- General knowledge of AWS AppSync and Apache Velocity
S3 Data Source
Since S3 is not available as a Data Source out of the box, we have to specify it as an HTTP Data Source
The easiest way to do this is with cloudformation. You can view a cdk implementation of this here.
To add S3 as a Data Source manually, use the following values:
- Data source name: <something>
- Data source type: HTTP Endpoint
- HTTP endpoint: <bucket_domain>
Your bucket domain is going to look something like the following:
https://<yourbucketname>.s3.<region>.amazonaws.com
Request / Response Templates
Response Template
This template returns the response from GetObject.
Error Handling
The response template includes basic error handling. If you need more fine-grained control, you can read about the errors that S3 returns here.
Sample Query
Finally if we wanted to test our resolver, you can use this example query:
If this object existed it would return the contents. In our case since the object doesn't exist, we get a response like this:
Data source CDK Construct
This snippet contains a CDK Construct that implements the `appsync.BackedDataSource` class. You can use this with CDK to specify S3 as a data source.
You can see this in the Sample Stack
Sample CDK Stack
This is a sample stack that creates a new graphql api, specifies S3 as a data source, and creates resolvers with the templates that we just walked through.
To use this you will just need to specify a `schema.graphql` and download the `request.vtl` and `response.vtl` templates into a place that the stack can find.
Warning: This stack may create resources that will cost you money