How to Make a Serverless GraphQL API using Lambda and DynamoDB
Tags: serverless, graphql, aws, dynamodb
Published: 2022-04-11 15:55:04

Originally posted at [Serverless](https://www.serverless.com/blog/make-serverless-graphql-api-using-lambda-dynamodb/)

![](https://cdn-images-1.medium.com/max/2400/0*a44mEsKcLwWOl4Kp.jpeg)

The graphql module makes it easy to rapidly create a GraphQL service that validates queries. We use GraphQL at [Serverless.com](https://serverless.com) to query our backend services, and we love how well it fits into the serverless paradigm.

Interested in building your own GraphQL API? Awesome. Here we go.

### Building the API

In this example, I’ll be targeting AWS. Let’s build a simplistic version of an API that might be used by the front-end to retrieve a dynamic message to display in the UI; in this case, greeting the user by name.

Start by initializing a project and installing the [graphql](https://www.npmjs.com/package/graphql) module:
{% gist https://gist.github.com/richardgrantserverless/b6f05c904367990f7a579a08c11a5151.js %}
Now we can use it in handler.js, where we declare a schema and then use it to serve query requests:
{% gist https://gist.github.com/richardgrantserverless/72ec987ba451d3a61889d6c77e66ede3.js %}
Pretty simple! To deploy it, define a service in serverless.yml, and set the handler to service HTTP requests:
{% gist https://gist.github.com/richardgrantserverless/739ff220867b5cabae06d2cf2fde8b6a.js %}
Now we can bring it to life:
{% gist https://gist.github.com/richardgrantserverless/26fbc627a8feb6614b5f9ce9924d218a.js %}
### Creating the database

In the real world, virtually any service that does something valuable has a data store behind it. Suppose users have nicknames that should appear in the greeting message; we need a database to store those nicknames, and we can expand our GraphQL API to update them.

Let’s start by adding a database to the resource definitions in serverless.yml. We need a table keyed on the user's first name, which we define using CloudFormation, as well as some provider configuration to allow our function to access it:
{% gist https://gist.github.com/richardgrantserverless/6aa71173e1ba0d48af65c3e31d88246d.js %}
To use it, we’ll need the [aws-sdk](https://www.npmjs.com/package/aws-sdk). Here’s how you’d use the SDK’s vanilla DocumentClient to access DynamoDB records:
{% gist https://gist.github.com/richardgrantserverless/a1fe2f85ed78a2829ad4bb54ccd28552.js %}
Include these in our handler, and then we can get to work:
{% gist https://gist.github.com/richardgrantserverless/062ef8bde65a90c3a0f984d5ecd4005b.js %}
We started by defining a method that returned a simple string value for the greeting message. However, the GraphQL library can also use Promises as resolvers.

Since the DocumentClient uses a callback pattern, we’ll wrap these in promises and use the DynamoDB get method to check the database for a nickname for the user:
{% gist https://gist.github.com/richardgrantserverless/86e97df44b7abbe7d4be514a69bd5c6c.js %}
You can see here that we added a method changeNickname, but the GraphQL API is not yet using it. We need to declare a mutation that the front-end can use to perform updates.

We previously only added a query declaration to the schema; now we need a mutation as well:
{% gist https://gist.github.com/richardgrantserverless/34816a4a2922e17a582283fd5d0cd44d.js %}
After these changes, we can make the greeting request again and receive the same result as before:
{% gist https://gist.github.com/richardgrantserverless/741e173d0a65c94f6c0c1b48c16462a2.js %}
But if I want the API to call me “Jer”, I can update the nickname for “Jeremy”:
{% gist https://gist.github.com/richardgrantserverless/a0da67f97b7fa781048469ede248c8d2.js %}
The API will now call anyone named “Jeremy” by the nickname “Jer”.

Separation of concerns like this let you build front-ends and services that offload logic into backends. Those backends can then encapsulate data access and processing behind a strongly-typed, validating, uniform contract that comes with rich versioning and deprecation strategies.

### Deploy your own!

To deploy this service yourself, download the [source code](https://github.com/serverless/examples/tree/master/aws-node-graphql-api-with-dynamodb) and deploy it with the [Serverless Framework](https://serverless.com/framework/). Or, take a look at [a larger example project](https://github.com/boazdejong/serverless-graphql-api) for ideas on project structure and factoring.

### Architectural Diagram

![](https://cdn-images-1.medium.com/max/2000/0*WOfLaC0qYs20O8VM.jpg)

Happy building!

*Originally published at [https://www.serverless.com](https://www.serverless.com/blog/make-serverless-graphql-api-using-lambda-dynamodb/).*