@borderless/worker-graphql
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

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 @borderless/worker-graphql --save

Usage

import { processGraphQL } from "@borderless/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

/@borderless/worker-graphql/

    Package Sidebar

    Install

    npm i @borderless/worker-graphql

    Weekly Downloads

    2

    Version

    1.0.4

    License

    MIT

    Unpacked Size

    33.6 kB

    Total Files

    9

    Last publish

    Collaborators

    • blakeembrey