How are people generally handling getting a list o...
# graphql
j
How are people generally handling getting a list of Parent objects that can contain nested children (eg adding a
allUsers()
query to the nested fields documentation page schema, and having a client request
allUsers() {photos(1)}
to get the first photo for each user)? Exposing the nested children as a function is really cool from a simplicity of writing code perspective, but ends up executing one fetch per Parent (exposing it as a suspend function at least runs them concurrently). Is looking at the
DataFetchingEnvironment
and then performing different fetch logic based on the selectionSet the best way to try to optimize the fetches? Or is there another/better way of thinking about this use case?
d
Well it really depends on your use case
Instead of 'photos[0]' I'd expose it under separate field - maybe something like highlight/primary/lead image or something like that
So users requesting single image vs a list would resolve different fields
j
Yeah, I just chose that to minimally change the example schema -- it's more the
allUsers() { leadPhoto }
case that I'm interested in populating via constant number of queries instead of O(n) queries.
d
In this case it really depends on your underlying data sources
If all users = one call and then you do the photo per user
You might want to look into data loader and batching all photo requests
j
cool, thanks for the data loader pointer, will look at that. also ran across https://www.graphql-java.com/blog/deep-dive-data-fetcher-results/ just now, that might help too?
d
Somewhat it really depends how you fetch the data
eg if you can fetch users and photos and parallel then yeah
If photos depend on user then probably not
j
very belated follow up here, but in case anyone ever has similar questions: data loader was absolutely the right way to approach this for my use case / data sources. Ended up accessing
dfe.getDataLoader().load()
from a function of the
User
model object and returning that
CompleteableFuture<Photo>
, which was not completely obvious from the examples / documentation, but also wasn't impossible to figure out.