Skip to content

Make Context and Execution.Node accessible via Receiver parameter #145

@NathanPB

Description

@NathanPB

Hello, I see that currently, to access the Context or Execution.Node we have to do something like:

resolver { foo: String, ctx: Context -> /* ... */ }

which implies that we need to add rules to the schema parser to ignore those classes when kgraphql is reflecting the FunctionWrapper<*>'s arguments:

https://github.com/aPureBase/KGraphQL/blob/main/kgraphql/src/main/kotlin/com/apurebase/kgraphql/schema/structure/SchemaCompilation.kt#L162-L182

So, if I'm hypothetically working on #68, and I want to add a new type that should not be introspected (let's say List<Throwable>), I should create a wrapper class (like Context is just a wrapper for Map<Any, Any>), ignore this class in the schema introspection, and inject an object of this class into the FunctionWrapper's arguments when invoking it... Am I Right?

Kotlin supports Receiver Functions (https://kotlinlang.org/docs/lambdas.html#function-types), which, are, for instance, A.(B)->C where in this lambda, A will be the this context of the execution.

So, why instead of injecting Contexts and Execution.Node's in the Resolvers, we create a ExecutionScope class and make this class the receiver of the resolvers?

class ExecutionScope(val context: Context, val node: Execution.Node, val errors: MutableList<Throwable>)

// This, obviously adapted to work with ``FunctionWrapper``
fun <A, B> resolver(ExecutionScope.(A)->B: body) { ... }

resolver { foo: String ->
   /* context, node and errors are accessible here. */
   /* We can do something like errors += Throwable("bondia") */ 
}

This will make it much easier and cleaner to work with contexts and other runtime wrappers.

I have been thinking about this while reading the code, but I did not attempt to do anything yet, do you @jeggy thinks that this issue makes sense? Any advices?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions