Skip to content

Allow dispatch() caller to get stats back. #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 9, 2020
Merged

Allow dispatch() caller to get stats back. #56

merged 2 commits into from
Feb 9, 2020

Conversation

kdreibel
Copy link
Contributor

For context: I'm working on a change to allow better composability of DataLoaders. Right now, you can't nest DataLoader.load().thenApply() within another similar call. When a GraphQL field resolution expects a result type V, you can return a CompletableFuture<T> (e.g. due to a DataLoader), but you can't return a CompletableFuture<CompletableFuture<T>> and so on. So, instead of being able to chain DataLoaders together, you have to come up with a specialized DataLoader to calculate the one specific field you want. I'm trying to make it so that you can chain DataLoaders together.

The bulk of the change is going to be in graphql-java, in another PR. But that change is going to need insight into what happened when a dispatchAll was done in the DataLoaderRegistry. That's what this PR is about.

Actual change: Added dispatchWithCounts which now returns a DispatchResult which includes the previously returned CompletableFuture<List<X>>, plus a count of how many entries (keys) were dispatched. No API change to existing methods. (Aside: I did also think about changing the CompletableFuture<List<X>> to a List<CompletableFuture<X>>, which would accomplish the same thing. But that would change the API, so I opted for this approach instead.)

@kdreibel
Copy link
Contributor Author

There seems to be discussion of this same issue in #46 (comment) .

*
* @return the promise of the queued load requests and the number of entries dispatched.
*/
public DataLoaderHelper.DispatchResult<V> dispatchWithCounts() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am going to accept this PR and fix it up but I will put the changes I will make here for you to see.

Here would would not return a nest class from a helper - I would make it a full class since it now API

@@ -134,7 +134,16 @@ Object getCacheKey(K key) {
loaderOptions.cacheKeyFunction().get().getKey(key) : key;
}

CompletableFuture<List<V>> dispatch() {
public static class DispatchResult<X> {
public final CompletableFuture<List<X>> futureList;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know I know Java should have data classes so we dont have to write so much code

but it does - so I would (and will) use POJO patterns with getters.

@bbakerman bbakerman merged commit 0b54f86 into graphql-java:master Feb 9, 2020
@bbakerman bbakerman mentioned this pull request Feb 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants