Create a Serverless GraphQL server using Express, Apollo Server and AWS Lambda
…because who wouldn’t want a Serverless GraphQL server, right !?
If you’ve mucked around with GraphQL, Serverless and love JavaScript — you’ve come to the right place. What’s better than mixing a few of the most exciting technologies in recent times, not much, eh.
Continuing in our Serverless journey, and further building on a previous post, where we’ve explored Rendering and serving a Create-React-App from an Express server running within a Lambda function — let’s now turn our focus towards GraphQL and marry it with Serverless. And, in doing so, eliminate the need to have a continuously running GraphQL instance, an ops dream.
We will be using AWS Lambda functions along with the Serverless Framework for our server. Our Lambdas will run the Node.JS 8.1 runtime. We will also make use of Webpack for all it’s bundling goodness and Babel to transpile our pile.
In order to get our Serverless GraphQL server up and running we will need to:
First thing’s first, install the Serverless framework and all the necessary dependencies it’ll need:
We will also need to install Webpack, node-externals packages, and babel-loader. Next create a config file for Webpack:
and place the following, basic config in it:
You can find more information about the serverless-webpack plugin here.
Next we’re going to need to create and configure our serverless.yml file:
Place the following YML in it:
We will add and configure our functions next.
Right, let’s move on to our server. We will need to install Express, Apollo Server and a few other dependencies:
We’re also going to use the Prisma GraphQL playground to test our fancy server. Let’s install the necessary packages:
Create an index.js file which will contain our server code as well as be our entry point:
Place the following code in it:
Right, our index.js has been setup to accommodate two lambda functions, one for the GraphQL Apollo Server and the other for the Playground. Update the serverless.yml file and add the two functions we need:
…and with that, we’ve pretty much concluded the setup. Let’s give our GraphQL Apollo Server a test! Recall, earlier, we added serverless-offline as a dependency, let’s go ahead and startup our server offline:
This will start a local instance of our server. You should see the following output:
Running our GraphQL server for the first time!
You’ll notice that our two endpoints will be created, /graphql being our GraphQL server endpoint and our playground at /playground .To test our server, let’s navigate to our playground, http://localhost:3000/playground
Execute a simple query, and behold the magic of a Serverless GraphQL server…
Our Serverless GraphQL server in all it’s glory
In a few simple steps, we’ve created a simple Express and GraphQL server on AWS Lambda. To deploy our server, all we need to do now, is run:
Hook up your server to your React frontend, server-side rendered from a Lambda too and you’ve got yourself quite a flexible Serverless solution. This concludes our little attempt at marrying Serverless and GraphQL — two of the hottest technologies in the scene right now.
You can find the code for this post at: https://github.com/FBI23/serverless-express-graphql