overblog / dataloader-php

DataLoaderPhp is a generic utility to be used as part of your application's data fetching layer to provide a simplified and consistent API over various remote data sources such as databases or web services via batching and caching.
MIT License
199 stars 21 forks source link

DataLoader::await resolves all loaders #60

Open piotrkreft opened 2 years ago

piotrkreft commented 2 years ago

Hi, great job on the lib guys! It really does bring cool possibilities to PHP-driven apps :)

Quite recently I started to look into possibilities to introduce DataLoader to a new kind of API introduced into my project. Whilst it fits really cool for the use case it does have one downside which I managed to resolve but perhaps it could be a good addition to the lib in general.

In the project, I do have a couple of independently sourced loaders for different kinds of data - all of the loaders besides one return promises being resolved on the serialization/normalization process of the API response - and it generates a great result. Code is clean and the amount of backend/database roundtrips is significantly reduced. However, since I do have to resolve one loader's data in the runtime of generation of the response I use the DataLoaderInterface::await method - and the issue is that it resolves ALL the promises from all of the loaders. It's understandable of course that the method exists and it works how it does but in this very case instead of having x(amount of loaders - 1) + y(unique runtime resolutions) I end up with x*y roundtrips.

As a workaround, I prepared a new extending interface implementing the non-static awaitSingle method. In the implementation instead of using Overblog\DataLoader\Promise\Adapter\Webonyx\GraphQL\SyncPromiseAdapter I use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter allowing to resolve only specific loader.

Any thoughts on the topic? I could give it a try and prepare the appropriate implementation if this would sound like something you'd like to have in the solution :)