graph-gophers / dataloader

Implementation of Facebook's DataLoader in Golang
MIT License
1.21k stars 75 forks source link

Dynamic Queries #44

Closed vimalloc closed 5 years ago

vimalloc commented 6 years ago

First off, thanks for this awesome library!

I've got a question that might be more generalized to dataloader as a whole, but I would love to know if you have any insight. I'm using a postgres backend for my storage, and in one of my resolvers I may pass in a list of IDs that I'm interested in getting. Currently I'm doing a SELECT * to grab all the fields for those items, even if I may not need all the fields to satisfy the query, but I'm wondering if I can do better.

The naive solution would be to have the leaf node for each resolver use dataloader to load only what was required from the database, but if a query has a lot of fields that are being accessed that probably wont be very efficient, as it would hit the database once for each field I’m accessing.

Is there any way that I can still batch my queries through dataloader, but have the batch function dynamically load only what is specifically being requested for this query? I was thinking maybe I could encode the fields I wanted into the Key object, but I'm not sure if that would let me properly batch things together, and I imagine that might have consequences on the cache.

Thanks!

nicksrandall commented 5 years ago

We made the Key interface an interface for cases like this. You can create your own data structure that allows you to encode all the data you need in your batch function.

nicksrandall commented 5 years ago

Marking this as closed due to inactivity.