Unexpected End Of JSON On GraphQL Query With React While No Issue With GraphiQL
I am trying to do a very basic query via React with Apollo.
When I do this query in GraphiQL I nicely get my results back but in my app I get an undefined data object. And a error with a message:
Network error: Unexpected end of JSON input
The query is:
query {
category(id: 3) {
id
children {
id
name
}
}
}
This is my component
import React, { Component } from 'react';
import { Query } from 'react-apollo';
import gql from 'graphql-tag';
const CATEGORIES_LIST = gql`
query CATEGORIES_LIST {
category(id: 3) {
id
children {
id
name
}
}
}
`;
class Cat extends Component {
render() {
return (
<div>
<p>Items!</p>
<Query query={CATEGORIES_LIST}>
{payload => {
console.log(payload);
return <p>fetch done!</p>;
}}
</Query>
</div>
)
}
}
export default Cat;
While the GraphiQL response is with the exact same request
{
"data": {
"category": {
"id": 3,
"children": [
{
"id": 4,
"name": "Bags"
},
{
"id": 5,
"name": "Fitness Equipment"
},
{
"id": 6,
"name": "Watches"
}
]
}
}
}
By the way I'm querying a local Magento 2.3 graphql server.
When inspecting the network tab this is the response i get from the graphql endpoint. So no url typo are issue in the response
{
"data":{
"category":{
"id":3,
"children":[
{
"id":4,
"name":"Bags",
"__typename":"CategoryTree"
},
{
"id":5,
"name":"Fitness Equipment",
"__typename":"CategoryTree"
},
{
"id":6,
"name":"Watches",
"__typename":"CategoryTree"
}
],
"__typename":"CategoryTree"
}
}
}
Answer
Ok, i found it.
First issue was that i used
no-cors
option on theApolloClient
Which prevents it from ready the data thus sending back a empty data object.Second issue was that I needed to set my CORS headers on my GraphQL server properly, just for development accepting all with a * that solved it for the development phase.
Third and last issue was that Apollo sends a
OPTIONS
request to preflight check the CORS headers to see if its all allowed. Magento 2.3 flipped over that because its an empty request thus providing you with aUnable to unserialize value
error.
What i did to solve that third issue is temporary patching a core file during deployment. The following file needs to be changed: /vendor/magento/module-graph-ql/Controller/GraphQl.php
on line 111 the following is needed
- $data = $this->jsonSerializer->unserialize($request->getContent());
+ $content = ($request->getContent() === '') ? '{}' : $request->getContent();
+ $data = $this->jsonSerializer->unserialize($content);
I think there are other solutions for this on the React / Apollo side but haven't found that one yet.
Related Questions
- → Import statement and Babel
- → should I choose reactjs+f7 or f7+vue.js?
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → .tsx webpack compile fails: Unexpected token <
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → React Native with visual studio 2015 IDE
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
- → How do I determine if a new ReactJS session and/or Browser session has started?
- → Alt @decorators in React-Native
- → How to dynamically add class to parent div of focused input field?