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.

CORS ERROR when using Apollo Client to query Yelp Fusion

See original GitHub issue

Overview

  • Issue type: CORS ERROR when using Apollo Client to query Yelp Fusion. Using Apollo Client v3
  • Summary: Access to fetch at https://api.yelp.com/v3/graphql from localhost is blocked by CORS.
  • Platform: Desktop

Description

When using Apollo Client to query Yelp Fusion, access is denied. When requesting https://api.yelp.com/v3 or https://api.yelp.com/v3/businesses/search from localhost both are blocked by CORS. Created client using @apollo/client createHttpLink and SetContext. Request fails console error is thrown. When sending curl through postman works fine as expected – see parameters or sample request section below

More information

(Fill this section out if applicable (for things like bugs or questions))

Endpoint

https://api.yelp.com/v3/graphql https://api.yelp.com/v3/businesses https://api.yelp.com/v3/businesses/search

Parameters or Sample Request

cURL for https://api.yelp.com/v3/graphql - WORKS FINE

curl -X POST -H "Authorization: Bearer 2LoDZqt1aOH-XPiM1ef_kmKybmCiWC4zubqBznxU67xeHgcmg3SXXepn2bT-kavlQT4jwRwd9kZ0Z9zUrm-J_HwcJEKWMYklFbEyHjVz1MGwbeagllK0bJ4_JCMOX3Yx" -H "Content-Type: application/graphql" https://api.yelp.com/v3/graphql --data '
{
    business(id: "garaje-san-francisco") {
        name
        id
        alias
        rating
        url
    }
}'

Response

see snapshots Screen Shot 2020-07-21 at 3 08 26 PM Screen Shot 2020-07-21 at 3 08 46 PM

Extra information

Code:

import React from 'react';
import ReactDOM from 'react-dom';
import { ApolloClient, InMemoryCache, createHttpLink, gql } from '@apollo/client';
import { setContext } from '@apollo/client/link/context';
import API_KEY from '../.env';
import App from './App';

const httpLink = createHttpLink({
  uri: 'https://api.yelp.com/v3/businesses/search',
  credentials: 'include',
});

const authLink = setContext((_, { headers }) => {
  return {
    headers: {
      ...headers,
      authorization: `Bearer ${API_KEY}`,
    }
  }
})
const client = new ApolloClient({
  link: authLink.concat(httpLink),
  cache: new InMemoryCache(),

});

client.
  query({
    query: gql`
    query business {
      business(id: "garaje-san-francisco") {
        name
        id
        alias
        rating
        url
      }
    }
  `
  })
  .then(result => console.log(result))
ReactDOM.render(<App />, document.getElementById("root"));

I have tried adding headers like: Access-Control-Allow-Origin: *, content-type: application/json , content-type: application/graphql , added no-cors in the fetchOptions etc… same error persist

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:9

github_iconTop GitHub Comments

4reactions
paolaguitiancommented, Jul 27, 2020

@zaccanoy Thanks for the feedback & help, that’s what I was thinking. However, what baffles me is that here: https://www.yelp.com/developers/graphql/guides/requests they state in the docs that they support Apollo Client specifically React and React Native so it should be implicated that any browser request to Yelp Fusion should not be blocked by cors

2reactions
lhn136commented, Aug 7, 2021

Try using the CORS Anywhere proxy, it worked for me.

Just set the request URL from https://api.yelp.com/v3 to https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3

and unlock the demo challenge by going to https://cors-anywhere.herokuapp.com first, or setup your own proxy: https://github.com/Rob--W/cors-anywhere/issues/301

From Yelp: "Yelp API doesn’t support CORS and hence needs a server to connect with.

The auth flow we’ve built for the v3 API isn’t suited towards being integrated directly from a client, it risks exposing sensitive information from inside the app."

https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested-resource-whe

This works for me too! thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring CORS - Apollo GraphQL Docs
Control access to your server's resources​​ This feature requires that any client sending operations via GET or multipart upload requests must include a...
Read more >
How do I resolve the CORS error in Yelp API call?
Here's the code I'm using: var queryURL = "https://api.yelp.com/v3/"; var apiKey = "my key" $.ajax({ url ...
Read more >
CORS issue while using Apollo Client : r/graphql - Reddit
I am using apollo client to query my GraphQL Node API. I am trying to get all the books in my database. but,...
Read more >
Using Fusion's Signals API | Lucidworks
Clicking a search result to trigger sendClickSignal in such an environment results in an error: XMLHttpRequest cannot load http://fusionserver.local:8764/api/ ...
Read more >
Find the restaurant you like with Yelp Fusion and Vue.js
Yelp Fusion API — Business Search & Axios GET Request. To avoid the Cor issue, here we use the cors-anywhere and set the...
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 Hashnode Post

No results found