Skip to content

lusingander/kraphql

Repository files navigation

KraphQL

gradle plugin

Kotlin DSL Generator for GraphQL

[GraphQL schema] --(KraphQL)--> [DSL definition .kt file] <-- Call from your code

About

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)
    }

Usage

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.

Sample

See ./src/test/ directory, or see Related projects.

Test codes

./src/test/kotlin/com/github/lusingander/kraphql/test

Schema definitions

./src/test/resources/sdl

Generated Kotlin codes

./src/test/kotlin/com/github/lusingander/kraphql/gen

Related projects