简体   繁体   English

如何在github graphql api上检索多个问题(使用javascript)?

[英]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. 我要实现的目标是使用javascript阅读并稍后在github存储库中编写问题标签

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: 在第一个makeRequest()上,我得到以下信息,这是可以的:

Status code: 200 Body: { data: { repository: { createdAt: '2017-09-29T17:01:25Z', name: 'hello-world', projectsUrl: ' https://github.com/TonyEdelweiss/hello-world/projects ' } } } 状态码:200正文:{数据:{仓库:{createdAt:'2017-09-29T17:01:25Z',名称:'hello-world',projectsUrl:' https : //github.com/TonyEdelweiss/hello-世界/项目 '}}}

On te second one I only get an '[Object]' )-: 在第二个我只得到'[Object]')-:

Status code: 200 Body: { data: { repository: { issues: [Object] } } } 状态码:200正文:{数据:{储存库:{问题:[对象]}}}

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." 我也在github API v4文档中发现了这一点:“所有GraphQL操作必须指定其选择,直到返回标量值的字段为止,以确保响应的形式明确。” This might explain why I am not getting the data, but gives no further guidance. 这也许可以解释为什么我没有得到数据,但是没有给出进一步的指导。

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 : 但是,您可以使用console.log查看的最大深度默认为2。您可以使用util.inspect进行更改,将depth设置为null可以查看整个对象:

const util = require('util');

.....

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM