GraphQL 复杂的 cURL 查询(GitHub API v4)
GraphQL complex cURL query (GitHub API v4)
我正在尝试在 JS 应用程序中对 GitHub v4 API 执行 GraphQL 查询,但出现 "Problem with JSON" 错误。
所以我尝试用 cURL 来做,但它似乎有一个我看不到的问题。
curl -H "Authorization: token bearer" -H "Content-Type:application/json" -X POST -d '{ "query": "query {
viewer {
watching(first: 3, orderBy: {field: UPDATED_AT, direction: DESC}) {
nodes {
refs(refPrefix: \"refs/heads/\", orderBy: {direction: DESC, field: TAG_COMMIT_DATE}, last: 100) {
nodes {
target {
... on Commit {
history(first: 10) {
...CommitFragment
}
}
}
}
}
}
}
}
rateLimit {
limit
cost
remaining
resetAt
}
}
fragment CommitFragment on CommitHistoryConnection {
nodes {
message
author {
name
avatarUrl(size: 30)
}
}
}
我想提一下,该请求正在 github.com/v4/explorer/ 网站上运行。
我已经逃脱了引号...
问题是查询是 JSON 字符串化的,在您的请求中,您在 JSON 字符串字段 query
中添加了新行,这是不允许的:
curl -H "Authorization: token YOUR_TOKEN" \
-H "Content-Type:application/json" \
-d '{
"query": "{ viewer { watching(first: 3, orderBy: {field: UPDATED_AT, direction: DESC}) { nodes { refs(refPrefix: \"refs/heads/\", orderBy: {direction: DESC, field: TAG_COMMIT_DATE}, last: 100) { nodes { target { ... on Commit { history(first: 10) { ...CommitFragment } }}}}}}} rateLimit { limit cost remaining resetAt}}fragment CommitFragment on CommitHistoryConnection { nodes { message author { name avatarUrl(size: 30) } } }"
}' https://api.github.com/graphql
您还可以使用 bash parameter expansion 删除新行并转义双引号:
token="YOUR_TOKEN"
query='{
viewer {
watching(first: 3, orderBy: {field: UPDATED_AT, direction: DESC}) {
nodes {
refs(refPrefix: "refs/heads/", orderBy: {direction: DESC, field: TAG_COMMIT_DATE}, last: 100) {
nodes {
target {
... on Commit {
history(first: 10) {
...CommitFragment
}
}
}
}
}
}
}
}
rateLimit {
limit
cost
remaining
resetAt
}
}
fragment CommitFragment on CommitHistoryConnection {
nodes {
message
author {
name
avatarUrl(size: 30)
}
}
}'
queryStr="${query//[$'\n|\r\n']}"
curl -s -H "Authorization: token $token" \
-H "Content-Type:application/json" \
-d '{
"query": "'"${queryStr//[\"]/\\"}"'"
}' https://api.github.com/graphql
在Javascript的情况下,如果你使用XMLHttpRequest
:
var http = new XMLHttpRequest();
var url = "https://api.github.com/graphql";
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/json");
http.setRequestHeader("Authorization", "token YOUR_TOKEN");
http.onreadystatechange = function() {
if (http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(JSON.stringify({
"query": '{ viewer { watching(first: 3, orderBy: {field: UPDATED_AT, direction: DESC}) { nodes { refs(refPrefix: "refs/heads/", orderBy: {direction: DESC, field: TAG_COMMIT_DATE}, last: 100) { nodes { target { ... on Commit { history(first: 10) { ...CommitFragment } }}}}}}} rateLimit { limit cost remaining resetAt}}fragment CommitFragment on CommitHistoryConnection { nodes { message author { name avatarUrl(size: 30) } } }'
}));
您还可以使用 apollo-client and graphql.js
查找示例
我正在尝试在 JS 应用程序中对 GitHub v4 API 执行 GraphQL 查询,但出现 "Problem with JSON" 错误。 所以我尝试用 cURL 来做,但它似乎有一个我看不到的问题。
curl -H "Authorization: token bearer" -H "Content-Type:application/json" -X POST -d '{ "query": "query { viewer { watching(first: 3, orderBy: {field: UPDATED_AT, direction: DESC}) { nodes { refs(refPrefix: \"refs/heads/\", orderBy: {direction: DESC, field: TAG_COMMIT_DATE}, last: 100) { nodes { target { ... on Commit { history(first: 10) { ...CommitFragment } } } } } } } } rateLimit { limit cost remaining resetAt } } fragment CommitFragment on CommitHistoryConnection { nodes { message author { name avatarUrl(size: 30) } } }
我想提一下,该请求正在 github.com/v4/explorer/ 网站上运行。 我已经逃脱了引号...
问题是查询是 JSON 字符串化的,在您的请求中,您在 JSON 字符串字段 query
中添加了新行,这是不允许的:
curl -H "Authorization: token YOUR_TOKEN" \
-H "Content-Type:application/json" \
-d '{
"query": "{ viewer { watching(first: 3, orderBy: {field: UPDATED_AT, direction: DESC}) { nodes { refs(refPrefix: \"refs/heads/\", orderBy: {direction: DESC, field: TAG_COMMIT_DATE}, last: 100) { nodes { target { ... on Commit { history(first: 10) { ...CommitFragment } }}}}}}} rateLimit { limit cost remaining resetAt}}fragment CommitFragment on CommitHistoryConnection { nodes { message author { name avatarUrl(size: 30) } } }"
}' https://api.github.com/graphql
您还可以使用 bash parameter expansion 删除新行并转义双引号:
token="YOUR_TOKEN"
query='{
viewer {
watching(first: 3, orderBy: {field: UPDATED_AT, direction: DESC}) {
nodes {
refs(refPrefix: "refs/heads/", orderBy: {direction: DESC, field: TAG_COMMIT_DATE}, last: 100) {
nodes {
target {
... on Commit {
history(first: 10) {
...CommitFragment
}
}
}
}
}
}
}
}
rateLimit {
limit
cost
remaining
resetAt
}
}
fragment CommitFragment on CommitHistoryConnection {
nodes {
message
author {
name
avatarUrl(size: 30)
}
}
}'
queryStr="${query//[$'\n|\r\n']}"
curl -s -H "Authorization: token $token" \
-H "Content-Type:application/json" \
-d '{
"query": "'"${queryStr//[\"]/\\"}"'"
}' https://api.github.com/graphql
在Javascript的情况下,如果你使用XMLHttpRequest
:
var http = new XMLHttpRequest();
var url = "https://api.github.com/graphql";
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/json");
http.setRequestHeader("Authorization", "token YOUR_TOKEN");
http.onreadystatechange = function() {
if (http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(JSON.stringify({
"query": '{ viewer { watching(first: 3, orderBy: {field: UPDATED_AT, direction: DESC}) { nodes { refs(refPrefix: "refs/heads/", orderBy: {direction: DESC, field: TAG_COMMIT_DATE}, last: 100) { nodes { target { ... on Commit { history(first: 10) { ...CommitFragment } }}}}}}} rateLimit { limit cost remaining resetAt}}fragment CommitFragment on CommitHistoryConnection { nodes { message author { name avatarUrl(size: 30) } } }'
}));
您还可以使用 apollo-client and graphql.js
查找示例