Skip to content
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

Feature Request - Pass DataFetchingEnvironment to the BatchLoader. #26

Closed
budjb opened this issue Aug 25, 2018 · 3 comments
Closed

Feature Request - Pass DataFetchingEnvironment to the BatchLoader. #26

budjb opened this issue Aug 25, 2018 · 3 comments
Milestone

Comments

@budjb
Copy link

budjb commented Aug 25, 2018

The dataloader library seems to give me most of the functionality I'm looking for when trying to batch my DB queries for a layer of a graphql query. I am working with very large documents in the database I'm querying, and I'd rather optimize my queries to the database so that I only request the information from the database necessary to fulfill the graphql query.

I'm able to determine the required set of data based on the DataFetchingEnvironment that's passed to the DataFetcher, but I see no way to pass that information downstream to the BatchLoader. Having that object, or any other custom object containing the contextual data I need for that layer of the query, would enable the optimization I'm looking for.

@bbakerman
Copy link
Member

bbakerman commented Aug 26, 2018

I have just added a capability into the library to allow context to be provided to the batch loader function

BatchLoaderEnvironment batchLoaderEnvironment = BatchLoaderEnvironment.newBatchLoaderEnvironment()
                .context(SecurityCtx.getCallingUserCtx()).build();

        DataLoaderOptions options = DataLoaderOptions.newOptions()
                .setBatchLoaderEnvironmentProvider(() -> batchLoaderEnvironment);

        BatchLoaderWithContext<String, String> batchLoader = new BatchLoaderWithContext<String, String>() {
            @Override
            public CompletionStage<List<String>> load(List<String> keys, BatchLoaderEnvironment environment) {
                SecurityCtx callCtx = environment.getContext();
                return callDatabaseForResults(callCtx, keys);
            }
        };

        DataLoader<String, String> loader = DataLoader.newDataLoader(batchLoader, options);

Note it is NOT per dataloader.load(key) call because of the batching nature. If I load keys A,B,C each with a context object then what context do I sent to the combined batch function?

Rather is is a provider model on the DataLoader options. The provider of context is called when the batch load function is dispatched.

In the case of graphql you will need to have a "mutable" provider that you "set" target fields into.

Let us know if this helps you.

@bbakerman
Copy link
Member

This has changed since I post the above. See the readme for more details

@bbakerman
Copy link
Member

This has been added in 2.1.1

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

No branches or pull requests

2 participants