1.0.3 • Published 4 years ago

@borderlesslabs/worker-graphql v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Worker GraphQL

NPM version NPM downloads Build status Test coverage

GraphQL server for worker environments (e.g. Cloudflare Workers).

GraphQL on Workers was inspired by this blog post, but using Apollo Server has a massive bundle size or can't bundle due to dependency on graphql-upload (a node.js dependency). Using worker-graphql resulted in a build of < 50KB.

Installation

npm install @borderlesslabs/worker-graphql --save

Usage

import { processGraphQL } from "@borderlesslabs/worker-graphql";
import { makeExecutableSchema } from "graphql-tools";

const schema = makeExecutableSchema({
  typeDefs: `
    type Query {
      hello: String
    }
  `,
  resolvers: {
    Query: {
      hello: () => "Hello world!"
    }
  }
});

// Wrap `processGraphQL` with CORS support.
const handler = async (req: Request) => {
  if (req.method.toUpperCase() === "OPTIONS") {
    return new Response(null, {
      status: 204,
      headers: {
        "Access-Control-Allow-Methods": "GET,POST",
        "Access-Control-Allow-Headers":
          req.headers.get("Access-Control-Request-Headers") || "Content-Type",
        "Access-Control-Allow-Origin": "*"
      }
    });
  }

  const res = await processGraphQL(req, { schema });
  res.headers.set("Access-Control-Allow-Origin", "*");
  return res;
};

addEventListener("fetch", event => {
  event.respondWith(handler(event.request));
});

License

MIT

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago