KGraphQL is a pure Kotlin implementation of a code-first GraphQL server with focus on a rich and easy-to-use DSL that leverages existing code to set up the schema.
data class Article(val id: Int, val text: String)
suspend fun main() {
val schema = KGraphQL.schema {
query("article") {
resolver { id: Int?, text: String ->
Article(id ?: -1, text)
}
}
type<Article> {
property("fullText") {
resolver { article: Article ->
"${article.id}: ${article.text}"
}
}
}
}
schema.execute("""
{
article(id: 5, text: "Hello World") {
id
fullText
}
}
""".trimIndent()).let(::println)
// {"data":{"article":{"id":5,"fullText":"5: Hello World"}}}
}KGraphQL has built-in support for Ktor but works with any underlying server. As of now, it has been used in production at Europe's leading consumer electronics retailer since 2019, stitching together multiple backend services with about 100 types combined.
Maven Dependency
<dependency>
<groupId>de.stuebingerb</groupId>
<artifactId>kgraphql</artifactId>
<version>${kgraphql.version}</version>
</dependency>Gradle Dependency
repositories {
mavenCentral()
}
dependencies {
implementation("de.stuebingerb:kgraphql:$kgraphql_version")
}KGraphQL was initially created by Paweł Gutkowski and then continued by Jógvan Olsen. Huge thanks to both of them for starting this amazing library!
See the documentation for a more detailed explanation of the library.
See Contributing.
Working examples are located in the examples folder. Every example is its own project, separated from the library build. To build and/or run it, move into the folder of the example (e.g. ktor) and execute Gradle tasks from there.
The versioning from 1.0.0 on will follow Semantic Versioning. Until then, expect breaking changes as required, although the general vision is to suck less with each release.
KGraphQL aims to provide full support for the September 2025 Edition of the GraphQL specification with some additions for schema stitching.
See issues for where support is currently lacking.
KGraphQL is Open Source software released under the MIT license.