GraphQL query for list of items

I'm starting to look into GraphQL. It's got a lot of hype at the moment and there is more and more pressure to start playing with it in production applications.

It's got a bunch of advantages and a quick play shows me that it's not just hype. There is plenty of information available on why you would want to use it, if you're looking for that kind of thing I'd suggest starting with https://graphql.org/.

One limitation that I have come across when I was looking to implement a small application with some colleagues was that there doesn't seem to be a way to query for a known list of items without making the server support it. I'll use GitHub's GraphQL API as an example.

Lets say I have a curated list of repositories that I would like to query for as far as I can see there is no way to enumerate over that list within the GraphQL syntax. It is simple enough to get a list of repositories for a given user but I haven't been able to work out (yet?) how to provide a list to the query and have it return me an array of repository using only GraphQL . What I wanted was something like this:

1:  query($myRepos:[String!]!){  
2:      $myrepos{  
3:          repository(owner:"kleeut"){  
4:              ... repoData       
5:          }  
6:      }  
7:  }  
8:  fragment repoData on Repository {  
9:      name  
10:      languages(first: 10) {  
11:          nodes {  
12:              name  
13:          }  
14:      }  
15:  }  
16:  #Query Data  
17:  {  
18:      "myRepos":["kleeut.github.io", "techradar"]  
19:  }  

The solution I have come up with is to generate the query dynamically using your programming language of choice. Rather than passing the list into the query as a parameter use write a function to create a query that aliases each of the items in the list as part of the query.  Don't forget to sanitise the code inputs to make sure that your aliases are valid. The items can then be extracted from the object using the original list that was used to create the query.

My working GraphQL query syntax example that would be output by the function:

1:  query {  
2:      kleeutgithubio: repository(owner: "kleeut", name: "kleeut.github.io") {  
3:          ...repoData  
4:      }  
5:      techradar: repository(owner: "kleeut", name: "techradar") {  
6:          ...repoData  
7:      }  
8:  }  
9:  fragment repoData on Repository {  
10:      name  
11:      languages(first: 10) {  
12:          nodes {  
13:              name  
14:          }  
15:      }  
16:  }  

It's something that I was hoping that GraphQL would have a built in work around for. Even though it doesn't by using this method I can still combine n round trips to the server into one.


P.S.
This is the best solution that I can find for this at the moment. If you've got a better solution, there is always a chance that I've missed something, or maybe the world has moved on since I wrote this, then let me know so I can update this and learn something. 


Comments

Popular posts from this blog

Solving `Empty reply from server` in DotNet Core

Testing functions that use local storage with Jest

Can't resolve DNS Aws