GraphQl仅返回对象而不是数据/查询

发布于 2025-02-10 23:24:53 字数 759 浏览 1 评论 0原文

我是GraphQl的新手,并且在将我的GraphQl响应解析为一个数组中遇到了麻烦:

var url = "https://data.objkt.com/v2/graphql";
var XMLHttpRequest = require('xhr2');
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "application/json");

xhr.onreadystatechange = function () {

    if (xhr.readyState === 4) {
   
        console.log(xhr.status);
        console.log(xhr.responseText);

    }

};

const data = JSON.stringify({
  query: `{
    currency(distinct_on: fa_contract) {
    id
  }
  }`,
});

xhr.send(data);

我回来的响应是:

{"data":{"currency":[{"id":65223}, {"id":2}, {"id":1}]}}

如何获得仅返回对象(括号内的ID)而不是“数据”和“数据”和“货币”零件以便我可以将其解析为阵列?

我尝试使用正则匹配项来提取括号,但我知道那不是正确/理想的方法!

谢谢

I'm new to GraphQL and am having trouble parsing my GraphQL response into an array:

var url = "https://data.objkt.com/v2/graphql";
var XMLHttpRequest = require('xhr2');
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "application/json");

xhr.onreadystatechange = function () {

    if (xhr.readyState === 4) {
   
        console.log(xhr.status);
        console.log(xhr.responseText);

    }

};

const data = JSON.stringify({
  query: `{
    currency(distinct_on: fa_contract) {
    id
  }
  }`,
});

xhr.send(data);

The response I get back is:

{"data":{"currency":[{"id":65223}, {"id":2}, {"id":1}]}}

How can I get the response to only return the object (the ids inside the brackets) and not the "data" and "currency" parts so I can proceed to parse it into an array?

I tried using regex matches to extract the brackets but I know that's not the proper/ideal way!

thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文