You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An [Apollo GraphQL](https://www.apollographql.com/) server, built with [Cloudflare Workers](https://workers.cloudflare.com).[Try a demo by looking at a deployed GraphQL playground](https://graphql-on-workers.signalnerve.com/___graphql).
3
+
An [Apollo GraphQL](https://www.apollographql.com/) server, built with [Cloudflare Workers](https://workers.cloudflare.com).
4
4
5
-
Why this rules: Cloudflare Workers is a serverless application platform for deploying your projects across Cloudflare's massive distributed network. Deploying your GraphQL application to the edge is a huge opportunity to build consistent low-latency API servers, with the added benefits of "serverless" (I know, the project has `server` in it): usage-based pricing, no cold starts, and instant, easy-to-use deployment software, using [Wrangler](https://github.com/cloudflare/wrangler).
5
+
Whether you host your APIs on-prem, in the cloud, or you're deploying [databases](https://developers.cloudflare.com/d1) to Cloudflare directly, you can deploy a globally distributed GraphQL server with Cloudflare Workers.
6
6
7
-
By the way - as a full-stack developer who _loves_ GraphQL, and the developer advocate for Cloudflare Workers, I would love to see what you build with this! Let me know [on Twitter](https://twitter.com/signalnerve)!
7
+
## Setup
8
8
9
-
[](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/workers-graphql-server)
10
-
11
-
## Usage
12
-
13
-
You can begin building your own Workers GraphQL server by [installing Wrangler](https://workers.cloudflare.com/docs/quickstart/), the Workers command-line tool, and generating a new project:
9
+
Begin by cloning this repo and installing the dependencies:
You'll need to configure your project's `wrangler.toml` file to prepare your project for deployment. See the ["Configuration"](https://developers.cloudflare.com/workers/cli-wrangler/configuration/) docs for a guide on how to do this. Note that you'll need to [find your Cloudflare API keys](https://developers.cloudflare.com/workers/cli-wrangler/authentication/) to set up your config file.
16
+
You can begin running the project locally by running `npm run dev`.
17
+
18
+
You'll need to configure your project's `wrangler.toml` file to prepare your project for deployment. See the ["Configuration"](https://developers.cloudflare.com/workers/cli-wrangler/configuration/) docs for a guide on how to do this.
19
+
20
+
## Usage
20
21
21
-
The source for this project includes an example external REST data source, and defined types for the [PokeAPI](https://pokeapi.co/), as an example of how to integrate external APIs. Once you have the worker available, try this query as a sanity check:
22
+
The source for this project shows how to make requests to external APIs, using the [PokeAPI](https://pokeapi.co/) as an example. You can run an example query to ensure it works after deployment:
22
23
23
24
```graphql
24
-
querysamplePokeAPIquery{
25
-
pokemon: pokemon(id:1) {
26
-
id,
27
-
name,
28
-
height,
29
-
weight,
30
-
sprites{
31
-
front_shiny,
25
+
query {
26
+
pokemon: pokemon(id:1) {
27
+
id
28
+
name
29
+
height
30
+
weight
31
+
sprites{
32
+
front_shiny
32
33
back_shiny
33
34
}
34
35
}
35
36
}
36
37
```
37
38
38
-
To start using the project, configure your `graphQLOptions` object in `src/index.js`:
39
+
Resolvers are defined in `src/resolvers.ts`. You can also use [Service Bindings](https://developers.cloudflare.com/workers/runtime-apis/bindings/service-bindings/) to connect to other Workers services, and use them inside your resolvers.
40
+
41
+
If you change your GraphQL schema at `src/schema.graphql`, you'll need to run `npm run codegen` to update the generated types in `src/generated/graphql.ts`. This ensures that you can correctly import and type your resolvers.
42
+
43
+
## Configuration
44
+
45
+
You can optionally configure your `graphQLOptions` object in `src/index.js`:
cors:true, // Boolean or Object to further configure
47
-
kvCache:false, // Boolean
49
+
baseEndpoint:'/',
50
+
enableSandbox:true,
51
+
forwardUnmatchedRequestsToOrigin:false,
52
+
cors:true,
53
+
kvCache:false,
48
54
}
49
55
```
50
56
51
-
### Endpoints
57
+
### Base endpoint
58
+
59
+
Make requests to your GraphQL server by sending `POST` requests to the `baseEndpoint` (e.g. `graphql-on-workers.signalnerve.com/`).
60
+
61
+
### Sandbox
52
62
53
-
Make requests to your GraphQL server at the `baseEndpoint` (e.g. `graphql-on-workers.signalnerve.com/`) and, if configured, try GraphQL queries at the `playgroundEndpoint` (e.g. `graphql-on-workers.signalnerve.com/___graphql`).
63
+
By default, the Apollo Sandbox is enabled. This allows you to test your GraphQL in a web GUI without needing to write any client code.
54
64
55
65
### Origin forwarding
56
66
@@ -62,37 +72,64 @@ While configuring your server, you may want to set the `debug` flag to `true`, t
62
72
63
73
### CORS
64
74
65
-
By default, the `cors` option allows cross-origin requests to the server from any origin. You may wish to configure it to whitelist specific origins, methods, or headers. To do this, change the `cors` option to an object:
75
+
By default, the `cors` option allows cross-origin requests to the server from any origin. You may wish to configure it to whitelist specific origins, methods, or headers. This is done by passing an object to `cors`, which is based on the [hono/cors](https://hono.dev/docs/middleware/builtin/cors) middleware:
Note that by default, any field that you _don't_ pass here (e.g. `allowMethods`) will fallback to the default value. See `utils/setCors.js` for the default values for these fields.
92
+
### Caching
81
93
82
-
### REST caching
83
-
84
-
Version 1.1.0 of this project includes support for caching external requests made via instances of [`RESTDataSource`](https://www.apollographql.com/docs/apollo-server/features/data-sources/), using KV. To use caching in your project, [create a new KV namespace](https://workers.cloudflare.com/docs/reference/storage/writing-data), and in `wrangler.toml`, configure your namespace, calling it `WORKERS_GRAPHQL_CACHE`:
94
+
This project includes support for using Workers KV as a cache in your resolvers. To use caching in your project, [create a new KV namespace](https://workers.cloudflare.com/docs/reference/storage/writing-data), and in `wrangler.toml`, configure your namespace, calling it `KV_CACHE` (note that this binding name is _required_, currently):
85
95
86
96
```toml
87
97
# wrangler.toml
88
98
89
99
[[kv-namespaces]]
90
-
binding = "WORKERS_GRAPHQL_CACHE"
100
+
binding = "KV_CACHE"
91
101
id = "$myId"
92
102
```
93
103
94
104
With a configured KV namespace set up, you can opt-in to KV caching by changing the `kvCache` config value in `graphQLOptions` (in `index.js`) to `true`.
95
105
106
+
In any resolver function, you can access the `cache` object, which is an instance of [`KVCache`](https://github.com/cloudflare/workers-graphql-server/blob/master/src/kv-cache.ts). You can use `.get` and `.set` to interact with the cache:
107
+
108
+
```ts
109
+
pokemon: async (_parent, { id }, { cache }) => {
110
+
if (cache) {
111
+
const pokemon =awaitcache.get(id)
112
+
if (pokemon) {
113
+
returnpokemon
114
+
}
115
+
}
116
+
117
+
// You can hook into any util functions, API wrappers, or other
118
+
// code that you need to resolve your query.
119
+
const pokemonData =awaitPokemonAPI.getPokemon(id)
120
+
121
+
// You can also cache the data if you need to, with an optional TTL
122
+
if (cache) awaitcache.set(id, pokemonData, { ttl: 60 })
123
+
returnpokemonData
124
+
},
125
+
```
126
+
127
+
## Credits
128
+
129
+
This project is heavily based on the [@as-integrations/cloudflare-workers](https://github.com/apollo-server-integrations/apollo-server-integration-cloudflare-workers) package, which is a great tool for building GraphQL servers with Cloudflare Workers.
130
+
131
+
It is built with [Hono](https://github.com/honojs/hono), a simple and powerful web framework for Cloudflare Workers.
132
+
96
133
## License
97
134
98
135
This project is licensed with the [MIT License](https://github.com/cloudflare/workers-graphql-server/blob/master/LICENSE).
0 commit comments