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

bug: GraphQL context not propagated to data loaders #2058

Open
njuro opened this issue Nov 7, 2024 · 0 comments
Open

bug: GraphQL context not propagated to data loaders #2058

njuro opened this issue Nov 7, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@njuro
Copy link

njuro commented Nov 7, 2024

Hi, we are trying to migrate to the new DGS Spring Boot starter (graphql-dgs-spring-graphql-starter). One problem we cannot overcome is this - we have GraphQLContextContributor storing the value into GraphQL context for further usage like this (simplified):

@Component
class EntityContextContributor: GraphQLContextContributor {
    override fun contribute(
        builder: GraphQLContext.Builder,
        extensions: Map<String, Any>?,
        requestData: DgsRequestData?,
    ) {
        builder.put("foo", "some-value")
    }
}

Next we have batch data loader defined like:

    @DgsDataLoader(name = "entities")
    class BatchEntitiesDataLoader(private val dataLoaderExecutor: Executor) : MappedBatchLoaderWithContext<String, String> {
        override fun load(
            keys: Set<String>,
            environment: BatchLoaderEnvironment,
        ): CompletionStage<Map<String, String>> {
            return CompletableFuture.supplyAsync(
                {
                    environment.getContext<GraphQLContext>.get<String>("foo")
                },
                dataLoaderExecutor,
            )
        }
    }

Which is invoked via

    @DgsData(parentType = "Entity", field = "someField")
    fun entitySomeField(dfe: DgsDataFetchingEnvironment): CompletableFuture<String> {
        val loader = dfe.getDataLoader<String, String>(BatchEntitiesDataLoader::class.java)
        return loader.load(dfe.getSource<Entity>().id)
    }

Until now this has worked nicely and we could access the stored context value for key foo in the batch loader, but after switching to the new starter, the loader environment has now an different, empty GraphQL context which doesn't contain the value stored in the context contributor (we made sure the contributor is invoked and actually stores the value to the context it is accessing).

Expected behavior

The GraphQL context is properly propagated to the BatchLoaderEnvironment and we can access the value stored in context contributor.

Actual behavior

The BatchLoaderEnvironment has different, empty GraphQL context.

Steps to reproduce

See above

Is this a bug, or are there some changes required after switching to the new starter?

@njuro njuro added the bug Something isn't working label Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants