用节点查询graphql [英] querying graphql with node

查看:67
本文介绍了用节点查询graphql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查询graphQL端点,但无法弄清楚自己在做什么错.我应该如何设置graphQL对象以传递过来.如果我想通过

I am trying to query a graphQL endpoint but I can't figure out what I am doing wrong. How am I supposed to set up the graphQL object to pass over. If I am trying to pass

{
  name {
     age
  }
}

我应该如何包装它以从服务器获得正确的响应?我目前正在使用的完整代码如下

How should I be wrapping this to get the correct response from the server? The full code I am currently working with is below

var http = require('http')
var qs = require('querystring')

var options = {
    hostname: 'url',
    method: 'POST',
    path: '/query'
}

var req = http.request(options, function(res){
    console.log('STATUS: ' + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));
    res.on('data', function (chunk) {
        console.log('BODY: ' + chunk);
    });
    res.on('end', function() {
        console.log('No more data in response.')
    })
})

var query = qs.stringify({data: {classes:{}}})


req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

req.write(query)
req.end()

推荐答案

您可以这样做.

payload = {
    "query": `{
        name {
            age
        }
    }`
}
JSON.stringify(payload)

您所用的名称查询"可能有所不同.基本上,您需要将JSON查询转换为字符串.这就是我使用GitHub GraphQL API v4的方式.

The name "query" might be different in your case. Basically, you need to convert the JSON query to string. This is how I do it with the GitHub GraphQL API v4.

这篇关于用节点查询graphql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆