Scalable GraphQL Backends on Serverless

tirumaraiselvan profile image Tirumarai Selvan Updated on ・2 min read

GraphQL Serverless

In case you want to get started quickly with a GraphQL service hosted on serverless platforms like AWS Lambda, I have made a repository of basic boilerplates in various languages with instructions to deploy them on serverless. Check it out here: https://github.com/hasura/graphql-serverless Contributions are welcome for other languages, frameworks and platforms.

These are basic "Hello World" boilerplates with the following GraphQL schema:

type User {
  id:       Int
  name:     String
  balance:  Int

type Query {
  hello:  String
  users:  [User]

type Mutation {
  addUser(name: String, balance: Int): User
  transfer(userIdFrom: Int, userIdTo: Int, amount: Int): User

You can get started with the following languages and serverless platforms:

NodeJS + AWS Lambda

Python + AWS Lambda

Go + AWS Lambda

On Database Connections on Serverless

Serverless backends cannot share state between different requests. This means that state must be recreated in each serverless request. In the case of a GraphQL backend, a database connection must be created and destroyed in each request which not only is very slow in performance but also consumes the database resources very quickly.

In this repo, we also solve this problem by using a lightweight connection pooler like pgBouncer to proxy the requests to the database. We provide a CloudFormation template to easily deploy pgBouncer on a free EC2 instance.

Using With Hasura GraphQL Engine

While you can use these boilerplates to create any kind of GraphQL schema you may wish to merge your schema with Hasura GraphQL Engine to augment your schema with high-performance CRUD and realtime GraphQL APIs. Check out Hasura Remote Schema docs for details: https://docs.hasura.io/1.0/graphql/manual/remote-schemas/index.html

Posted on by:

tirumaraiselvan profile

Tirumarai Selvan


GraphQL and Serverless dude


Editor guide