Skip to content

Instantly share code, notes, and snippets.

@bobvanderlinden
Last active March 9, 2023 16:39
Show Gist options
  • Save bobvanderlinden/a42ba178266640c0951d3cfaa4f1347f to your computer and use it in GitHub Desktop.
Save bobvanderlinden/a42ba178266640c0951d3cfaa4f1347f to your computer and use it in GitHub Desktop.
Example GraphQL call towards GitHub using curl and jq

This is an example how to do GraphQL queries in a somewhat sane way in bash using curl and jq.

The example does an query towards GitHub to fetch the commit SHAs of open pull requests for a specific repository.

The example needs the environment variables REPO_OWNER, REPO_NAME and GITHUB_TOKEN to be set.

jq -cn '
{
query: $query,
operationName: $operationName,
variables: {
owner: $owner,
repo: $repo
}
}' \
--arg query '
query getPullRequestOids($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
pullRequests(states: OPEN, first: 100) {
nodes {
headRefOid
}
}
}
}' \
--arg operationName getPullRequestOids \
--arg owner "$REPO_OWNER" \
--arg repo "$REPO_NAME" \
| curl \
--silent \
--header "Authorization: token $GITHUB_TOKEN" \
--header "Content-Type: application/json" \
--data @- \
https://api.github.com/graphql \
| jq -r '.data.repository.pullRequests.nodes | .[] | .headRefOid'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment