spring-projects / spring-projects/spring-graphql

Add Kotlin extension for registering suspend functions in BatchLoaderRegistry

Open
#1,433 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
1.6k
Forks
336
PR merge metrics
No merged PRs in 30d

Description

Hey!

TLDR: add kotlin extension function to BatchLoaderRegistry to simplify registration of a suspend function.

Request

If you want to register a "mapped batch loader" in Spring GraphQL, you must/can provide a function that returns a Mono(source).

To register a suspend function, you can provide this function as: mono() { /*suspend/* } .
There is a problem with this approach: it fails to properly propagate/restore ThreadLocalAccessor values (and also tracing information I think).

You can reproduce this if you access SecurityContextHolder.getContext().authentication before and after the mono call.
If authentication used to be non-null before the mono call (e.g. TestingAuthenticationToken), it will suddenly be null after it.

In Spring Framework 7, a coroutine element called PropagationContextElement() was introduced to solve this (docs).

It would be nice to have kotlin extensions to register a suspend function in the correct way:

fun <K, V> BatchLoaderRegistry.RegistrationSpec<K, V>.registerSuspendMappedBatchLoader(
    dispatcher: CoroutineDispatcher = Executors.newVirtualThreadPerTaskExecutor().asCoroutineDispatcher(),
    batchLoader: suspend (keys: Set<K>, env: BatchLoaderEnvironment) -> Map<K, V>,
) {
    this.registerMappedBatchLoader { keys: Set<K>, env: BatchLoaderEnvironment ->
        mono(dispatcher + PropagationContextElement()) {
            batchLoader(keys, env)
        }
    }
}
fun <K, V> BatchLoaderRegistry.RegistrationSpec<K, V>.registerSuspendMappedBatchLoader(
    dispatcher: CoroutineDispatcher = Dispatchers.Default,
    batchLoader: suspend (keys: Set<K>) -> Map<K, V>,
) {
    this.registerMappedBatchLoader { keys: Set<K>, _: BatchLoaderEnvironment ->
        mono(dispatcher + PropagationContextElement()) {
            batchLoader(keys)
        }
    }
}

NOTE: I'm not sure about the default value of the dispatcher though.

Could we add such an extension function to Spring GraphQL?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading BatchLoaderRegistry registration and the linked DefaultBatchLoaderRegistry implementation, then review Spring Framework's PropagationContextElement coroutine documentation. Reproduce the SecurityContextHolder authentication loss around mono and compare both proposed suspend-loader signatures. Done means the appropriate Kotlin extensions register suspend mapped batch loaders with context propagation and a settled dispatcher choice.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, kotlin, spring
Domain
backend-api-design
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.