Skip to content

How to let BatchLoader know the columns of current DataFetchingEnvironment? #64

@babyfish-ct

Description

@babyfish-ct

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions