2

I am trying to retrieve some data from Github's GraphQL API using graphql.js library.

var graph = graphql("https://api.github.com/graphql", {
  method: "POST",
  headers: {
    "Authorization": "Bearer <my-token-here>",
    "Content-Type": "application/json"
  },
  fragments: {
    rateLimitInfo: "on RateLimit {cost,remaining,resetAt}"
  }
});
graph(`
    query repo($name: String!, $owner: String!){
        repository(name:$name, owner:$owner){
            id      
        }
    }
`,{
    name: "freeCodeCamp",
    owner: "freeCodeCamp"
}).then(function(response){
    console.log(response);
}).catch(function(error){
    console.log(error);
});

My promise is not being fulfilled and always failing. I am getting an HTTP response with code 400 (Bad Request) and the error argument of the catch function reads:

{
    message: "Problems parsing JSON", 
    documentation_url: "https://developer.github.com/v3"
}

I have already tried passing the variables as JSON, like so:

{
    "name": "freeCodeCamp",
    "owner": "freeCodeCamp"
}

But it didn't help. I got the same bad request.

Looking at the Network tab of Chrome's inspector I see what the request payload is. Adding it here in case it give any clues or help.

query=query%20repo(%24name%3A%20String!%2C%20%24owner%3A%20String!)%7Brepository(name%3A%24name%2C%20owner%3A%24owner)%7Bid%7D%7D&variables=%7B%22name%22%3A%22freeCodeCamp%22%2C%22owner%22%3A%22freeCodeCamp%22%7D

What am I doing wrong?

1
  • Seems like the form the graphql.js is forming the data payload is not in the format that the Github API expects. Commented Jan 20, 2018 at 19:41

1 Answer 1

1

The default behaviour of graphql.js is to send the body in form-url-encoded format whereas Github GraphQL api accepts only JSON format. From graphql.js readme :

As default, GraphQL.js makes a POST request. But you can change the behavior by setting asJSON.

var graph = graphql("http://localhost:3000/graphql", {   
    asJSON: true
});

You can see the difference here

The following will work as expected :

var graph = graphql("https://api.github.com/graphql", {
  headers: {
    "Authorization": "Bearer YOUR_ACCESS_TOKEN"
  },
  asJSON: true
});
graph(`
    query repo($name: String!, $owner: String!){
        repository(name:$name, owner:$owner){
            id      
        }
    }
`, {
  name: "freeCodeCamp",
  owner: "freeCodeCamp"
}).then(function(response) {
  console.log(response);
}).catch(function(error) {
  console.log(error);
});
Sign up to request clarification or add additional context in comments.

1 Comment

I tried to run your code from html file but it doesn't seem to work. Asked the folks maintaining GraphQL.js but no response. Any idea why it would fail?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.