# 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).

Latest version **0.0.4** (published 2020-12-01) · Apache 2.0 license · 0 weekly downloads

## Install

```sh
npm install graphql-metal
pnpm add graphql-metal
yarn add graphql-metal
bun add graphql-metal
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2020-12-01 |
| First published | 2020-12-01 |
| Weekly downloads | 0 |
| License | Apache 2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 40.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | jcarver989 |

## Links

- npm: https://www.npmjs.com/package/graphql-metal
- npm.io page: https://npm.io/package/graphql-metal

## Dependencies (4)

- [graphql](https://npm.io/package/graphql.md) ^15.4.0
- [tiny-lru](https://npm.io/package/tiny-lru.md) ^7.0.6
- [graphql-jit](https://npm.io/package/graphql-jit.md) ^0.4.3
- [graphql-tools](https://npm.io/package/graphql-tools.md) ^7.0.1

## Recent versions

- 0.0.4 (latest) — 2020-12-01
- 0.0.3 — 2020-12-01
- 0.0.2 — 2020-12-01

## README

# 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](https://github.com/zalando-incubator/graphql-jit#readme) and falls back to
[graphql-js](https://github.com/graphql/graphql-js) if the query can't be compiled.

The only runtime dependencies of this package are:

```json
{
  "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.:

```TypeScript

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

```TypeScript
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:

```TypeScript
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:

```TypeScript
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:

```TypeScript
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)
```

---
_Source: https://npm.io/package/graphql-metal · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
