|
| 1 | +# Worker GraphQL |
| 2 | + |
| 3 | +[![NPM version][npm-image]][npm-url] |
| 4 | +[![NPM downloads][downloads-image]][downloads-url] |
| 5 | +[![Build status][travis-image]][travis-url] |
| 6 | +[![Test coverage][coveralls-image]][coveralls-url] |
| 7 | + |
| 8 | +> GraphQL server for worker environments (e.g. Cloudflare Workers). |
| 9 | +
|
| 10 | +GraphQL on Workers was inspired by [this blog post](https://blog.cloudflare.com/building-a-graphql-server-on-the-edge-with-cloudflare-workers/), but using Apollo Server has a [massive bundle size](https://github.com/apollographql/apollo-server/issues/1572) or can't bundle due to dependency on `graphql-upload` (a node.js dependency). Using `worker-graphql` resulted in a build of < 50KB. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +``` |
| 15 | +npm install @borderlesslabs/worker-graphql --save |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +```ts |
| 21 | +import { processGraphQL } from "@borderlesslabs/worker-graphql"; |
| 22 | +import { makeExecutableSchema } from "graphql-tools"; |
| 23 | + |
| 24 | +const schema = makeExecutableSchema({ |
| 25 | + typeDefs: ` |
| 26 | + type Query { |
| 27 | + hello: String |
| 28 | + } |
| 29 | + `, |
| 30 | + resolvers: { |
| 31 | + Query: { |
| 32 | + hello: () => "Hello world!" |
| 33 | + } |
| 34 | + } |
| 35 | +}); |
| 36 | + |
| 37 | +// Wrap `processGraphQL` with CORS support. |
| 38 | +const handler = async (req: Request) => { |
| 39 | + if (req.method.toUpperCase() === "OPTIONS") { |
| 40 | + return new Response(null, { |
| 41 | + status: 204, |
| 42 | + headers: { |
| 43 | + "Access-Control-Allow-Methods": "GET,POST", |
| 44 | + "Access-Control-Allow-Headers": |
| 45 | + req.headers.get("Access-Control-Request-Headers") || "Content-Type", |
| 46 | + "Access-Control-Allow-Origin": "*" |
| 47 | + } |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + const res = await processGraphQL(req, { schema }); |
| 52 | + res.headers.set("Access-Control-Allow-Origin", "*"); |
| 53 | + return res; |
| 54 | +}; |
| 55 | + |
| 56 | +addEventListener("fetch", event => { |
| 57 | + event.respondWith(handler(event.request)); |
| 58 | +}); |
| 59 | +``` |
| 60 | + |
| 61 | +## License |
| 62 | + |
| 63 | +MIT |
| 64 | + |
| 65 | +[npm-image]: https://img.shields.io/npm/v/borderlesslabs/worker-graphql.svg?style=flat |
| 66 | +[npm-url]: https://npmjs.org/package/borderlesslabs/worker-graphql |
| 67 | +[downloads-image]: https://img.shields.io/npm/dm/borderlesslabs/worker-graphql.svg?style=flat |
| 68 | +[downloads-url]: https://npmjs.org/package/borderlesslabs/worker-graphql |
| 69 | +[travis-image]: https://img.shields.io/travis/borderlesslabs/worker-graphql.svg?style=flat |
| 70 | +[travis-url]: https://travis-ci.org/borderlesslabs/worker-graphql |
| 71 | +[coveralls-image]: https://img.shields.io/coveralls/borderlesslabs/worker-graphql.svg?style=flat |
| 72 | +[coveralls-url]: https://coveralls.io/r/borderlesslabs/worker-graphql?branch=master |
0 commit comments