17

enter image description here

const httpLink = createHttpLink({
  uri: 'http://localhost:3090/'
})

const client = new ApolloClient({
  link: httpLink,
  cache: new InMemoryCache()
})

client.query({
  query: gql`
    query users {
        email
    }
  `,
})
  .then(data => console.log(data))
  .catch(error => console.error(error));

This query gives an error when fetching from client-side code but when i execute this query in browser on http://localhost:3090/graphql it fetches data correctly

trixn
  • 15,761
  • 2
  • 38
  • 55
Mukesh Kumar
  • 944
  • 2
  • 10
  • 26

8 Answers8

26

The graphql endpoint you are posting your queries to is missing the /graphql. So your server probably returns an html document containing the 404 error message that starts with < from <html.... Apollo tries to parse that as the query result and fails to do so.

Check that httpLink is actually localhost:3090/graphql.

Also the syntax of a query is either:

{
    users {
        email
    }
}

or if you want to name the query:

query Users {
    users {
        email
    }
}
trixn
  • 15,761
  • 2
  • 38
  • 55
  • @MukeshKumar please add the full error message to your question. – trixn Nov 08 '18 at 14:39
  • after adding /graphql ..? – Mukesh Kumar Nov 08 '18 at 14:41
  • @MukeshKumar Yes. I can't tell you why it fails without the message. – trixn Nov 08 '18 at 14:42
  • POST http://localhost:3090/graphql 400 (Bad Request) index.js:1452 Error: Network error: Response not successful: Received status code 400 at new ApolloError (ApolloError.js:58) at QueryManager.js:522 at QueryManager.js:982 at Array.forEach () at QueryManager.js:981 at Map.forEach () at QueryManager.broadcastQueries (QueryManager.js:977) at QueryManager.js:477 – Mukesh Kumar Nov 08 '18 at 14:42
  • @MukeshKumar I'm affraid that does not contain any helpful information. But I think that the syntax of your query is incorrect too. It must be just ``gql`{ users { email } }`` or ``qgl`query Users { users { email } }` ``. – trixn Nov 08 '18 at 14:48
  • Looks like a network issue. Check where your backend is serving from. I made the same mistake – tufac2 Nov 19 '21 at 07:28
1

For posterity in case someone finds this in the future, another reason you might get this error is if your API is returning something other than JSON. https://medium.com/programmers-developers/one-simple-apollo-client-debugging-tip-youll-like-7877a97b9c16

I ran into this issue because the content type that was being returned from my API was text/plain rather than application/json. Apollo lets you specify a different body serializer in this case. https://www.apollographql.com/docs/link/links/rest/

Joyce Lee
  • 378
  • 3
  • 9
0

This happens when you do not define the path correctly in the queries files. This is a typechecking error when passing arguments

0

In my case this error was being thrown due to a trailing slash I had after the API URL. A possible fix would be checking your API URL properly and ensuring it is referencing the correct URL.

In my case, I only had to take out the trailing slash / after the URL.

ce0la
  • 143
  • 2
  • 6
0

As @Trixn posted: "So your server probably returns an html document containing the 404 error message that starts with < from <html.... Apollo tries to parse that as the query result and fails to do so."

In my case, I did not put "http://" in the beginning of my URL, and this returned a HTML web page with an error message.

Also check that usually, you should use "https://"

0

Make sure that you have configured the uri for httpLink and wsLink same. I have seen this error when I had different uris as below.

httpLink had uri : "http://localhost:4000" wsLink had uri: "ws://localhost:4000/graphql"

setting httpLink's uri to http://localhost:4000/graphql helped resolving the issue.

0

In my case, I recieved the same error, when I switched from a create-react-app to a next JS app pointing to port 3000.The react app had some values saved to local storage (tokens). In my case all I had to do, was to clear the local storage for localhost:3000, and I got rid of the error.

RBT
  • 24,161
  • 21
  • 159
  • 240
Eldrige
  • 21
  • 4
0

In my case, there was a typo in my graphql endpoint url. I had added a few characters to the end by accident. I think it would be good practice to add a catch rule for this situation so it does not break the site, just does not show the content and posts error to console log.