question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support CORS enabled servers and OPTIONS requests

See original GitHub issue
  // GraphQL HTTP only supports GET and POST methods.
    if (request.method !== 'GET' && request.method !== 'POST') {
      response.set('Allow', 'GET, POST');
      return sendError(
        response,
        httpError(405, 'GraphQL only supports GET and POST requests.'),
        pretty
      );
    }

Currently if the Express app enables CORS express-graphql will fail due to the above check sending a 405 error when the OPTIONS request comes in.

Two options I see:

  1. Create a flag for enabling CORS in express-graphql and enable OPTION support when true
  2. Just support OPTION requests by default

I’d be happy to implement either, or another option, pending approval!

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:37 (7 by maintainers)

github_iconTop GitHub Comments

112reactions
mnpennercommented, May 17, 2016

Okay, I got CORS working for my needs. Here’s a full(ish) example for anyone else who stumbles upon this:

import express from 'express';
import graphqlHttp from 'express-graphql';

const port = 8080;
const app = express();
const schema = require('schema/schema.js');
const Chalk = require('chalk');
const cors = require('cors');

app.use('/graphi',express.static(`${__dirname}/public`)); // we could have just used the `graphiql` option: https://github.com/graphql/express-graphql

app.use('/graphql', cors(), graphqlHttp(() => ({
    schema: schema
})));
app.listen(port);
console.log(`Started on ${Chalk.underline.blue(`http://localhost:${port}/`)}`);
45reactions
xuxucodecommented, May 2, 2017

A simple way without use of the cors module:

app.use("/graphql", function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
  if (req.method === 'OPTIONS') {
    res.sendStatus(200);
  } else {
    next();
  }
});

app.use('/graphql', graphQLHTTP({ schema }));
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cross-Origin Resource Sharing (CORS) - MDN Web Docs
The CORS mechanism supports secure cross-origin requests and data transfers between browsers and servers. Modern browsers use CORS in APIs ...
Read more >
CORS Enabled - W3C Wiki
CORS can be enabled using the Headers core module which is compiled into nginx by default: ... Set the CORS options on your...
Read more >
Enabling CORS for a REST API resource - Amazon API Gateway
For a mock integration, you enable CORS by creating an OPTIONS method to return the required response headers (with appropriate static values) as...
Read more >
CORS Support - MockServer
By default, CORS support is not enabled for the Control Plane API and or for mocked response, such as, when expectations are matched,...
Read more >
CORS Tutorial: A Guide to Cross-Origin Resource Sharing
When a server has been configured correctly to allow cross-origin resource sharing, some special headers will be included. Their presence can be ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found