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

io.javalin.plugin.graphql.GraphQLHandler.kt Maven / Gradle / Ivy

There is a newer version: 4.6.8
Show newest version
package io.javalin.plugin.graphql

import io.javalin.plugin.graphql.graphql.GraphQLRun
import io.javalin.websocket.WsMessageContext
import kotlinx.coroutines.runBlocking

class GraphQLHandler(private val graphQLBuilder: GraphQLPluginBuilder<*>) {


    fun execute(ctx: WsMessageContext) {
        val body = ctx.messageAsClass(Map::class.java)
        val query = body.get("query").toString()
        val variables: Map = getVariables(body)
        val operationName = body.get("operationName")?.toString()
        val context = runBlocking { graphQLBuilder.contextWsFactory.generateContext(ctx) }

        GraphQLRun(graphQLBuilder.getSchema())
            .withQuery(query)
            .withVariables(variables)
            .withOperationName(operationName)
            .withContext(context)
            .subscribe(SubscriberGraphQL(ctx))
    }

    private fun getVariables(body: Map<*, *>) =
        if (body["variables"] == null) emptyMap() else body["variables"] as Map

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy