Building a Golang GraphQL API on AWS Lambda

Pedro Fernando Marquez Soto
A man with no server
4 min readJan 18, 2018

If you have interest in both Go and FaaS (or Serverless), you’re probably aware that Go is now one of the supported languages for AWS Lambda:

Since I played with Go for an IoT hackathon last year I’ve been looking for more opportunities to keep using it.

Go is fast, with a really low memory footprint and with the benefits of strongly typed languages, making it perfect for Serverless apps.

Go brings many of the benefits of using Java in Serverless apps, without the big memory requirements.

Of course, we still need to wait for the community to actually use it and compare it with other languages. But that won’t stop us from start playing with it today!

Coincidently, I’ve been working on a toy project for these past few weeks: A GraphQL API build on Node.js, running on AWS Lambda. So, since I had that idea already in my head, I wanted to see if I could manage to do the same with Go.

GraphQL for Go

There are a couple of projects out there which are implementing the GraphQL specification. Looking in forums, I found that github.com/neelance/graphql-go is well maintained, and with many developers using it actively.

Make sure you install the package for graphql-go:

go get github.com/neelance/graphql-go

So, using that library, I built a small GraphQL API on Golang:

I won’t go into a lot of detail here, but it’s basically building a schema with a single query, “Person”, which receives and ID as a string and returns a Person object.

Since this is only a test, the data is coming from an in-memory map, peopleData. The query is resolved by the Person function, which uses the ID passed as parameter as the map’s key, and returns its value.

Now, this works as a regular web application. You can see that it uses a module called “relay” and “net/http” to expose the GraphQL schema through a server running in 8080.

Wiring our GraphQL API to AWS Lambda handlers

We need to replace the contents of the main function to expose the GraphQL API through AWS’s handler methods.

So, lets take the tutorial published by AWS on how to create a Lambda function on Go, and merge that with our current GraphQL API.

Install AWS Go package:

go get github.com/aws/aws-lambda-go

In line 20, we parse the query from the request’s body. We unmarshall it into the “params” struct.

Instead of using the “relay” module, we manually pass the request parameters (query, operationName and variables) to the GraphQL schema mainSchema, which executes the query and returns a graphql object with the response.

We convert the result of the query to JSON and we return it as a string in a APIGatewayProxyResponse.

Now, in line 45, our main function only contains the Start function to initialize our Lambda.

Let’s put everything in a single file:

And that’s it. Build the application for Linux, and ZIP it:

$ GOOS=linux go build -o main

$ zip deployment.zip main

Usually, I use the Serverless Framework to deploy, but since support for Go was only released this week, we need to manually deploy the ZIP we just generated:

Make sure you add API Gateway as a trigger, and select “Go 1.x” as the Runtime. If you need more details, follow AWS’s post.

Once it’s deployed, try your API with cURL:

$ curl -XPOST -d ‘{“query”:”query test {\n person(id:\”1000\”) {\n id\n firstName\n }\n}\n”,”variables”:null,”operationName”:”test”}’ https://your-api-gateway-endpoint-here/GoGraphQL

And you should see something like this:

{“data”:{“person”:{“id”:”1000",”firstName”:”Pedro”}}}

As an extra, here is an unit test for your handler (replace pfernandom/helloWorldGraphQL with your package’s name):

You now have a working GraphQL API built on Go, running in AWS Lambda.

From here you can start breaking it into multiple files, adding more queries and mutations and real business logic in the GraphQL resolvers.

You can ask yourself how GraphQL fits in your Serverless application. Personally I think that it’s a great way to provide more flexibility than a simple REST API can give you.

You can reduce the number of functions published to Lambda since GraphQL queries offer the flexibility of adapting to different client’s use cases.

But even if you don’t want to use GraphQL, definitely Go has everything to become the favorite language for Serverless applications.

Feel free to reach out with your thoughts.

--

--