2
votes

I'm using react native fetch to make a post http request with graphql data to a graphql server endpoint. When starting up my graphql server and calling fetch with a POST in my react native app, I'm getting the following error:

TypeError: Network Request Failed at XMLHttpRequest in react native app

Request code

let graphqlServer = config.graphQLServerUrl;
const graphQLFetcher = (graphQLParams, requestType, token) => {
let body = null; //earlier i used var body =' '; //blank value --null & blank makes a different
let headers = null;
body = graphQLParams; 

headers = {
  'Accept': 'application/json',
  'Content-Type': 'application/graphql',
  'Authorization': 'Bearer ' +  token
 };

  return  fetch(graphqlServer, {
    method: requestType,
    headers: headers,
    body: graphQLParams,
  });
};

Data qraphQLParams query { login(userName: ${credentials.userName}, passWord: ${credentials.passWord}) } ,

Any ideas?

1
I`m facing the same problem. Have you found a solution?Valter Júnior
JSON.stringify the body. (body is the graphql query itself)user3162979

1 Answers

0
votes

I managed to get this to work by simply JSON.stringify the body of the request.

So..

graphQLParams = `query login{
      login(input: {userName: "${obj.input.userName}", passWord: "${obj.input.passWord}" }) {
        token
      }
    }
  ` 

body = JSON.stringify({
  query: graphQLParams
 });