jcrygier / graphql-jpa

JPA Implementation of GraphQL (builds on graphql-java)
MIT License
165 stars 46 forks source link

Add an option to avoid lazy loading #16

Open aesteve opened 7 years ago

aesteve commented 7 years ago

Hello, and thanks for your amazing work.

I tried graphql-jpa on very simple projects and it worked simply, out-of-the-box, which is really neat !

Something I'm concerned about though, is lazy loading.

GraphQL is very good for the client to tell "I want that piece of information, and only that", which, translated into the ORM-world could mean : "I want that entity, this relation to that other entity, but not this other one".

What would be awesome, would be to add an ability, when creating the GraphQLQuery, to generate an "adaptative" query, using FETCH JOIN to fetch all the entities needed at once, avoiding lazy loading when sending the response to the client.

Not sure how to implement it, but by parsing the client's query (and the field he is asking for), and analyzing the annotations on the fields of the entity returned, I guess it should be possible.

What do you think ? Is this something you're thinking about ? Working on ? Is it too difficult ? Out of scope ?

Thanks a lot !

jwgmeligmeyling commented 7 years ago

Currently it seems all the to-one relationships are eagerly fetch joined. This could be a point to implement to-many fetching as well. Keep in mind that fetch joining multiple to-many relationships can cause all sorts of funky behaviour, see also: http://blog.eyallupu.com/2010/06/hibernate-exception-simultaneously.html . It just won't work with unindexed lists. Also, the cross product may become quite large and sometimes its just more efficient to run a couple of queries instead. Another idea could be to provide an EntityGraph to the query run by graphql-jpa or to use any of the fetch strategies that your JPA provider provides.