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
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
}
}
@MukeshKumar please add the full error message to your question.
after adding /graphql ..?
@MukeshKumar Yes. I can't tell you why it fails without the message.
POST 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 (<anonymous>) at QueryManager.js:981 at Map.forEach (<anonymous>) at QueryManager.broadcastQueries (QueryManager.js:977) at QueryManager.js:477
@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 } }
orqgl`query Users { users { email } }`
.