Kotlin DSL Generator for GraphQL
[GraphQL schema] --(KraphQL)--> [DSL definition .kt file] <-- Call from your code
For example, given the following schema:
type Query {
books: [Book!]!
authors: [Author!]!
}
type Book {
title: String!
author: Author!
}
type Author {
firstName: String!
lastName: String!
books: [Book!]!
}
From here, KraphQL generates Kotlin code for the DSL.
Then, we can use it to write the following DSL:
@Test
fun query() {
val q = query {
books {
title
author {
firstName
lastName
}
}
}
val expected = """
query {
books {
title
author {
firstName
lastName
}
}
}
""".trimIndent()
val actual = formatQuery(q.toString()) // formatQuery just makes it look good
assertThat(actual).isEqualTo(expected)
}
KraphQL is available as Gradle plugin.
Add the following to build.gradle:
plugins {
id "com.github.lusingander.kraphql-plugin" version "0.0.4"
}
kraphql {
input = "./schema.graphql"
output = "./output.kt"
packageName = "com.github.example"
}
Then, when you run the kraphqlGenerateDsl
task, it will output the Kotlin DSL file.
For an example, see kraphql-github.
See ./src/test/
directory, or see Related projects.
./src/test/kotlin/com/github/lusingander/kraphql/test
./src/test/kotlin/com/github/lusingander/kraphql/gen
- lusingander/kraphql-github
- Kotlin DSL for GitHub GraphQL API (GitHub API v4)