This is an opinionated starter template for developing AWS AppSync APIs using the AWS CDK.
This CDK project deploys the following resources:
- A public AppSync API that is authenticated via API Key
- A private AppSync API that is authenticated via Cognito User Pools
- Example DynamoDB tables to demonstrate how to create resolvers that access them
The cdk.json
file tells the CDK Toolkit how to execute your app.
- Install Docker or Docker Desktop to be able to run
npm run deploy
orcdk synth
- Make sure you have a Java runtime installed to be able to run DynamoDB locally from the tests using
@shelf/jest-dynamodb
.
- Run
npm install
- If you want to run the unit tests, run
cd test && npm install
- Run tests with
npm run test
- Deploy with
npm run deploy
(assuming you've configured the CDK and rancdk bootstrap
)
bin
andlib
- AWS CDK folders used to deploy your cloud infrastructuregraphql
- defines your AppSync API GraphQL Schemasrc
- defines any Lambda function code needed to support your resolverstest
- tests your AppSync API usingamplify-appsync-simulator
utils
- utils for making testing the deployed APIs easier.
- Add new GraphQL types to the corresponding directory in
/graphql
- Add new resolver and datasource config to
lib/appsync-api-stack.ts
- Add new Lambda function definitions (if needed) to
src
- Add tests for the new resolvers in
test
- Commit your changes to automatically run
prettify
on all committed files
npm run build
compile typescript to jsnpm run watch
watch for changes and compilenpm run test
perform the jest unit testsnpm run deploy
deploy this stack to your default AWS account/regioncdk diff
compare deployed stack with current statecdk synth
emits the synthesized CloudFormation templatenpm run gql:codegen
creates Typescript types for all of your Typescript Lambda Functions to use using the@graphql-codegen/cli
packagenpm run gql:compile
compiles all of your GraphQL schema defined in/graphql
into your public and private API schemas
This project utilizes amplify-appsync-simulator
to simulate your AppSync APIs to test against locally using jest
and supertest
to run the tests.
There is currently an issue with amplify-appsync-simulator
that prevents you from installing it alongside other projects that depend on graphql
, issue created here: aws-amplify/amplify-category-api#692
As a workaround, the test
folder has its own npm project, however the only dependency is aws-appsync-simulator
. You should keep it this way unless you run into similar issues with other dependencies.
- There are a few GraphQL types that do not have resolvers implemented for them, they exist to show that you can deploy both public and private schemas that share certain elements but each have their own unique additions.
- The GraphQL queries in this project have tests defined, but the mutations do not. Will be added at some point in the near future though.
- If you do not need a public and private API, you are free to delete all references to the one you don't need and base your template off of that.
- There is no CI/CD configuration (yet). The project is set up to assume you are using the AWS CDK to deploy from your local environment.
- The
amplify-appsync-simulator
library used to simulate appsync is noticeably slow. From testing on a WSL2 Ubuntu distribution the tests can take anywhere from 90 to 120 seconds to complete, you may have better luck on more powerful systems.
Feel free to submit PRs that either implement resolvers that have not been completed, or make the development process easier in some way. One thing that would be nice is a way to generate the test resolver config based on what has already been defined in the CDK stack, however this is not a trivial task since the primary way to expose CDK resources outside of the stack is to synthesize a cloudformation stack and export values from that.