pgutkowski / KGraphQL

Pure Kotlin GraphQL implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for suspendable property resolvers

jeggy opened this issue · comments

Would be great with suspendable property resolvers. Something like #37. But something that actually works.

I haven't gotten a full overview of how everything in this project works yet, so I failed implementing this. If there is anything I can help out with I would love to.

Would to see this feature as well! So I could use java-dataloader which uses Futures.
The problem is that we can't just call dataloader.dispatchAndJoin in property resolver since the batching won't work.

With suspend resolver we could use await() to solve the problem

I've been thinking about implementing a dataloader directly into this project.

Haven't figured exactly how to archive it yet, but I'm thinking something like this?

...
data class Tree(val id: String, val parentId: String?)
...
type<Tree> {
    // String - defines the key that will be sent from the [prepare] into [loader]
    // List<Tree> - defines the return type that the [loader] is required to return.
    // the loader is then required to return it in a map format like Map<String, List<Tree>>
    dataProperty<String, List<Tree>>("children") {
        // Step 2: This will only be called once.
        loader { keys: List<String> ->
            keys.map{ id -> id to listOf(Tree("SomeId", id)) }.toMap()
        }
    
        // Step 1: This will be called for each node in the list, or only once if it's not a list
        prepare { parent: Tree -> parent.id }
        
        // Step 3(Optional): This will be called as many times as the prepare did, but now holds the loaded values
        resolve { resolved: List<Tree> -> resolved } // This could maybe process something extra and then return some other type instead of List<Tree>
    }
    ...
}

Please come with any ideas how something like this could look like.