如何使用curl访问github graphql API

如何使用curl访问github graphql API,curl,github,graphql,Curl,Github,Graphql,在参考了这个之后,我需要通过使用curl来访问githubgraphql,以进行测试。我试过这个简单的命令 curl -i -H "Authorization: bearer myGithubAccessToken" -X POST -d '{"query": "query {repository(owner: "wso2", name: "product-is") {description}}"}' https://api.github.com/graphql 但它给了我 解析JSON时出现

在参考了这个之后,我需要通过使用
curl
来访问github
graphql
,以进行测试。我试过这个简单的命令

curl -i -H "Authorization: bearer myGithubAccessToken" -X POST -d '{"query": "query {repository(owner: "wso2", name: "product-is") {description}}"}' https://api.github.com/graphql
但它给了我

解析JSON时出现问题


我做错了什么。我花了近2个小时试图找出它,并尝试了不同的例子,但没有一个奏效。您能帮我解决这个问题吗?您只需要将JSON中的双引号转义为查询

$ curl -i -H 'Content-Type: application/json' -H "Authorization: bearer myGithubAccessToken" -X POST -d '{"query": "query {repository(owner: \"wso2\", name: \"product-is\") {description}}"}' https://api.github.com/graphql


如果希望查询保持良好的多行性,可以这样做:

script='query{
repositoryOwner(登录名:\“danbst\”){
储存库(第一:100){
边缘{
节点{
所有者姓名
pullRequests(最后:100,状态:打开){
边缘{
节点{
标题
网址
作者{
登录
}
标签(首张:20){
边缘{
节点{
名称
}
}
}
}
}
}
}
}
}
}
}'
script=“$(echo$script)”#查询应该是一行,没有换行符
curl-i-H'内容类型:application/json'\
-H“授权:持票人……”\
-X POST-d“{\“query\”:\“$script\”}”https://api.github.com/graphql

我建议将graphql存储在一个文件中,并将用于处理它的脚本存储在一个单独的文件中,然后在提示时将两者结合起来

这使您可以在最喜爱的编辑器中使用和编辑
examplequery.gql
。同时还保留了在graphql fu无法完成任务的情况下使用cli工具包的能力

用法

❯ ./ghgql.sh examplequery.gql

    {"data":{"user":{"repositories":{"nodes":[{"name":"firstrepo","languages":{"nodes":[]}},{"name":"secondrepo","languages":{"nodes":[{"name":"Shell"},{"name":"Vim script"}]}},{"name":"thirdrepo","languages":{"nodes":[{"name":"TeX"}]}}]}}}}

❯ ./ghgql.sh examplequery.gql \
    | jq -c '.data.user.repositories.nodes | to_entries | .[]' \
    | grep 'TeX' \
    | jq -r '.value.name'

    thirdrepo

ghgql.sh

#!/usr/bin/env bash

if [ ! -f $1 ] || [ $# -ne 1 ]
then
    echo Queries the github graphql API
    echo "Usage:"
    echo
    echo "$0 somefile.gql"
fi

# read the gql query from the file named in the argument
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
TOKEN=$(cat $DIR/token)
QUERY=$(jq -n \
           --arg q "$(cat $1 | tr -d '\n')" \
           '{ query: $q }')

# do the query
curl -s -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: bearer $TOKEN" \
  --data "$QUERY" \
  https://api.github.com/graphql
examplequery.gql

{
  user(login: "MatrixManAtYrService") {
    repositories(first: 3) {
      nodes {
        name
        languages(first: 3) {
          nodes {
            name
          }
        }
      }
    }
  }
}

由于这是“graphql curl”的第一个成功案例,下面是一个简单的示例:

$ curl \
  --request POST \
  --header 'Content-Type: application/json' \
  --data '{"query": "query { fish(key:\"838\") { name } }"}' \
  http://localhost:4001

{"data":{"fish":{"name":"plecy"}}}

我是在尝试访问我自己的基于Django/石墨烯的API时得到这个答案的;为此,我需要一个额外的
-H'内容类型:application/json'
为什么需要
script=“$(echo$script)”
?@dkrikun删除换行符。截至编写时,请求正文中不允许使用换行符
sed
解决方案在这里也可以使用。看起来现在可以使用换行符了,我的JSON中有换行符(类似于浏览器使用的结构),只要引号被转义,它就可以完美地工作。我更新了解决方案,通过使用sed来避免转义引号。或者您可以使用
curl-H“授权:标记您的\u GITHUB\u标记”-X帖子https://api.github.com/graphql --data@gql.json
带有名为
gql.json
的文件,其中包含您的对象,您可以使用json格式化程序等轻松地使用您喜爱的代码编辑器进行更改。