Skip to content

Commit

Permalink
Initial code spike towards fixing CORS headers
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianfreeman committed Aug 19, 2019
1 parent 2e48eee commit cdaf59c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const apollo = require('./handlers/apollo')
const playground = require('./handlers/playground')
const setCors = require('./utils/setCors')

const graphQLOptions = {
// Set the path for the GraphQL server
Expand All @@ -17,7 +18,12 @@ const handleRequest = request => {
const url = new URL(request.url)
try {
if (url.pathname === graphQLOptions.baseEndpoint) {
return apollo(request, graphQLOptions)
const response =
request.method === 'OPTIONS'
? new Response('', { status: 204 })
: await apollo(request, graphQLOptions)
setCorsHeaders(response)
return response
} else if (
graphQLOptions.playgroundEndpoint &&
url.pathname === graphQLOptions.playgroundEndpoint
Expand Down
9 changes: 9 additions & 0 deletions src/utils/setCors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const setCorsHeaders = response => {
response.headers.set('Access-Control-Allow-Origin', '*')
response.headers.set('Access-Control-Allow-Credentials', 'true')
response.headers.set('Access-Control-Allow-Methods', 'GET,POST')
response.headers.set('Access-Control-Allow-Headers', 'application/json, Content-type')
response.headers.set('X-Content-Type-Options', 'nosniff')
}

module.exports = setCorsHeaders

0 comments on commit cdaf59c

Please sign in to comment.