Apollo Server + Serverless = Graphql Bliss!

Yes you heard me. A serverless server. Damn this nomenclature.

I’ve been working with GraphQL on and off for the last couple years. The project I’ve been on the most has been a React + Relay + GraphQL stack.

Until recently, my conclusion on GraphQL was:

GraphQL sounds like a great idea, if only I could figure out how it worked.

Enter Graphcool and Apollo.

In the last month, as I’ve been dabbling a little more in the front end, I found two great tools in this ecosystem:

  • Graphcool: a managed Graphql backend as a service, and
  • Apollo: an actually sane implementation of GraphQL (sorry Relay and Redux)

Note: to be fair, my distasteful experience with GraphQL in the last couple years has likely been a combination of the over complication of our project combined with the sheer complexity of Relay. Back when I was working on this project, writing new GraphQL mutations was a day long job!

Apollo Server

Once I had a play around with Apollo in the front end, and I got to see how easy it was to use, I thought I’d give it a shot as well. I’m more of a backend guy, but sometimes it’s fun to dabble around — am I right?

Out of the box, Apollo Server comes with configuration for AWS Lambda. Instead of using the AWS Configuration, we’re going to hook it up with the Serverless framework to build an easily maintainable, highly scaleable GraphQL service.

I’ve deployed apps both with the AWS Serverless application Model, and the serverless framework — and it seems that the serverless framework is well ahead of Amazon. Not to mention the fact that it supports multiple Function as a Service providers.

For those interested, you can find an example of setting up AWS Cloudformation’s Serverless Application Model (not to be confused with the serverless framework) on Apollo’s github here.

Prequisites:

  • node & npm
  • an AWS account with credentials

Setup:

$ npm install -g serverless
$ serverless create --template aws-nodejs --path al_serverless
$ cd al_serverless
$ npm init -y
$ npm install --save apollo-server-lambda graphql-tools lodash graphql

The Code

All set up. Let’s edit serverless.yml :

This defines the following:

  • a HTTP endpoint: /graphql for receiving POST requests
  • a /graphiql endpoint which will load the pretty query editor.

Next, we need to implement our handlers.

al_serverless/handler.js

These methods are pretty straightforward, in fact, Apollo Server takes care of most of the heavy lifting for us. If we wanted to set custom headers, or do some logging, handler.js would be the place to do it.

Finally, we must define a GraphQL schema for Apollo to use, and call makeExecutableSchema to do magic:

al_serverless/schema.js

const resolvers = {...} is our place to implement any custom logic, such as talk to another service or database. There’s a bunch of tutorials out there that cover this (and it’s getting late 😴) so we won’t go over it here. For reference, take a look at the apollo docs. Right now, we’re just going to mock it out with fake ‘Post’ data.

Alright. The coding is done for now. Let’s deploy it.

Test + Deploy

  • test serverless locally
$ serverless invoke local --function graphql --data '{"httpMethod": "POST",  "body": {"query":"{posts{id description}}"}}'
  • actually deploy and give it a go
$ serverless deploy
#get the url from command line, under endpoints> POST
$ URL="https://****.execute-api.ap-southeast-2.amazonaws.com/development/graphql"
$ curl -X POST $URL\
-H 'Content-Type: application/json' \
-d '{"query":"{posts{id description}}"}'

I must say that the Apollo team has done a great job! I look forward to seeing further developments 😋.

A good point to go from here would be to hook this up to a database. If I get enough comments below, I’ll go ahead and write it for you 😆😆😆.

If you got stuck, let me know! I’d love to help. Feel free to check out the full source code here as well.

If you enjoyed this post, or have any suggestions or questions, let me know in the comments. If you liked this post, give it a ❤️ or a 👏, or whatever you crazy cats are calling it nowadays.