graphql-java-spring-boot-example icon indicating copy to clipboard operation
graphql-java-spring-boot-example copied to clipboard

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

Open pedroviniv opened this issue 6 years ago • 1 comments

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.

pedroviniv avatar Aug 21 '19 04:08 pedroviniv

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).

eh3rrera avatar Aug 25 '19 23:08 eh3rrera