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

jvmMain.graphql.coercion.EnumCoercer.kt Maven / Gradle / Ivy

There is a newer version: 0.26.0
Show newest version
package io.fluidsonic.raptor.graphql.internal

import io.fluidsonic.graphql.*
import io.fluidsonic.raptor.*


internal object EnumCoercer : GNodeInputCoercer, GOutputCoercer, GVariableInputCoercer {

	private fun GInputCoercerContext.coerceInput(input: Any?): Any {
		val context = checkNotNull(execution.raptorContext)
		val raptorType = (type as GEnumType).raptorType as EnumGraphType

		val inputScope = object : RaptorGraphInputScope, RaptorGraphScope by context { // TODO improve

			override fun invalid(details: String?): Nothing =
				[email protected](details = details)
		}

		return (input as? String)
			?.let { raptorType.parse(inputScope, it) }
			?: invalid()
	}


	override fun GNodeInputCoercerContext.coerceNodeInput(input: Any): Any =
		coerceInput((input as? GEnumValue)?.name)


	override fun GOutputCoercerContext.coerceOutput(output: Any): Any {
		val context = checkNotNull(execution.raptorContext)
		val raptorType = (type as GEnumType).raptorType as EnumGraphType

		val outputScope = object : RaptorGraphOutputScope, RaptorGraphScope by context {}  // TODO improve

		return raptorType.serialize(outputScope, output)
	}


	override fun GVariableInputCoercerContext.coerceVariableInput(input: Any): Any =
		coerceInput(input)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy