如何使用curl访问github graphql API

问题描述 投票:0回答:4

参考此指南后,我需要使用

graphql
访问github
curl
以进行测试。我尝试了这个简单的命令

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
4个回答
49
投票

您只需转义 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


36
投票

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

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

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"}}}

7
投票

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

这使您可以在您最喜欢的编辑器中编辑examplequery.gql时使用graphql语法突出显示插件

graphql漂亮的打印机
。同时还保留了在 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

示例query.gql

{
  user(login: "MatrixManAtYrService") {
    repositories(first: 3) {
      nodes {
        name
        languages(first: 3) {
          nodes {
            name
          }
        }
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.