【问题标题】:How to use curl to access the github graphql API如何使用 curl 访问 github graphql API
【发布时间】:2017-06-20 15:21:24
【问题描述】:

引用此guide 后,我需要使用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 个小时试图弄清楚它并尝试了不同的示例,但都没有奏效。请您帮我解决这个问题吗?

【问题讨论】:

    标签: curl github graphql


    【解决方案1】:

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

    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 a one-liner, without newlines
    
    curl -i -H 'Content-Type: application/json' \
       -H "Authorization: bearer ........." \
       -X POST -d "{ \"query\": \"$script\"}" https://api.github.com/graphql
    

    【讨论】:

    • 为什么需要script="$(echo $script)"?
    • @dkrikun 删除换行符。截至撰写本文时,请求正文中不允许换行。 sed 解决方案也可以在这里使用
    • 现在好像允许换行了,我已经在我的 JSON 中得到了它们(类似于资源管理器使用的结构),只要引号被转义,它就可以完美地工作
    • 我通过使用 sed 更新了解决方案以避免转义引号。
    • 或者您也可以使用curl -H "Authorization: token YOUR_GITHUB_TOKEN" -X POST https://api.github.com/graphql --data @gql.json 和一个名为gql.json 的文件,其中包含您的对象,您可以在其中使用您喜欢的带有json 格式化程序的代码编辑器等轻松地进行更改。
    【解决方案2】:

    因为这是 'graphql curl' 的第一个热门,这里有一个简单的例子:

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

    【讨论】:

      【解决方案3】:

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

      这使您可以在您喜欢的编辑器中编辑examplequery.gql 时使用graphql syntax highlighting plugins 和graphql pretty printers。同时还保留了在您的 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
                }
              }
            }
          }
        }
      }
      

      【讨论】:

      • 我将 bearer 更改为我的 GitHub 用户名,并将来自 GitHub 的 PAT 放在工作目录中名为 token 的文件中,但仍然得到 { "message": "This endpoint requires you to be authenticated.", "documentation_url": "https://docs.github.com/graphql/guides/forming-calls-with-graphql#authenticating-with-graphql" } 我不理解 token 的传递方式吗?
      【解决方案4】:

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

      【讨论】:

      • 我来到这个答案试图访问我自己的基于 Django/Graphene 的 API;为此,我需要一个额外的-H 'Content-Type: application/json'
      猜你喜欢
      • 2017-06-20
      • 2014-10-29
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-21
      • 2018-02-08
      相关资源
      最近更新 更多