1

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 Answers1

0

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.

xadm
  • 8,219
  • 3
  • 14
  • 25
  • Query is correct wrt graphQL manners. Please refere [here](https://github.com/apollographql/graphql-tag) for different query format. This is to enable queries to be separated from script over .graphql||.gql files. – Jon Patrovi Oct 05 '18 at 14:40
  • No, expected query is written in right graph QL way It is aliasing the response for every username – Jon Patrovi Oct 07 '18 at 06:48
  • "It might work but you shouldn't think that way" - I didn't write that it's incorrect - YAGNI - don't complicate simple things – xadm Oct 07 '18 at 07:10
  • Okay, seconded. How can I ask for an array if The API simply does not provide that for this api, It just takes single username and responses with the "asked" data for the "single username" – Jon Patrovi Oct 07 '18 at 08:04
  • You should wrote that in title/question before. Not your API? Did you read about [batching](https://blog.apollographql.com/query-batching-in-apollo-63acfd859862)? Simply render list of items, each of them with own query - it should work w/o additional efforts if server support batching - if not ask a new, more detailed question. – xadm Oct 07 '18 at 08:32
  • changes made to question – Jon Patrovi Oct 07 '18 at 12:34
  • It doesn't change anything ;) [API is limited in free version] - use separate user component (each with own query) to render their repos (or to save in array). Making strange workaround isn't worth the effort. – xadm Oct 08 '18 at 10:34
  • missing [link](https://platform.github.community/t/how-to-list-up-all-users-in-github/3070/6) – xadm Oct 08 '18 at 10:44