Setting a GraphQL endpoint using AWS Lambda functions

Setting a GraphQL endpoint using AWS Lambda functions
Guillermo Osores
November 2, 2020
GraphQL
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.

Contact Us

Ready to get started? Use the form or give us a call to meet our team and discuss your project and business goals.
We can’t wait to meet you!

Write to us!
info@vairix.com

OR
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Setting a GraphQL endpoint using AWS Lambda functions