Setting a GraphQL endpoint using AWS Lambda functions
Reynaldo Rodríguez
November 2, 2020
GraphQL
AWS
Lambda
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
You need an account to use serverless, so if you are not logged in, run this script to login or create a new account:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Serverless needs to be connected to your AWS console, so running this script will guide you through the process of setting it up:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Create serverless.yml file to configure your serveless instance:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Handler property is the route to the server file plus your exported handler. This is our server file /src/server.js:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We did the same with our resolvers: /src/queries/index.js (the squasher)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-f is the function defined on our serverless.yml and -p is the file mentioned above. To upload this to AWS just type:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
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.
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."