Reputation: 302
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
Upvotes: 4
Views: 3793
Reputation: 302
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
}
Upvotes: 1
Reputation: 6344
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.
Upvotes: 4