Skip to content

Latest commit

 

History

History
61 lines (51 loc) · 1.79 KB

File metadata and controls

61 lines (51 loc) · 1.79 KB
name description url github
KGraphQL
KGraphQL is a Kotlin implementation of GraphQL. It provides a rich DSL to set up the GraphQL schema.
aPureBase/KGraphQL

Here's an example on how to create a simple schema based on a kotlin data class plus a property resolver that gets applied onto your class.

data class Article(val id: Int, val text: String)

fun main() {
    val schema = KGraphQL.schema {
        query("article") {
            resolver { id: Int?, text: String ->
                Article(id ?: -1, text)
            }
        }
        type<Article> {
            property<String>("fullText") {
                resolver { article: Article ->
                    "${article.id}: ${article.text}"
                }
            }
        }
    }

    schema.execute("""
        {
            article(id: 5, text: "Hello World") {
                id
                fullText
            }
        }
    """).let(::println)
}

KGraphQL is using coroutines behind the scenes to provide great asynchronous performance.

See KGraphQL docs for more in depth usage.

Ktor Plugin

KGraphQL has a Ktor plugin which gives you a fully functional GraphQL server with a single install function call. Example below shows how to set up a GraphQL server within Ktor and it will give you a GraphQL Playground out of the box by entering localhost:8080/graphql.

fun Application.module() {
  install(GraphQL) {
    playground = true
    schema {
      query("hello") {
        resolver { -> "World!" }
      }
    }
  }
}

You can follow the Ktor tutorial to set up a KGraphQL server with ktor from scratch up.