I am trying to create a query to Github GraphQL API that receive a list of repos as a parameter, and returns the info of those repositories in a single API call, does anyone know how to do that?
Something like this (I know this doesn’t work)
query myOrgRepos($repos: [String!]!) {
search(query:"""
repo in $repos
""", type: REPOSITORY, first: 100) {
repo: nodes {
... on Repository{
name
description
updatedAt
}
}
}
2
3 Answers
My understanding of your question is that you want to input a list of "known" repos and output information about them. In this case you can form a query of the following type.
{
nodes(ids: ["node_id_1", "node_id_2"]) {
... on Repository {
nameWithOwner
createdAt
}
}
}
Here the ids
are the node_ids
of the repositories you are interested in. You can obtain these by individual GraphQL calls as follows
{
repository(owner: "owner", name: "name") {
id
}
}
@David Butler gave an excellent answer on using GitHub’s GraphQL API.
I will extend the answer by giving more context:
Get the repository ID like this:
{
repository(owner: "github", name: "gitignore") {
databaseId
id
name
nameWithOwner
}
}
Response:
{
"data": {
"repository": {
"databaseId": 1062897,
"id": "MDEwOlJlcG9zaXRvcnkxMDYyODk3",
"name": "gitignore",
"nameWithOwner": "github/gitignore"
}
}
}
- get "owner" + "name" directly from the GitHub URL in your browser: https://github.com/github/gitignore
databaseId
is the integer id field you also get from the REST APIid
is a base64 encoded representation of thedatabaseId
- find all possible fields in the GraphQL docs
If you decode MDEwOlJlcG9zaXRvcnkxMDYyODk3
with base64 you get 010:Repository1062897
. You can read more what this means in this github community discussion.
Now you can get multiple repositories in a single call:
You need to use global node IDs (GraphQL docs):
{
nodes(ids: ["MDEwOlJlcG9zaXRvcnkxMDYyODk3", "MDEwOlJlcG9zaXRvcnk3NjkxNjMx"]) {
... on Repository {
databaseId
nameWithOwner
createdAt
}
}
}
Result:
{
"data": {
"nodes": [
{
"databaseId": 7691631,
"nameWithOwner": "moby/moby",
"createdAt": "2013-01-18T18:10:57Z"
},
{
"databaseId": 1062897,
"nameWithOwner": "github/gitignore",
"createdAt": "2010-11-08T20:17:14Z"
}
]
}
}
Unfortunately, the databaseIds don’t work on nodes
.
It took me some time to understand your comment so I will post the answer in full. The key is to use Github’s advanced search “syntax” in the query string, see here for examples.
The following query will find all repositories of user “github” as well as the graphql-js repository of user “graphql”:
query {
search(query: "user:github repo:graphql/graphql-js" type: REPOSITORY first: 10) {
nodes {
... on Repository {
name
description
updatedAt
}
}
}
}
I think that the right way would be query:"repo:org/repo1 repo:org/repo2 repo:org/repo3"
Dec 19, 2019 at 13:02
See also
gh search repo --owner user1,user2,...
as an alternative to GraphQL query.Apr 30 at 3:56