A Ktor plugin exposing a graphql engine to http and websockets.
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.
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")
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>
.
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 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
}
}
- 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