Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub graphQL API multiple queries on organizations & repositories

I am trying to get from GitHub using graphQL multiple organizations & repositories data. The code i wrote below get only 1 organization & repository. I thought using variables of two Array [String!] one for Organizations & the second for Repositories. So

organization(login:"javascript") {...} 

should be

organization(login:$organization) {...}

and

repository(owner:"javascript", name:"sorted-array") {...}

should be

repository(owner:$organization, name:$repository) {...}

But i couldn't find how to implement the variables into the below code.

query {
  rateLimit{
  cost
  remaining
  resetAt
 }
organization(login:"javascript") {
    repositories{
      totalCount
    }
  }
  repository(owner:"javascript", name:"sorted-array") {
    updatedAt
    branches: refs(refPrefix:"refs/heads/") {
      totalCount
    }
    tags: refs(refPrefix:"refs/tags/") {
      totalCount
    }
    releases {
      totalCount
    }
    object(expression:"master") {
      ... on Commit {
        committedDate
        history {
          totalCount
        }
      }
    }
  }
}

Will appreciate the help. Thanks

like image 509
Jotta E Avatar asked Oct 29 '25 09:10

Jotta E


2 Answers

Here is the request updated to work with variables

query getOrg($owner: String!, $repo: String! ){
    organization(login:$owner) {
    repositories{
        totalCount
    }
  }
  repository(owner:$owner, name:$repo) {
    updatedAt
    branches: refs(refPrefix:"refs/heads/") {
      totalCount
    }
    tags: refs(refPrefix:"refs/tags/") {
      totalCount
    }
    releases {
      totalCount
    }
    object(expression:"master") {
      ... on Commit {
        committedDate
        history {
          totalCount
        }
      }
    }
  }
}

However, you may be better served by using GraphQL Node IDs as an array of inputs. With the Node ID of the Organization or Repository, something along the lines of...

query inputArray($id: [ID!]!){
  nodes(ids: $id){
    ...on Repository{
      id
    }
  }
}

Note that GitHub V3 of the API supports returning the GraphQL Node IDs to help transition to GraphQL queries.

like image 199
osowskit Avatar answered Nov 01 '25 12:11

osowskit


Here are my solutions i hope it will be helpful to someone:) First answer is using the Array variables with multiple id's of the organizations "Facebook","JavaScript" and the repositories "360-Capture-SDK","sorted-array". If you have 10 ,20 organizations/repositories or more , you will have a great time fetching the id's from the REST API:).

query inputArray($idOrg: [ID!]! $idRepo: [ID!]!){
  orgNode:nodes(ids: $idOrg){
    ...on Organization{
      name
    }
  }
  repNode:nodes(ids: $idRepo){
    ...on Repository{
      name
    }
  }
}

{
  "idOrg": ["MDEyOk9yZ2FuaXphdGlvbjY5NjMx","MDEyOk9yZ2FuaXphdGlvbjE3ODIxODA="],
  "idRepo":["MDEwOlJlcG9zaXRvcnk4Njg2MDg0Nw==","MDEwOlJlcG9zaXRvcnk5NzkxMTYy"]
}

Second answer is using a more readable approach though cumbersome.

query{
  FacebookOrg: organization(login: "facebook") {
    ...OrgInfo
  }

  JavaScriptOrg: organization(login: "javaScript") {
    ...OrgInfo
  }

  FacebookRep: repository(owner: "facebook" name:"360-Capture-SDK"){
    ...RepInfo
  }

  JavaScriptRep: repository(owner: "javaScript" name:"sorted-array"){
    ...RepInfo
  }
}

fragment OrgInfo on Organization {
  name
}

fragment RepInfo on Repository {
  name
}
like image 43
Jotta E Avatar answered Nov 01 '25 12:11

Jotta E



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!