GraphQL 复杂 cURL 查询(GitHub API v4)

Posted

技术标签:

【中文标题】GraphQL 复杂 cURL 查询(GitHub API v4)【英文标题】:GraphQL complex cURL query (GitHub API v4) 【发布时间】:2018-08-21 15:42:12 【问题描述】:

我正在尝试在 JS 应用程序中对 GitHub v4 API 执行 GraphQL 查询,但出现“JSON 问题”错误。 所以我试图用 cURL 来做,但它似乎有一个我看不到的问题。

curl -H "授权:令牌持有者" -H "Content-Type:application/json" -X POST -d ' "query": "query 观众 观看(第一:3,orderBy:字段:UPDATED_AT,方向:DESC) 节点 refs(refPrefix: \"refs/heads/\", orderBy: direction: DESC, field: TAG_COMMIT_DATE, last: 100) 节点 目标 ...提交 历史(第一:10) ...提交片段 限速 限制 成本 其余的 重置 CommitHistoryConnection 上的片段 CommitFragment 节点 信息 作者 姓名 头像网址(大小:30)

我想提一下,该请求正在 github.com/v4/explorer/ 网站上运行。 而且我已经逃脱了引号...

【问题讨论】:

【参考方案1】:

问题是查询是 JSON 字符串化的,在您的请求中,您在 JSON 字符串字段 query 添加新行,这是不允许的:

curl -H "Authorization: token YOUR_TOKEN" \
     -H  "Content-Type:application/json" \
     -d ' 
          "query": " viewer  watching(first: 3, orderBy: field: UPDATED_AT, direction: DESC)  nodes  refs(refPrefix: \"refs/heads/\", orderBy: direction: DESC, field: TAG_COMMIT_DATE, last: 100)  nodes  target  ... on Commit  history(first: 10)  ...CommitFragment   rateLimit  limit cost remaining resetAtfragment CommitFragment on CommitHistoryConnection  nodes  message author  name avatarUrl(size: 30)   "
      ' https://api.github.com/graphql

您还可以使用bash parameter expansion 删除新行并转义双引号:

token="YOUR_TOKEN"
query='
  viewer 
    watching(first: 3, orderBy: field: UPDATED_AT, direction: DESC) 
      nodes 
        refs(refPrefix: "refs/heads/", orderBy: direction: DESC, field: TAG_COMMIT_DATE, last: 100) 
          nodes 
            target 
              ... on Commit 
                history(first: 10) 
                  ...CommitFragment
                
              
            
          
        
      
    
  
  rateLimit 
    limit
    cost
    remaining
    resetAt
  


fragment CommitFragment on CommitHistoryConnection 
  nodes 
    message
    author 
      name
      avatarUrl(size: 30)
    
  
'
queryStr="$query//[$'\n|\r\n']"

curl -s -H "Authorization: token $token" \
     -H  "Content-Type:application/json" \
     -d ' 
          "query": "'"$queryStr//[\"]/\\\""'"
      ' https://api.github.com/graphql

javascript 的情况下,如果您使用 XMLHttpRequest

var http = new XMLHttpRequest();
var url = "https://api.github.com/graphql";
http.open("POST", url, true);

http.setRequestHeader("Content-type", "application/json");
http.setRequestHeader("Authorization", "token YOUR_TOKEN");

http.onreadystatechange = function() 
    if (http.readyState == 4 && http.status == 200) 
        alert(http.responseText);
    


http.send(JSON.stringify(
    "query": ' viewer  watching(first: 3, orderBy: field: UPDATED_AT, direction: DESC)  nodes  refs(refPrefix: "refs/heads/", orderBy: direction: DESC, field: TAG_COMMIT_DATE, last: 100)  nodes  target  ... on Commit  history(first: 10)  ...CommitFragment   rateLimit  limit cost remaining resetAtfragment CommitFragment on CommitHistoryConnection  nodes  message author  name avatarUrl(size: 30)   '
));

您还可以使用apollo-client 和graphql.js 查找示例

【讨论】:

完美适用于 curl 版本!谢谢你的回答!

以上是关于GraphQL 复杂 cURL 查询(GitHub API v4)的主要内容,如果未能解决你的问题,请参考以下文章