Skip to content

Can't use Spring Security annotation when injecting DataLoader and returning a CompletableFuture #343

Closed as not planned
@koenpunt

Description

@koenpunt

I'm having trouble combining DataLoaders with Spring Security;

My scenario;

typealias MerchantDataLoader = DataLoader<UUID, Merchant?>

// MerchantDataLoader
registry.forTypePair(UUID::class.java, Merchant::class.java).registerMappedBatchLoader { merchantIds, _ ->
    Mono.just(merchantService.getMerchants(merchantIds).associateBy { it.id })
}

// ...

@SchemaMapping
@PreAuthorize("@permissionEvaluator.hasPermission(authentication, #merchant.id, 'Merchant', 'read')")
fun dashboardForDate(
    merchant: Merchant,
    @Argument date: LocalDate?,
    merchantLoader: MerchantDataLoader
): CompletableFuture<Dashboard> = merchantLoader.load(merchant.id).thenApply {
    if (it == null) throw EntityNotFoundException()
    Dashboard(date, it)
}

For GraphQL Java being able to dispatch the dataloader the method has to return the CompletableFuture, but for the @PreAuthorize annotation it "must return an instance of org.reactivestreams.Publisher (i.e. Mono / Flux) or the function must be a Kotlin coroutine function".

I've also tried making it a suspend fun, while still returning the CompletableFuture, but then I get an error like does not match the type of the source Object 'class java.util.concurrent.CompletableFuture', which makes sense because my controller is not expecting the CompletableFuture.

Is my assumption correct, or is there a way around this that I don't know about?

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