4

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

Jotta E
  • 292
  • 5
  • 14

2 Answers2

4

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.

osowskit
  • 5,904
  • 2
  • 29
  • 38
  • Thanks for the quick answer though i already did that. But to my opinion it's not comfortable at all using an Array of ID's, it's like using ip's when you have DNS. i was looking for a solution that i will be able to pass an Array of the organizations & repositories names , it is far more convenient. Probably they are not there yet. Maybe it's the answer for now. – Jotta E Jun 27 '18 at 09:36
  • The array of IDs would be closer to using a MAC address (or some concept of a unique identifier) since the Repository/Organization names can change. Using IDs will handle users modifying the names. Separately, you can batch the array of IDs to avoid making multiple queries which is also likely more efficient for both applications to handle/implement. – osowskit Jul 11 '18 at 19:19
1

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
}
Jotta E
  • 292
  • 5
  • 14