Tony Edelweiss
Tony Edelweiss

Reputation: 13

How to retrieve multiple issues on github graphql api (using javascript)?

The goal I want to achieve is to read and later write issues and labels within a github repository using javascript.

So far I have been able to get authenticated and retrieve some data on the repository, but I do not find the way to retrieve data neither on one single, nor on a set of issues.

This is the code I am using.

var request = require("request");

var url = 'https://api.github.com/graphql';
var headers = {
    Authorization:'token XXXXXXXXXXXXXXXXXXXXXXXXXXX',
    Accept: 'application/json',
    'User-Agent': 'request',
    'Content-Type': 'application/json'
};

var options = {
  method: 'post',
  body: undefined,
  json: true,
  url: url,
  headers: headers
};


function makeRequest(options){ 
    request(options, function (error, response, body) {
      if (error) {
        console.error('error posting json: ', error);
        throw error;
      }
      var responseHeaders = response.headers;
      var statusCode = response.statusCode;
      console.log('Status code: ', statusCode);
      console.log('Body: ', body);
    });
};

options.body = {
query: '{repository(owner:"TonyEdelweiss", name:"hello-world") {createdAt name projectsUrl}}'
};
makeRequest(options);

options.body = {
query: '{repository(owner:"TonyEdelweiss", name:"hello-world"){issues(first: 2){edges{cursor node{id}}}}}'
};
makeRequest(options);

On the first makeRequest() I get the following, which is okay:

Status code: 200 Body: { data: { repository: { createdAt: '2017-09-29T17:01:25Z', name: 'hello-world', projectsUrl: 'https://github.com/TonyEdelweiss/hello-world/projects' } } }

On te second one I only get an '[Object]' )-:

Status code: 200 Body: { data: { repository: { issues: [Object] } } }

Can anybody give a hint?

Also I have found this in github API v4 documentation: "All GraphQL operations must specify their selections down to fields which return scalar values to ensure an unambiguously shaped response." This might explain why I am not getting the data, but gives no further guidance.

Upvotes: 1

Views: 430

Answers (1)

Bertrand Martel
Bertrand Martel

Reputation: 45382

Your request is actually working fine. But the maximum depth you can view using console.log default to 2. You can use util.inspect to change it, set the depth to null to view the full object :

const util = require('util');

.....

console.log('Body: ', util.inspect(body, {depth: null}));

Upvotes: 0

Related Questions