如何使用 curl 访问 github graphql API
How to use curl to access the github graphql API
参考此 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
但它给了我
problems parsing JSON
我做错了什么。我花了将近 2 个小时试图弄清楚它并尝试了不同的示例,但其中 none 个有效。你能帮我解决这个问题吗
您只需要将 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(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
我建议将 graphql 存储在一个文件中,将处理它的脚本存储在一个单独的文件中,然后在提示时将两者结合起来。
这样您就可以在使用自己喜欢的编辑器编辑 examplequery.gql
的同时使用 graphql syntax highlighting plugins and 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 ] || [ $# -ne 1 ]
then
echo Queries the github graphql API
echo "Usage:"
echo
echo "[=11=] 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 | 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"}}}
参考此 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
但它给了我
problems parsing JSON
我做错了什么。我花了将近 2 个小时试图弄清楚它并尝试了不同的示例,但其中 none 个有效。你能帮我解决这个问题吗
您只需要将 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(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
我建议将 graphql 存储在一个文件中,将处理它的脚本存储在一个单独的文件中,然后在提示时将两者结合起来。
这样您就可以在使用自己喜欢的编辑器编辑 examplequery.gql
的同时使用 graphql syntax highlighting plugins and 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 ] || [ $# -ne 1 ]
then
echo Queries the github graphql API
echo "Usage:"
echo
echo "[=11=] 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 | 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"}}}