【问题标题】:How to use part of a POST response as an argument for another post request? (pagination for GraphQL)如何使用 POST 响应的一部分作为另一个 post 请求的参数? (GraphQL 的分页)
【发布时间】:2018-07-08 01:41:51
【问题描述】:

我想使用“endCursor”通过分页向 Github GraphQL API 发出多个 POST 请求。

var unirest = require ('unirest');
var async = require('async');
const endpoint="https://api.github.com/graphql"
const token = "sometoken";

var search_query={
  "query": 'query($name: String!, $after: String){\
          search(query:$name, type: USER, first: 2, after: $after){\
                userCount\
                pageInfo{endCursor}\
                nodes{\
                   ...on User{\
                          id \
                          bio \
                          html_url:url \
                         }\
                       }\
                     }\
                   }',
     "variable": {"name":"somename", "after":null}
    };
unirest.post(endpoint)
.headers({"Content-Type": "text/html" , "Authorization": "bearer "+token, 'user-agent': 'node.js'})
.send(JSON.stringify(search_query)) //creating requests
.end(function(response, request){
    console.log(response.body.data.search.pageInfo.endCursor);
  });

我想多次迭代这个 POST 请求。每次使用 $after 作为最后一个请求的 endCursor。我该怎么做?谢谢!

【问题讨论】:

    标签: node.js post pagination graphql


    【解决方案1】:

    首先,我建议您也从 pageInfo 获取 hasNextPage 这样您就可以知道何时到达终点。

    之后你只需要更好地构建你的代码

    将 search_query 改为这样的函数:

    function searchQuery(after = null) {
      return {
        "query": `query($name: String!, $after: String){
              search(query:$name, type: USER, first: 2, after: $after){
                    userCount
                    pageInfo {
                        hasNextPage
                        endCursor
                    }
                    nodes{
                       ...on User{
                              id 
                              bio 
                              html_url:url 
                             }
                           }
                         }
                     }`,
        "variable": {
          "name": "somename",
          "after": after
        }
      };
    };

    现在searchQuery 返回一个基于参数after 的查询
    所以你需要做的是创建一个逻辑"while",当响应hasNextPage为假时完成:

    function queryServer(after) {
      unirest.post(endpoint)
        .headers({
          "Content-Type": "text/html",
          "Authorization": "bearer " + token,
          'user-agent': 'node.js'
        })
        .send(JSON.stringify(searchQuery(after))) //creating requests
        .end(function(response, request) {
          // do stuff with the result
          console.log(response.body.data.search.pageInfo.endCursor);
          
          // if there are more pages get them
          if (response.body.data.search.pageInfo.hasNextPage) {
            return queryServer(response.body.data.search.pageInfo.endCursor);
          }
        });
    }
    
    queryServer(null);

    注意:我还没有测试过代码,而且 IMO 使用支持 promises 的不同请求包(如 axios),代码可能会更干净,然后您甚至可以合并 async/await,这样会很多更易于使用和理解 而不是当前需要的那种丑陋的递归......

    【讨论】:

    • 感谢您的详细解答!我已经查看了Promise 并将其用作我的代码的一部分。非常感谢您的建议!
    • 很高兴,如果您能对此答案投赞成票,我将不胜感激,请参阅stackoverflow.com/help/someone-answers
    猜你喜欢
    • 2016-06-26
    • 1970-01-01
    • 2021-12-26
    • 2020-12-30
    • 2019-03-02
    • 2016-11-14
    • 2021-08-22
    • 2019-11-13
    • 2013-11-26
    相关资源
    最近更新 更多