New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

graphql-metal

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

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

  • 0.0.4
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

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)

FAQs

Package last updated on 01 Dec 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc