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

jvmMain.graphql.GraphRoute.kt Maven / Gradle / Ivy

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

import io.fluidsonic.graphql.*
import io.fluidsonic.json.*
import io.fluidsonic.raptor.ktor.*
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import kotlin.system.*
import org.slf4j.*


internal class GraphRoute(
	internal val system: GraphSystem,
) {

	suspend fun handle(call: ApplicationCall) {
		// TODO rewrite JSON parsing
		// TODO disallow mutation for GET

		val context = DefaultRaptorGraphContext(
			parent = call.raptorContext,
			system = system
		)

		val query: String
		val operationName: String?
		val variableValues: Map

		if (call.request.contentType().match(ContentType("application", "graphql"))) {
			query = call.receiveText()
			operationName = null
			variableValues = emptyMap()
		}
		else { // TODO check contentType is json or not specified
			val json = JsonParser.default.parseMap(call.receiveText())

			query = json["query"] as String
			operationName = json["operationName"] as String?
			@Suppress("UNCHECKED_CAST")
			variableValues = (json["variables"] as Map?).orEmpty()
		}

		val result: Map
		val time = measureTimeMillis {
			result = system.execute(
				documentSource = GDocumentSource.of(query, name = "query"),
				operationName = operationName,
				variableValues = variableValues,
				context = context
			)
		}
		// TODO
		LoggerFactory.getLogger(GraphRoute::class.java).info("Execution took ${time}ms")

		call.respondText(
			text = JsonSerializer.default.serializeValue(result),
			contentType = ContentType.Application.Json.withCharset(Charsets.UTF_8)
		)
	}


	internal companion object {

		val propertyKey = RaptorPropertyKey("graph route")
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy