You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: KGraphQL is a Kotlin implementation of GraphQL. It provides a rich DSL to set up the GraphQL schema.
4
+
url: https://kgraphql.io/
5
+
github: aPureBase/KGraphQL
6
+
---
7
+
8
+
Here's a simple example on how to create a simple schema based on a kotlin data class plus a property resolver that gets applied onto your class.
9
+
10
+
```kotlin
11
+
data classArticle(valid:Int, valtext:String)
12
+
13
+
funmain() {
14
+
val schema =KGraphQL.schema {
15
+
query("article") {
16
+
resolver { id:Int?, text:String->
17
+
Article(id ?:-1, text)
18
+
}
19
+
}
20
+
type<Article> {
21
+
property<String>("fullText") {
22
+
resolver { article:Article->
23
+
"${article.id}: ${article.text}"
24
+
}
25
+
}
26
+
}
27
+
}
28
+
29
+
schema.execute("""
30
+
{
31
+
article(id: 5, text: "Hello World") {
32
+
id
33
+
fullText
34
+
}
35
+
}
36
+
""").let(::println)
37
+
}
38
+
```
39
+
40
+
KGraphQL is using coroutines behind the scenes to provide great asynchronous performance.
41
+
42
+
See [KGraphQL docs](https://kgraphql.io/Installation/) for more in depth usage.
43
+
44
+
## Ktor Plugin
45
+
46
+
KGraphQL has a Ktor plugin which gives you a fully functional GraphQL server with a single [install](https://ktor.io/docs/zfeatures.html) function call. Example below shows how to set up a GraphQL server within Ktor and it will give you a [GraphQL Playground](https://github.com/graphql/graphql-playground) out of the box by entering `localhost:8080/graphql`.
47
+
48
+
```kotlin
49
+
fun Application.module() {
50
+
install(GraphQL) {
51
+
playground =true
52
+
schema {
53
+
query("hello") {
54
+
resolver { ->"World!" }
55
+
}
56
+
}
57
+
}
58
+
}
59
+
```
60
+
61
+
You can follow the [Ktor tutorial](https://kgraphql.io/Tutorials/ktor/) to set up a KGraphQL server with ktor from scratch up.
0 commit comments