Setting a GraphQL endpoint using AWS Lambda functions

 Reynaldo Rodríguez
Reynaldo Rodríguez
November 2, 2020
GraphQL
AWS
Lambda
Setting a GraphQL endpoint using AWS Lambda functions

In this article we will see how easy it is to set a GraphQL endpoint using AWS Lambda functions. We are going to assume you already know about GraphQL (here is a link so that you can learn it if you haven’t yet https://graphql.org/). First we need to Install the serverless framework from NPM. This will help to not only upload our solution to AWS but also to test it locally.

You need an account to use serverless, so if you are not logged in, run this script to login or create a new account:

Serverless needs to be connected to your AWS console, so running this script will guide you through the process of setting it up:


In the example we are going to mock our data and we are only going to use these two NPM libs: apollo-server-lambda and graphql. The first one is responsible for the GraphQL integration on AWS Lambda.


Create serverless.yml file to configure your serveless instance:


Handler property is the route to the server file plus your exported handler. This is our server file /src/server.js:

We set the server so that if we want to add a query or mutation, we only need to create a resolver and add it to the schema. So inside src/types we have two types of files, one exposing the type itself and the other that gathers all types and squashes them into one exported object: /src/types/index.js (the squasher)

User type in src/types/user.js


User type in src/types/query.js


We did the same with our resolvers: /src/queries/index.js (the squasher)

Ping resolver /src/queries/ping.js:

getUserInfo resolver /src/queries/getUserInfo.js:

As you can see, we mocked two users, but the getUserInfo function should have whatever you need to resolve the query, like querying the database, consuming an API, etc. Serverless lib has an option to test your functions locally so you don't have to deploy to AWS to test the changes. So to test getUserInfo we can create a JSON file containing the requested information. localTests/getUserInfo.json

Then we can use it like this:

-f is the function defined on our serverless.yml and -p is the file mentioned above. To upload this to AWS just type:


If all goes correctly, it will tell you the endpoint that was created. Now we can paste it on our preferred browser to get this interface to play around.

Solving Real-Time Data Challenges with AWS Kinesis

Explore a journey from traditional batch processing to real-time data handling using AWS Kinesis. Learn how to scale efficiently, process data instantly, and overcome challenges without needing deep data science expertise.

October 9, 2024
Read more ->
AWS

Serverless GraphQL API with Hasura and AWS stack

As a follow-up to our serverless REST API post, this article provides a thorough step-by-step guide on how to creating a GraphQL API boilerplate based on Hasura and the AWS stack.

May 5, 2020
Read more ->
Serverless
API
GraphQL
AWS
AWS Benefits

Get in Touch

Let's Discuss Your IT Augmentation Needs

Have questions or are interested in our IT Staff Augmentation services? We'd love to hear from you. Reach out to our team using the contact information below, and we'll be in touch shortly to discuss how we can support your projects.

Find Us!

One Beacon St, 15th Floor, Boston, MA 02108

What do you need help with?
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
"They're very collaborative, and they offer great benefits to us. The interaction is very important to us, and they take time to explain their process. They excel in all aspects of what we do, and I would recommend them to anybody."
Jonathan Wride
CEO at