Last active
September 11, 2018 16:55
-
-
Save soudmaijer/3fc6d32a2dad10960556a5e6ce0a5aca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ParentType(val id: Long) | |
class ChildType | |
class ChildTypeBatchApi { | |
fun load(keys: List<Long>): List<ChildType> = mutableListOf() | |
} | |
val childTypeBatchApi = ChildTypeBatchApi() | |
// Example BatchLoader | |
var batchLoader: BatchLoader<Long, ChildType> = BatchLoader { keys -> | |
CompletableFuture.supplyAsync<List<ChildType>> { childTypeBatchApi.load(keys) } | |
} | |
// How to register all the dataloaders zo graphql-java-tools can use them for injection ? | |
var childDataLoader = DataLoader<Long, ChildType>(batchLoader) | |
@Component | |
class ChildResolver() : GraphQLResolver<ParentType> { | |
// Optionally, could inject the DataLoader bean, but more clear adding it as a parameter in the resolver function | |
// val childDataLoader: DataLoader<Long, ChildType> | |
// Inject the DataLoader for this specific type (using reflection) | |
fun childField(parentItem: ParentType, childDataLoader: DataLoader<Long, ChildType>): CompletableFuture<ChildType> { | |
// Should return the CompletableFuture for the DataLoader to do its work | |
return childDataLoader.load(parentItem.id) | |
.thenApply { it } | |
.exceptionally { e -> | |
throw Exception(e.message, e) | |
} | |
} | |
} | |
// Need to invoke dispatchAndJoin after processing this level in the graph | |
childDataLoader.dispatchAndJoin(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment