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

org.ryoframework.exposition.adapters.DefaultGraphQLEngineImpl.kt Maven / Gradle / Ivy

The newest version!
package org.ryoframework.exposition.adapters

import graphql.ExceptionWhileDataFetching
import graphql.ExecutionInput
import graphql.GraphQL
import org.ryoframework.exposition.graphql.GraphQLEngine
import org.ryoframework.exposition.graphql.GraphQLRequest
import org.ryoframework.exposition.graphql.GraphQLResponse


internal class DefaultGraphQLEngineImpl(private val graphQL: GraphQL) : GraphQLEngine {

    override fun execute(request: GraphQLRequest): GraphQLResponse {

        val executionInput = ExecutionInput.newExecutionInput().query(request.query)
            .operationName(request.operationName)
            .variables(request.variables)
            .build()

        val executionResult = graphQL.execute(executionInput)

        val data = executionResult.getData>()
        val errors = executionResult.errors.map {
            if (it is ExceptionWhileDataFetching) {
                mapOf("message" to (it.exception.cause?.message ?: it.exception!!.message!!))
            } else {
                mapOf("message" to it.message!!)
            }
        }

        if (executionResult.errors.isNullOrEmpty()) {
            return GraphQLResponse(data)
        }

        return GraphQLResponse(emptyMap(), errors)
    }

    // *****************************************************************************************************************




}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy