A GitHub Action to send queries to GitHub's GraphQL API
Minimal example
name: Log latest release
on:
push:
branches:
- main
jobs:
logLatestRelease:
runs-on: ubuntu-latest
steps:
- uses: octokit/graphql-action@v2.x
id: get_latest_release
with:
query: |
query release($owner:String!,$repo:String!) {
repository(owner:$owner,name:$repo) {
releases(first:1) {
nodes {
name
createdAt
tagName
description
}
}
}
}
owner: ${{ github.event.repository.owner.name }}
repo: ${{ github.event.repository.name }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- run: "echo 'latest release: ${{ steps.get_latest_release.outputs.data }}'"
You can also use the variables
parameter to pass variables.
- uses: octokit/graphql-action@v2.x
with:
query: |
query release($owner:String!,$repo:String!) {
repository(owner:$owner,name:$repo) {
releases(first:1) {
nodes {
name
createdAt
tagName
description
}
}
}
}
variables: |
owner: ${{ github.event.repository.owner.name }}
repo: ${{ github.event.repository.name }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
To access deep values of outputs.data
, use fromJSON()
.
To see additional debug logs, create a secret with the name: ACTIONS_STEP_DEBUG
and value true
.
octokit/graphql-action
is using @octokit/graphql
internally with the addition
that requests are automatically authenticated using the GITHUB_TOKEN
environment variable. It is required to prevent rate limiting, as all anonymous requests from the same origin count against the same low rate.
The actions sets data
output to the response data. Note that it is a string, you should use fromJSON()
to access any value of the response. The action also sets headers
(again, to a JSON string) and status
.
New features of graphql require the use of an Accept header with a preview media-type.
This is supported with a mediaType
property, e.g.
steps:
- uses: octokit/graphql-action@v2.x
with:
query: |
query pullRequest(
$repo:String!
$owner:String!) {
repository(name:$repo, owner:$owner) {
pullRequests(last:1) {
nodes {
number
mergeStateStatus
}
}
}
}
owner: ${{ github.event.repository.owner.name }}
repo: ${{ github.event.repository.name }}
mediaType: |
previews:
- merge-info
It's important to remark input variables
are converted to lowercase at runtime. This happens with GitHub Actions by design1.
In the following example, the variable itemId
is casted to itemid
so, when trying to use it in the query
, the execution will fail because of a missing variable: itemId
query release($itemId: String!) {
...
# Fails with "Error: Variable $itemId of type String! was provided invalid value"
itemId: "randomId"
The recommendation1 is to use variables in lowercase to avoid this kind of problems:
# The variable name must be lower-case:
query release($itemid: String!) {
...
# Both: in the query and action var declaration:
itemid: "randomId"