Oleg Shuliak
01/20/2025, 2:10 PMdata class Context(
val fieldArguments: Map<String, Any>,
val selectionSet: SelectionSet?,
) {
companion object {
fun fromEnv(env: Env): Context {
return Context(
fieldArguments = env.arguments,
selectionSet = env.selectionSet,
)
}
fun getEmptyInstance(): Context {
return Context(
fieldArguments = emptyMap(),
selectionSet = null,
)
}
}
}
which I’m not sure I like due to the functions on companion object.
on the other hand I could implement something like this, using top-level functions:
data class Context(
val fieldArguments: Map<String, Any>,
val selectionSet: SelectionSet?,
)
fun createContext(
env: Env
): BatchLoaderKeyContext {
return Context(
fieldArguments = dataFetchingEnvironment.arguments,
selectionSet = dataFetchingEnvironment.selectionSet
)
}
fun emptyContext(): Context {
return Context(
fieldArguments = emptyMap(),
selectionSet = null
)
}
which example would be more Kotlin way?
thanks in advance!reactormonk
01/20/2025, 2:39 PMKlitos Kyriacou
01/20/2025, 2:41 PMDaniel Pitts
01/20/2025, 4:51 PMoperator fun invoke
on companion object if the initialization I want can't be one via constructor, but I want it to look like a constructor. Sometimes I'll do fun fromFoo
and the like, if the intent is a lot clearer. Top level functions are also okay, though it depends on the size of the namespace and other things.