eh3rrera / graphql-java-spring-boot-example

Sample GraphQL server implemented with graphql-java and Spring Boot
MIT License
211 stars 151 forks source link

Make Batch calls instead of getting each Author by Id at a time. #17

Open pedroviniv opened 5 years ago

pedroviniv commented 5 years ago

Right now for each Book, GraphQL is making a new request to the database in order to retrieve the Book Author By it's id, but this could be done in batch to avoid N+1 queries to get a list a books with the authors.

That could be made by implementing a method in AuthorRepository that retrieves a list of authors based in a list a ids. After that it would be necessary to implement a BatchLoader that by a list of keys, gets a list of Authors by delegating the request to the AuthorRepository. Once you've done that you just have to implement a GraphQLContextBuilder, registering a DataLoader that uses the BatchLoader implementation and then, in BookResolver, instead of making the request directly to the AuthorRepository, you could do this request by getting the DataLoader you registered from a DataFetchingEnvironment.

eh3rrera commented 5 years ago

Hi Pedro, thanks, that would be a great improvement.

Right now, I don't have the time to implement it, but I'll do it. However, I don't know if I'll do it as a branch of this repo or as a new project, because, as this project is linked to a tutorial, I would also have to update the tutorial (which would be a bit complicated).