Skip to main content

Klerk-graphql

Klerk-graphql generates a GraphQL API for your Klerk application. It is based on GraphQL Java and Ktor.

Installation

Add klerk-graphql to your project:

implementation(platform("dev.klerkframework:klerk-bom:$klerk_version"))
implementation("dev.klerkframework:klerk")
implementation("dev.klerkframework:klerk-graphql")

Note that klerk-bom includes a compatible version of ktor-bom, which means that you don't have to specify versions for ktor components. You can use a newer Ktor Gradle plugin version for build tasks, but your code dependencies will be aligned by the BOM.

For details on how to set up Klerk, see the Klerk documentation.

Install the GraphQL plugin, specifying the schema like this:

embeddedServer(Netty, port = 8080, host = "0.0.0.0", module = {
install(GraphQL) {
schema {
packages = listOf("dev.klerkframework.graphql")
queries = listOf(GenericQuery(klerk, ::graphQlContextProvider))
mutations = listOf(EventMutationService(klerk, ::graphQlContextProvider))
}
}
// remaining configuration
}).start(wait = true)

Create a function to create a context:

suspend fun graphQlContextProvider(graphQlContext: GraphQLContext): Ctx {
// as described in https://klerkframework.dev/docs/building-config/context a Context is always required when
// interacting with Klerk. This function tells Klerk-web how to get this Context.
}

Now use graphql-kotlin-ktor-server as normal:

routing {
graphQLPostRoute()
graphQLGetRoute()
graphiQLRoute()
graphQLSDLRoute()
}

You can now browse to /graphiql to explore the API.

Examples

To learn more how Klerk-graphql can be used, see the examples.