Skip to content
Software Engineering Stories
Go back

Serverless GraphQL Implementation

Infrastructure

Create a serverless GraphQL implementation ready production, including CI/CD.

Developers should be able to just clone, add AWS credentials, and deploy away using GitHub Actions.

AWS Playground GitHub Repo

Implementation details

Use apollo-server-lambda npm package to serve the Apollo GraphQL through AWS Lambda. It exposes the API to the Apollo implementation.

src/main.js fromrdok/serverless-graphql at05ba96araw
const { ApolloServer, gql } = require('apollo-server-lambda');

GraphQL playground

The playground provides a GUI for making GraphQL requests. E.g. GitHub’s Playground

When running locally, the playground is enabled by default. However, if you wish to have the playground available when deploying to AWS infrastructure, there is an additional step to take. Instruct the Lambda, when the requested HTTP method is GET, to server playground UI:

src/main.js fromrdok/serverless-graphql at05ba96araw

return server.createHandler()(
  { ...event, path: event.requestContext.path || event.path },
  lambdaContext,
  callback,
);

CORS

Modify the AWS SAM template to allow all CORS.

template.yml fromrdok/serverless-graphql at05ba96araw
ApiGatewayApi:
  Type: AWS::Serverless::Api
  Properties:
    StageName: "Prod"
    Cors: "'*'"

Share this post:

Previous Post
Minimalistic AWS SAM PHP
Next Post
Cyberpunk 2077 R6 Config Editor