graphql-java / java-dataloader

A Java 8 port of Facebook DataLoader

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to let BatchLoader know the columns of current DataFetchingEnvironment?

babyfish-ct opened this issue · comments

I've created a demo project about graphql-java by kotlin: https://github.com/babyfish-ct/grahpql-java-example. but I met a problem and I don't know how to do it better

For the columns that are neither primary keys nor foreign keys of any tables, this demo only selects the columns that's required by GraphQL. that means the BatchLoader need to know the name list of selected columns of the current DataFetchingEnvironment.

Not like the global context, DataFetchingEnvironment objects for different asociation data loading are not same, so it's impossible to use BatchLoaderWithContext.

So I creatd the class

data class LoaderKey<K>(
    val value: K,
    val propNames: Collection<String>
)

in https://github.com/babyfish-ct/grahpql-java-example/blob/master/src/main/kotlin/org/frchen/graphql/example/loader/Common.kt. The first property 'value' is the DataKey of DataLoader, the second property 'propNames' is the collection "env.selectionSet.get().keys"(env is DataFetchingEnvironment object) that is used to specify the select name list of columns for current assocation data loading. please see the functions "loadOptionalReferenceAsync", "loadRequiredReferenceAsync" and "loadListAsync" in the same file.

This solution workds but it's stupid, because different LoaderKey objects often have the same propNames, this is wasteful.

How can I do it better?