aPureBase / KGraphQL

Pure Kotlin GraphQL implementation
https://kgraphql.io
MIT License
298 stars 58 forks source link

added Context arguments at DataLoader's loader func. #180

Closed ima9dan closed 1 year ago

ima9dan commented 2 years ago

The context argument did not exist in the BatchLoader's loader function, so I had to add it. However, because the "BatchLoader" type was in "de.nidomiro: KDataLoader" I imported it directly into the project and fixed it. Therefore, it is not a beautiful correction. But I hope it helps someone.

old

typealias BatchLoader<K, R> = suspend (ids: List<K>) -> List<ExecutionResult<R>>

new

typealias BatchLoader<K, R> = suspend (ids: List<K>, ctx:Context) -> List<ExecutionResult<R>>

how to use.

type<Person> {
    dataProperty<Int, Person?>("nextPerson") {
        prepare { person, skipAmount: Int -> person.id + skipAmount }
        loader { ids, ctx ->
            val user = ctx.get<User>()
            ids.map {
                ExecutionResult.Success(people[it-1])
            }
        }
    }
}
jeggy commented 2 years ago

Instead of copying all the files from KDataLoader and modifying them a bit. Could you start with creating a PR in that repository and then when it has been merged update this to use his updated library version?