Skip to content

Commit 36ce340

Browse files
Merge pull request #14 from signalnerve/1.1.0
1.1.0
2 parents 5d6a28b + 18396ff commit 36ce340

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

README.md

+42-1
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,55 @@ const graphQLOptions = {
2626
playgroundEndpoint: '/___graphql', // ?String
2727
forwardUnmatchedRequestsToOrigin: false, // Boolean
2828
debug: false, // Boolean
29+
cors: true, // Boolean or Object to further configure
30+
kvCache: false // Boolean
2931
}
3032
```
3133

34+
### Endpoints
35+
3236
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`).
3337

38+
### Origin forwarding
39+
3440
If you run your GraphQL server on a domain already registered with Cloudflare, you may want to pass any unmatched requests from inside your Workers script to your origin: in that case, set `forwardUnmatchedRequestToOrigin` to true (if you're running a GraphQL server on a [Workers.dev](https://workers.dev) subdomain, the default of `false` is fine).
3541

36-
Finally, while configuring your server, you may want to set the `debug` flag to `true`, to allow script errors to be returned in your browser. This can be useful for debugging any errors while setting up your GraphQL server.
42+
### Debugging
43+
44+
While configuring your server, you may want to set the `debug` flag to `true`, to return script errors in your browser. This can be useful for debugging any errors while setting up your GraphQL server, but should be disabled on a production server.
45+
46+
### CORS
47+
48+
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:
49+
50+
```js
51+
const graphQLOptions = {
52+
// ... other options ...
53+
54+
cors: {
55+
allowCredentials: 'true',
56+
allowHeaders: 'Content-type',
57+
allowOrigin: '*',
58+
allowMethods: 'GET, POST, PUT',
59+
}
60+
}
61+
```
62+
63+
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.
64+
65+
### REST caching
66+
67+
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`:
68+
69+
```toml
70+
# wrangler.toml
71+
72+
[[kv-namespaces]]
73+
binding = "WORKERS_GRAPHQL_CACHE"
74+
id = "$myId"
75+
```
76+
77+
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`.
3778

3879
## License
3980

src/handlers/apollo.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const createServer = graphQLOptions =>
1818
resolvers,
1919
introspection: true,
2020
dataSources,
21-
...(graphQLOptions.enableKvCache ? kvCache : {}),
21+
...(graphQLOptions.kvCache ? kvCache : {}),
2222
})
2323

2424
const handler = (request, graphQLOptions) => {

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const graphQLOptions = {
3131
// Note that you'll need to add a KV namespace called
3232
// WORKERS_GRAPHQL_CACHE in your wrangler.toml file for this to
3333
// work! See the project README for more information.
34-
enableKvCache: false
34+
kvCache: false,
3535
}
3636

3737
const handleRequest = async request => {

0 commit comments

Comments
 (0)