DEV Community

Cover image for How to Make a Serverless GraphQL API using Lambda and DynamoDB
We're Serverless! for Serverless Inc.

Posted on • Originally published at serverless.com

How to Make a Serverless GraphQL API using Lambda and DynamoDB

Originally posted at Serverless

The graphql module makes it easy to rapidly create a GraphQL service that validates queries. We use GraphQL at 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 module:


Now we can use it in handler.js, where we declare a schema and then use it to serve query requests:

Pretty simple! To deploy it, define a service in serverless.yml, and set the handler to service HTTP requests:

Now we can bring it to life:

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:


To use it, we’ll need the aws-sdk. Here’s how you’d use the SDK’s vanilla DocumentClient to access DynamoDB records:

Include these in our handler, and then we can get to work:

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:


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:


After these changes, we can make the greeting request again and receive the same result as before:

But if I want the API to call me “Jer”, I can update the nickname for “Jeremy”:

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 and deploy it with the Serverless Framework. Or, take a look at a larger example project for ideas on project structure and factoring.

Architectural Diagram

Happy building!

Originally published at https://www.serverless.com.

Top comments (0)