如何使用curl访问github graphql API

Kas*_*iya 12 curl github graphql

在参考本指南后,我需要graphql通过curl用于测试目的来访问github .我尝试了这个简单的命令

curl -i -H "Authorization: bearer myGithubAccessToken" -X POST -d '{"query": "query {repository(owner: "wso2", name: "product-is") {description}}"}' https://api.github.com/graphql
Run Code Online (Sandbox Code Playgroud)

但它给了我

解析JSON的问题

我做错了什么.我花了将近2个小时试图弄清楚它并尝试了不同的例子,但没有一个能够奏效.能帮助我解决这个问题吗?

Yur*_*mke 14

您只需要将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

Run Code Online (Sandbox Code Playgroud)

  • 我试图访问我自己的基于Django/Graphene的API来回答这个问题.为此,我需要额外的`-H'Content-Type:application/json'` (4认同)

Mat*_*ice 7

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

\n

这使您可以在编辑时使用graphql 语法突出显示插件graphql 漂亮的打印机examplequery.gql。同时还保留了在 graphql-fu 无法胜任任务的情况下使用 cli 工具包的能力。

\n

用法

\n
\xe2\x9d\xaf ./ghgql.sh examplequery.gql\n\n    {"data":{"user":{"repositories":{"nodes":[{"name":"firstrepo","languages":{"nodes":[]}},{"name":"secondrepo","languages":{"nodes":[{"name":"Shell"},{"name":"Vim script"}]}},{"name":"thirdrepo","languages":{"nodes":[{"name":"TeX"}]}}]}}}}\n\n\xe2\x9d\xaf ./ghgql.sh examplequery.gql \\\n    | jq -c \'.data.user.repositories.nodes | to_entries | .[]\' \\\n    | grep \'TeX\' \\\n    | jq -r \'.value.name\'\n\n    thirdrepo\n\n
Run Code Online (Sandbox Code Playgroud)\n

ghgql.sh

\n
#!/usr/bin/env bash\n\nif [ ! -f $1 ] || [ $# -ne 1 ]\nthen\n    echo Queries the github graphql API\n    echo "Usage:"\n    echo\n    echo "$0 somefile.gql"\nfi\n\n# read the gql query from the file named in the argument\nDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"\nTOKEN=$(cat $DIR/token)\nQUERY=$(jq -n \\\n           --arg q "$(cat $1 | tr -d \'\\n\')" \\\n           \'{ query: $q }\')\n\n# do the query\ncurl -s -X POST \\\n  -H "Content-Type: application/json" \\\n  -H "Authorization: bearer $TOKEN" \\\n  --data "$QUERY" \\\n  https://api.github.com/graphql\n
Run Code Online (Sandbox Code Playgroud)\n

示例查询.gql

\n
{\n  user(login: "MatrixManAtYrService") {\n    repositories(first: 3) {\n      nodes {\n        name\n        languages(first: 3) {\n          nodes {\n            name\n          }\n        }\n      }\n    }\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n


Bri*_*rns 7

由于这是“graphql curl”的第一次点击,因此这里有一个简单的示例:

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

{"data":{"fish":{"name":"plecy"}}}
Run Code Online (Sandbox Code Playgroud)


dan*_*bst 5

如果您希望查询保持美观和多行,可以这样做:

script='query {
  repositoryOwner(login:\"danbst\") {
    repositories(first: 100) {
      edges {
        node {
          nameWithOwner
          pullRequests(last: 100, states: OPEN) {
            edges {
              node {
                title
                url
                author {
                  login
                }
                labels(first: 20) {
                  edges {
                    node {
                      name
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}'
script="$(echo $script)"   # the query should be onliner, without newlines

curl -i -H 'Content-Type: application/json' \
   -H "Authorization: bearer ........." \
   -X POST -d "{ \"query\": \"$script\"}" https://api.github.com/graphql
Run Code Online (Sandbox Code Playgroud)

  • 或者您可以只使用 `curl -H "Authorization: token YOUR_GITHUB_TOKEN" -X POST https://api.github.com/graphql --data @gql.json` 和一个名为 `gql.json` 的文件,其中包含您的对象,您可以在其中使用您最喜欢的代码编辑器和 json 格式化程序等轻松进行更改。 (4认同)