jvmMain.definitions.RaptorInterfaceExtensionGraphDefinitionBuilder.kt Maven / Gradle / Ivy
package io.fluidsonic.raptor
import io.fluidsonic.raptor.graphql.internal.*
import io.fluidsonic.stdlib.*
import kotlin.reflect.*
@RaptorDsl
public class RaptorInterfaceExtensionGraphDefinitionBuilder internal constructor(
private val kotlinType: KotlinType,
private val stackTrace: List,
) {
private val fieldDefinitions: MutableList = mutableListOf()
internal fun build() =
InterfaceExtensionGraphDefinition(
additionalDefinitions = emptyList(),
fieldDefinitions = fieldDefinitions.ifEmpty { null }
?: error("At least one field must be defined: field(…)"),
kotlinType = kotlinType,
stackTrace = stackTrace
)
@RaptorDsl
public inline fun field(
name: String,
noinline configure: RaptorGraphFieldBuilder.() -> Unit,
) {
field(name = name, type = typeOf(), configure = configure)
}
@RaptorDsl
@Suppress("NOTHING_TO_INLINE")
public inline fun field(
property: KProperty1,
noinline configure: RaptorGraphFieldBuilder.() -> Unit = {},
) {
field(name = property.name, type = property.returnType, configure = configure) // TODO
}
@RaptorDsl
@Suppress("NOTHING_TO_INLINE")
public inline fun field(
function: KSuspendFunction2,
noinline configure: RaptorGraphFieldBuilder.() -> Unit = {},
) {
field(name = function.name, type = function.returnType, configure = configure)
}
@RaptorDsl
public fun field(
name: String,
type: KType,
configure: RaptorGraphFieldBuilder.() -> Unit,
) {
if (fieldDefinitions.any { it.name === name })
error("Cannot define multiple fields named '$name'.")
fieldDefinitions += RaptorGraphFieldBuilder(
kotlinType = KotlinType.of(
type = type,
containingType = kotlinType,
allowMaybe = false,
allowNull = true,
allowedVariance = KVariance.OUT,
requireSpecialization = true
),
name = name,
parentKotlinType = kotlinType,
stackTrace = stackTrace(skipCount = 1)
)
.apply(configure)
.build()
}
}