I'm trying to make graphql calls from a React component to a PHP server, using fetch
. It is the first time I try using fetch
.
I'm using React JS on browser side and Symfony 4 on server side. However all of my calls end up with an empty body on server side, even though my browser dev tools seem to indicate data is sent.
When using graphiql, the requests succeed. I'm trying as much as possible to mimic the graphiql request from React, but the body is always blank.
Here is the call:
fetch('/api', {
'method': 'post',
'credentials': 'include',
'headers': {
'Content-Type': 'application/json',
},
'body': JSON.stringify({
"query": "mutation {UpdateActionStatus(input: {actionId: 61, status: TO_DO}) { id }}",
"variables": null
}),
}).then(r => {
console.log(r.json());
return r.json();
}).catch(error => {
console.log('There has been a problem with your fetch operation: ', error.message);
});
I have tried with mode
set to cors
, no-cors
, same-origin
, navigate
: none of these work.
I have tried with Firefox and Chrome: still no luck.
Changing the content type to application/graphql
does not seem to work either.
I have seen many questions dealing with empty bodies in both responses and requests when using fetch
, on stackoverflow and elsewhere, but none of the found solutions seem to solve to my case.
I also tried the following, much cruder call, to no avail. It seems I just cannot use fetch at all for a reason I cannot explain:
fetch('/api', {
'method': 'post',
'credentials': 'include',
'headers': {
'Content-Type': 'application/x-www-form-urlencoded',
},
'body': 'test=test',
});
What to do? Am I missing something simple here?