0.0.4 • Published 3 years ago

graphql-metal v0.0.4

Weekly downloads
-
License
Apache 2.0
Repository
-
Last release
3 years ago

GraphQL Metal

GraphQL Metal is an experimental prototype that runs a (node.js) graphQL "server" on AWS Lambda as close to the "metal" as possible (or at least closer to the metal than apollo-server).

GraphQL Metal uses graphql-jit and falls back to graphql-js if the query can't be compiled.

The only runtime dependencies of this package are:

{
  "graphql": "^15.4.0",
  "graphql-jit": "^0.4.3",
  "graphql-tools": "^7.0.1",
  "tiny-lru": "^7.0.6"
}

Usage

Assuming you have a graphQL schema + resolvers, e.g.:

const typeDefs = `
    type Query {
        getPost(id: ID!): Post!
    }

    type Post {
        id: ID!
        content: String!
    }
`

const posts = [{ id: "1", content: "Hello World!" }]

const resolvers =  {
  Query: {
      getPost: (_, args) => {
        return posts.find(_ => _.id === args.id)
      }
  }

Then, create and export a Lambda handler

import { createHandler } from "graphql-metal"

export const handler = createHandler({
  typeDefs,
  resolvers,
  createContext: event => ({ authHeader: event.headers["authorization"] }),
  createHeaders: event => ({ "accesss-control-allow-origin": "*" })
})

You can configure caching and jit settings like so:

import { createHandler, GraphQLQueryCache } from "graphql-metal"
export const handler = createHandler({
  typeDefs,
  resolvers,
  jitThreshold: 2 // cache + jit a query as soon as we see it 2x
  queryCache: new GraphQLQueryCache({ maxSize: 10, ttlInMs: 30 * 60 * 1000 })
  createContext: event => ({ authHeader: event.headers["authorization"] }),
  createHeaders: event => ({ "accesss-control-allow-origin": "*" }),
})

And test your function like this:

import { event } from "graphql-metal"
import { Context } from "aws-lambda"

it("should get a post by id", async () => {
  const result = await handler(event({
    query: `
        query getPostQuery($id: ID!) {
            getPost(id: $id) {
                id
            }
        }
      `,
    variables: { id: "1" }
}),
    {} as Context, // fake Lambda context
    () => {}       // fake Lambda "callback"
)

  expect(result).toEqual({
    statusCode: 200,
    headers: { "accesss-control-allow-origin": "*", "content-type": "application/json" },
    body: JSON.stringify({ data: { getPost: { id: "1" } } })
  })
})

Notes on Deployment

Generally you'll want to connect your shiny new Lambda handler to API gateway. But you can also directly invoke your Lambda handler via the AWS SDK like so:

import { Lambda } from "aws-sdk"

const query = `
    query getPostQuery($id: ID!) {
        getPost(id: $id) {
            id
        }
    }
`

const variables = variables: { id: "1" }

const payload = {
  httpMethod: "POST",
  headers,
  body: JSON.stringify({ query, variables })
}

const request: Lambda.Types.InvocationRequest = {
  FunctionName: "foo-function",
  InvocationType: "RequestResponse",
  Payload: JSON.stringify(payload)
}

const lambda = new Lambda({ region: "us-east-1" })
const { StatusCode, FunctionError, Payload } = await lambda.invoke(request)