React-Apollo : Make query calls for a list of variables

515 Views Asked by At

Given a list of usernames and the following query to GITHUB API:-

query.gql:
query USER_QUERY($username:String!){
            user(login: $username){
                  name
                repositories(isFork:false){
                totalCount
              }
            }
          }

I want to create a composite query for every username.

Example:

Let's say this is the list of usernames

array usernames=["Sheldon","Leonard","Raj","Holowitz"]

Expected Query:

 {
  sheldon:user(login: "sheldon") {
    name
    repositories(isFork: false) {
      totalCount
    }
  }
  leonard:user(login: "leonard") {
    name
    repositories(isFork: false) {
      totalCount
    }
  }
  raj:user(login: "raj") {
    name
    repositories(isFork: false) {
      totalCount
    }
  }
  holowitz:user(login: "holowitz") {
    name
    repositories(isFork: false) {
      totalCount
    }
  }

Is there any reactive-way to achieve the result?

1

There are 1 best solutions below

8
On

There is no reactive way for this - react is not for glue string templates.

Your expected query is not constructed in the right graphQL way. It might work but you shouldn't think that way - when you need array - just ask for an array. If you really need this kind of structure do it (conversion) after feching 'normal' array - it's a matter of one-liner .filter() fn. Don't expect this (strange) format (behavoiur) from universal response.

Read related answers for this and this.

Even calling separate queries may be batched by apollo.