Skip to content

Gui-Yom/ktor-graphql

Repository files navigation

ktor-graphql

A Ktor plugin exposing a graphql engine to http and websockets.

Features

GraphQL ktor plugin

Exposes the graphql engine as a plugin to your application. Automatically configures a graphql http endpoint based on the specified configuration. Can also add a graphql over websocket endpoint.

Websockets

The plugin supports ktor websockets but the ktor websockets library is loosely coupled. Add the following dependency to use the provided graphqlWS route :

implementation("io.ktor:ktor-server-websockets")

Serialization

The plugin will try to use the ktor content converters when possible but in some places it needs to make calls to jackson directly. Also, kotlinx.serialization is unsupported as a ktor content serializer since it can't de|serialize dynamic content like Map<String, Any>.

Installation

Artifacts are published to Github Packages and Maven Central. With Gradle :

dependencies {
    implementation("io.github.gui-yom:ktor-graphql:0.8.0")
}

Those artifacts are built with jdk 18.

Example

Example usage with http methods (no subscriptions) and websockets :

// For graphql-over-ws support
// You might want to install the Deflate Websocket extension because some client libraries use it by default
// You need to install a content converter
install(WebSockets) {
    contentConverter = JacksonWebsocketContentConverter()
}

install(GraphQLPlugin) {
    graphql(schema)
}

install(ContentNegotiation) {
    jackson {
        registerModule(KotlinModule())
    }
}

routing {
    graphql {
        // Do something before handling graphql like authentication
        // The returned object will be the graphql context
        // return null to prevent processing the request
    }
    graphqlWS {
        // Do something before handling graphql like authentication
        // You get access to the initial payload Map
        // The returned object will be the graphql context
        // return null to prevent processing the request
    }
}

TODO

  • Basic graphql engine configuration and feature
  • Http endpoint POST (only json body)
  • Http endpoint GET
  • Graphql over websocket
  • Subscription support via websocket (basic)
  • Use the serialization provided by ktor (where possible)
  • Customization of ExecutionInput for GraphQLContext and others
  • Complete graphql-ws spec impl
  • Subscription support via SSE