Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CORS issue with using localhost:3000 to access graphql API at a different URL

I have searched all over StackOverflow and saw similar questions but have not found a working solution. Wondering if anyone has a working solution? I am developing a create react app on localhost:3000 and trying to access through my Apollo Client a URI on a different site (I am testing with https://developer.github.com/v4/explorer/).

I have added the fetchOptions mode of 'no-cors' to my new ApolloClient instance but I am still getting a CORS error in my console. My enitre index.js file is below:

import React from 'react';
import ReactDOM from 'react-dom';

import { ApolloProvider } from 'react-apollo';
import { ApolloClient, HttpLink, InMemoryCache } from 'apollo-boost';

import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';


const client = new ApolloClient({
    link: new HttpLink({ uri: 'https://developer.github.com/v4/explorer/' }),
    fetchOptions: {
        mode: 'no-cors',
      },
    cache: new InMemoryCache()
  });
  
  const AppWithProvider = () => (
    <ApolloProvider client={client}>
      <App />
    </ApolloProvider>
  );
ReactDOM.render(<AppWithProvider />, document.getElementById('root'));


serviceWorker.unregister();

Error message in console: Access to fetch at 'https://developer.github.com/v4/explorer/' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

like image 752
Elaine Avatar asked Sep 10 '19 22:09

Elaine


People also ask

How do I fix localhost error in CORS?

1. Use the proxy setting in Create React App. "proxy": "https://cat-fact.herokuapp.com/", Now when you make an API request to https://localhost:3000/api/facts Create React App will proxy the API request to https://cat-fact.herokuapp.com/facts and the CORS error will be resolved.

How do I enable CORS in GraphQL?

You can enable credentials with CORS by setting the Access-Control-Allow-Credentials HTTP header to true .

How do I fix CORS error in API?

Cross-Origin Resource Sharing (CORS) errors occur when a server doesn't return the HTTP headers required by the CORS standard. To resolve a CORS error from an API Gateway REST API or HTTP API, you must reconfigure the API to meet the CORS standard.

How do I fix strict origin when cross origin?

In order to fix CORS, you need to make sure that the API is sending proper headers (Access-Control-Allow-*). That's why it's not something you can fix in the UI, and that's why it only causes an issue in the browser and not via curl: because it's the browser that checks and eventually blocks the calls.


1 Answers

In order for CORS to work, the server needs to be configured to support your host (in this case localhost). However, CORS is not needed for server-to-server communication, so what you typically do is set up a proxy.

Create-react-app has built-in support for setting up such a proxy: https://create-react-app.dev/docs/proxying-api-requests-in-development/.

like image 187
peternyc Avatar answered Sep 24 '22 16:09

peternyc