How to batch multiple queries?


#1

Let’s say I want to retrieve the avatar URLs of 10 different users specifying their login. How would I do that with the below query?

query {
    user(login: "octocat") {
    avatarUrl
  }
}

Batching & mutations?
#2

Hi @bartclaeys!

There are a couple of different ways this is possible. If you don’t have their GraphQL Node IDs, you can do something like this:

{
  octocat: user(login: "octocat") {
    ...NameInfo
  }
  nickvanw: user(login: "nickvanw") {
    ...NameInfo
  }
}

fragment NameInfo on User {
  avatarUrl
  login
}

If you happened to collect their Node IDs, you could do something like this:

query {
 nodes(ids:["MDQ6VXNlcjE5MDcwNzE=","MDQ6VXNlcjU4MzIzMQ=="]) {
   ... on User {
     avatarUrl
     login
   }
 }
}

Let me know if this doesn’t answer your question!


Batching & mutations?
#3

Thanks! I don’t understand your second piece of code. Where do the node IDs come from?


#4

Hey!

Good question! In lieu of database IDs like you’ll find in REST, GraphQL has a concept of ‘Global Node IDs’ which can be used to reference and lookup individual objects without needing to know the type ahead of time.

There’s some light documentation about this in our GraphQL API Documentation here.

To get those IDs, you can go to two different places: RESTv3 or the GraphQL API.

In this case, I modified my first query to select the id field from User as well, at it’s most basic level you can grab your Global Relay ID by doing:

query {
  viewer {
    id
  }
}

It’s also possible to fetch them with the REST API for various objects, which you can find here: https://developer.github.com/v3/#graphql-global-relay-ids

Let me know if you have any other questions, or if I haven’t been clear!