|
1 | 1 | const apollo = require('./handlers/apollo')
|
2 | 2 | const playground = require('./handlers/playground')
|
| 3 | +const setCors = require('./utils/setCors') |
3 | 4 |
|
4 | 5 | const graphQLOptions = {
|
5 | 6 | // Set the path for the GraphQL server
|
6 | 7 | baseEndpoint: '/',
|
| 8 | + |
7 | 9 | // Set the path for the GraphQL playground
|
8 | 10 | // This option can be removed to disable the playground route
|
9 | 11 | playgroundEndpoint: '/___graphql',
|
| 12 | + |
10 | 13 | // When a request's path isn't matched, forward it to the origin
|
11 | 14 | forwardUnmatchedRequestsToOrigin: false,
|
| 15 | + |
12 | 16 | // Enable debug mode to return script errors directly in browser
|
13 | 17 | debug: false,
|
| 18 | + |
| 19 | + // Enable CORS headers on GraphQL requests |
| 20 | + // Set to `true` for defaults (see `utils/setCors`), |
| 21 | + // or pass an object to configure each header |
| 22 | + cors: true, |
| 23 | + // cors: { |
| 24 | + // allowCredentials: 'true', |
| 25 | + // allowHeaders: 'Content-type', |
| 26 | + // allowOrigin: '*', |
| 27 | + // allowMethods: 'GET, POST, PUT', |
| 28 | + // }, |
14 | 29 | }
|
15 | 30 |
|
16 | 31 | const handleRequest = request => {
|
17 | 32 | const url = new URL(request.url)
|
18 | 33 | try {
|
19 | 34 | if (url.pathname === graphQLOptions.baseEndpoint) {
|
20 |
| - return apollo(request, graphQLOptions) |
| 35 | + const response = |
| 36 | + request.method === 'OPTIONS' |
| 37 | + ? new Response('', { status: 204 }) |
| 38 | + : await apollo(request, graphQLOptions) |
| 39 | + if (graphQLOptions.cors) { |
| 40 | + setCors(response, graphQLOptions.cors) |
| 41 | + } |
| 42 | + return response |
21 | 43 | } else if (
|
22 | 44 | graphQLOptions.playgroundEndpoint &&
|
23 | 45 | url.pathname === graphQLOptions.playgroundEndpoint
|
|
0 commit comments