Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to batch GitHub GraphQL API queries?

How can multiple queries be batched into a single request to GitHub's GraphQL API?

For example, how would you batch these 2 queries into a single request and receive a single response? And would this technique work with many more queries (say 200)?

{
  repositoryOwner(login:"rails") {
    repository(name:"rails") {
      description
      homepageURL
    }
  }
}

{
  repositoryOwner(login:"github") {
    repository(name:"graphql-client") {
      description
      homepageURL
    }
  }
}

(The GitHub GraphQL API can be experimented with at https://developer.github.com/early-access/graphql/explorer/)

like image 241
Eliot Sykes Avatar asked Dec 14 '22 03:12

Eliot Sykes


1 Answers

You need to wrap the calls to both fields in one query:

{
  repositoryOwner(login:"rails") {
    repository(name:"rails") {
      description
      homepageURL
    }
  } 
  repositoryOwner(login:"github") {
    repository(name:"graphql-client") {
      description
      homepageURL
    }
  }
}

This will still fail though, as there are now two fields in the output with the same name (repositoryOwner), so you need to alias them:

{
   rails: repositoryOwner(login:"rails") {
     repository(name:"rails") {
       description
       homepageURL
     }
   } 
   graphql_client: repositoryOwner(login:"github") {
     repository(name:"graphql-client") {
       description
       homepageURL
     }
   }
 }

See this explanation.

If you could generate a unique alias for every one of your queries, then yes this technique should work fine.

like image 171
MikeRalphson Avatar answered Jan 29 '23 06:01

MikeRalphson