graphql-kotlin icon indicating copy to clipboard operation
graphql-kotlin copied to clipboard

feat: support returning CompletableFuture FederatedTypeResolver

Open samuelAndalon opened this issue 2 years ago • 0 comments

:pencil: Description

With the idea of proving support for returning a CompletableFuture in FederatedTypeResolver a new sealed interface is added

sealed interface TypeResolver {
    val typeName: String
}

this will be the basic interface of 2 interfaces

  1. The currently supported FederatedTypeResolver, with support of suspendable function to be executed in a coroutine
interface FederatedTypeResolver<out T> : TypeResolver {
    override val typeName: String
    suspend fun resolve(
      environment: DataFetchingEnvironment, 
      representations: List<Map<String, Any>>
    ): List<T?>
}
  1. A new interface called FederatedTypePromiseResolver that will provide support to return a CompletableFuture allowing compatibility with a DataLoader
interface FederatedTypePromiseResolver<T> : TypeResolver {
    fun resolve(
      environment: DataFetchingEnvironment, 
      representations: List<Map<String, Any>>
    ): CompletableFuture<List<T?>>
}

Clients will have the freedom to decided which interface to use for their resolvers and graphql-kotlin will be smart enough to decide how to execute them.

The execution logic will be capable of executing suspend resolvers or promise resolvers concurrently, specially useful if you have support for data loaders in only certain entity

example representations for multiple types, request to _entities field

UserResolver implementing FederatedTypeResolver PromiseResolver implementing FederatedTypePromiseResolver

[
  { "__typename": "User", "userId": 123 },
  { "__typename": "User", "userId": 456 },
  { "__typename": "Author", "authorId": 1 },
  { "__typename": "Author", "authorId": 2 },
]

This might not be a breaking change after all, given that we will keep supporting the FederatedTypeResolver and we are just modifying the execution logic of resolvers of that interface and the new interface FederatedTypePromiseResolver

:link: Related Issues

https://github.com/ExpediaGroup/graphql-kotlin/issues/1506

samuelAndalon avatar Aug 12 '22 02:08 samuelAndalon