All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jvmMain.definitions.RaptorInterfaceGraphDefinitionBuilder.kt Maven / Gradle / Ivy

The newest version!
package io.fluidsonic.raptor

import io.fluidsonic.raptor.graphql.internal.*
import io.fluidsonic.stdlib.*
import kotlin.reflect.*
import kotlin.reflect.jvm.*


@RaptorDsl
public class RaptorInterfaceGraphDefinitionBuilder internal constructor(
	kotlinType: KotlinType,
	name: String,
	private val stackTrace: List,
) : RaptorStructuredGraphTypeDefinitionBuilder(
	kotlinType = kotlinType,
	name = name
) {

	private val fieldDefinitions: MutableList = mutableListOf()


	override fun build(description: String?, additionalDefinitions: Collection) =
		InterfaceGraphDefinition(
			additionalDefinitions = additionalDefinitions,
			description = description,
			fieldDefinitions = fieldDefinitions.ifEmpty { null }
				?: error("At least one field must be defined: field(…)"),
			kotlinType = kotlinType,
			name = name,
			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)
	}


	@OptIn(ExperimentalReflectionOnLambdas::class)
	@RaptorDsl
	@Suppress("NOTHING_TO_INLINE")
	public inline fun  field(
		function: KSuspendFunction2,
		noinline configure: RaptorGraphFieldBuilder.() -> Unit = {},
	) {
		field(name = function.name, type = function.reflect()!!.returnType, configure = configure) // TODO
	}


	@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()
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy