Skip to content

仓库 files navigation

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

关于

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

Topics

Resources

安全 policy

Stars

34 stars

关注者

2 watching

复刻s

发布

Used by

贡献者

Languages