GraphQL APIs With AWS AppSync Part Two
GraphQL APIs With AWS AppSync Part Two
medium.com/@cbartling/graphql-apis-with-aws-appsync-part-two-c7bc432f3cc6
March 27,
2019
Introduction
This is the second of a series of posts describing my experimentation with AWS AppSync,
Lambda, and DynamoDB. A public GitHub repository exists with this experiment so
others can learn from my efforts. For this post, I will be describing the code that resides
on the v1 tag, so feel free to checkout to this point in time: git checkout v1
GraphQL schema
AWS AppSync API
AWS Lambda resolver (implemented in Python 3.7)
Various AWS resources to map the AppSync GraphQL API to the Lambda resolver
1/5
Unit and integration tests for the Python handler and supporting code (the testing
will be thoroughly described in the third post of this series)
Instead, create an individual IAM user with administrative permissions for the work you
will be performing on AWS. The AWS CLI allows you to maintain a named profile for each
IAM user. The named profiles usage assumes you have AWS CLI installed and configured.
Go here to install and configure the AWS CLI. Once you have configured and signed in to
AWS, perform the following tasks:
1. From the AWS Management Console, create an InvestGuruAdmin IAM user with
the AdministratorAccess Permission Policy. Copy the Access Key ID and Secret Access
Key for the next step.
2. Create the InvestGuruAdmin named profile in your AWS credentials file
( ~/.aws/credentials ) with the associated Access Key ID and Secret Access Key.
2/5
Using CloudFormation YAML
AWS CloudFormation supports both JSON and YAML formats. I chose YAML as it works
well with the Serverless Framework’s own serverless.yml YAML-based configuration file.
Serverless variables
I also use some Serverless Framework variables in the serverless.yml file. A full
description of those can be found here. Again, super helpful for making the
configuration modular. I especially like having the GraphQL schema in its own file
( schema.graphql ) and being able to edit it with syntax highlighting.
serverless.yml
The first forty-four lines of the serverless.yml file contain standard Serverless
Framework YAML configuration, which is annotated below. Everything from line 48 on is
AWS-specific CloudFormation YAML.
Our fledgling schema starts with a couple of custom types and a single query:
Not much to look at, as most of the heavy lifting is delegated to the
simple_moving_average function. We make two calls to this simple_moving_average
function, retrieving the 50-day and 300-day simple moving averages for a stock. Finally a
result dictionary is built and returned to the event source (in this case, AWS AppSync). A
couple of things to note in this result dictionary:
3/5
1. The structure of the dictionary matches the hierarchical/graph structure from the
GraphQL Schema. The SimpleMovingAverageAnalytic type contains a single
Company type and two lists, keyed by averages50 and averages300 . Each
element in the lists is of type MovingAverageObservation .
2. Do not try to use standard Python snake_case with these keys in the result. The
keys should remain in camelCase to match what GraphQL is going to map to.
AppSync will puke if you don’t exactly match the keys specified in the GraphQL
Schema.
When completed with the experiment, use yarn undeploy-dev to remove the experiment
from the dev stage and from AWS.
HTTP headers
Use the AppSync URL from the deployment step as the endpoint. The screenshot below
demonstrates our simpleMovingAverageAnalytic GraphQL query.
4/5
Next steps
I’ll stop here so you have a chance to ingest this first iteration of our AppSync-hosted
GraphQL API. The next post will cover using pytest to drive our test-driven development
cadence.
5/5