Inspired by Kevin Old's project, this is a sample implementation a GraphQL service on AWS Lambda using Claudia. The service offers CRUD operations via just 1 endpoint. The data persistence is done via DynamoDB, similar to the dynamodb-example. Additionally, this sample project is written in ES2015 and is transpiled by Babel before uploading to AWS.
Create a table in DynamoDB, with a string
primary key called userid
. You can do that from the DynamoDB web console, or using the AWS CLI command line. Here is an example command that will create the table with the minimal provisioned throughput:
aws dynamodb create-table --table-name claudia-graphql-example \
--attribute-definitions AttributeName=userid,AttributeType=S \
--key-schema AttributeName=userid,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \
--query TableDescription.TableArn --output text
This example project includes an IAM access policy that will grant the lambda function access to all your DynamoDB tables, to make it easier to get started. If you wish, you can edit the policies/access-dynamodb.json file and restrict the access to your new table only.
To set this up, first set up the credentials, then:
- run
npm install
to grab the dependencies - run
npm run create
to create the lambda project under the default name on AWS. - run
./test/run.sh
to execute cURL scripts to test the CRUD operations against the lambda endpoint.
For subsequent updates, use the npm run deploy
command.
With GraphQL, there is only 1 endpoint /latest/graphql
for all CRUD operations. User construct a GraphQL query string based on the schema definition defined in src/schema.js
, and post the string to the endpoint.
Post application/json
that looks like this:
{
"query": "mutation {
addUser (userid:\"2\", name:\"John Doe\", age:29) {
userid
name
age
}
}"
}
Post application/json
that looks like this:
{
"query": "{
user (userid:\"4\") {
userid
name
age
}
}"
}
Post application/json
that looks like this:
{
"query": "mutation {
deleteUser (userid:\"4\") {
userid
name
age
}
}"
}
Run ./test/run.sh
to launch the cURL scripts that perform the various operations.
GraphiQL is an IDE that help user edit and test queries and discover the schema. You can download a GraphiQL app at https://github.com/skevy/graphiql-app
The implementation in this example is written in ES2015 under the src
folder. It is transpiled to ES5 during npm create
and npm deploy
via Babel. See the npm run tranpile
target at package.json.
babel --presets es2015 --plugins add-module-exports src --out-dir .