title: How to make Graphql run with CORS
body: <p>I've read several articles about this, but none of them work for me. </p>
  
  <p><a href="https://github.com/graphql/express-graphql/issues/14" rel="noreferrer">https://github.com/graphql/express-graphql/issues/14</a></p>
  
  <p>Here is my expressjs code:</p>
  
  <pre><code>app.use("/graphql", function (req, res, next) {
    res.header('Access-Control-Allow-Origin', 'http://localhost:8080');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
    if (req.method === 'OPTIONS') {
      res.sendStatus(200);
    } else {
      next();
    }
  });
  
  
  // apply graphql middleware
  app.use('/graphql', graphqlHTTP({
    schema: schema,
    rootValue: rootResolver,
    graphiql: true,
  }))
  </code></pre>
  
  <p>If I do it this way, the pre-flight OPTIONS is successful, but the actual POST request fails.</p>
  
  <p>I am using this function to make request to local graphql server.</p>
  
  <pre><code>function postFn(url, payload) {
    return $.ajax({
      method: 'POST',
      url: url,
      contentType: 'application/json',
      xhrFields: {
        withCredentials: true
      },
      data: payload
    });
  }
  </code></pre>
  
  <p>Here is the front-end code to trigger the POST request:</p>
  
  <pre><code>  let query = `
      query myqury($offset: Int, $limit: Int) {
        clients(limit:$limit , offset:$offset ) {
          docs{
            _id
            full_name
          }
          total
          limit
          offset
        }
      }
    `
    var variables = {
      offset: offset,
      limit: limit
    }
    let payload = {
      query: query,
      variables: variables
    }
    return request.post(graphQlEndpoint, payload)
  </code></pre>
  
  <p>The error message is:</p>
  
  <p>No 'Access-Control-Allow-Origin' header is present on the requested resource</p>