com.auritylab.graphql.kotlin.toolkit.spring.controller.AbstractController.kt Maven / Gradle / Ivy
package com.auritylab.graphql.kotlin.toolkit.spring.controller
import com.auritylab.graphql.kotlin.toolkit.spring.api.GraphQLInvocation
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import java.util.concurrent.CompletableFuture
import org.springframework.web.context.request.WebRequest
abstract class AbstractController(
protected val objectMapper: ObjectMapper,
private val invocation: GraphQLInvocation
) {
/**
* Will execute the given [operation] using the [invocation].
*/
protected fun execute(
operation: Operation,
request: WebRequest
): CompletableFuture =
invocation.invoke(
GraphQLInvocation.Data(operation.query, operation.operationName, operation.variables),
request
).thenApply { it.toSpecification() }
/**
* Represents a GraphQL operation with a [query], [operationName] (optional) and [variables] (optional).
*/
protected data class Operation(
val query: String = "",
val operationName: String?,
val variables: Map?
)
/**
* Will parse the given [input] into [T]. If the given [input] can not be parsed into [T] `null` will be returned.
*/
protected inline fun parse(input: String): T? =
try {
objectMapper.readValue(input)
} catch (ex: Exception) {
null
}
}