Skip to content

Commit 2e4c1eb

Browse files
authored
Merge pull request graphql#965 from dariuszkuc/graphql_kotlin
update documentation on graphql-kotlin
2 parents 4dbab42 + 70c26cf commit 2e4c1eb

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
name: graphql-kotlin
3+
description: A set of libraries for running GraphQL client and server in Kotlin.
4+
url: https://github.com/ExpediaGroup/graphql-kotlin/
5+
github: ExpediaGroup/graphql-kotlin
6+
---
7+
8+
GraphQL Kotlin provides a set of lightweight type-safe GraphQL HTTP clients. The library provides Ktor HTTP client and Spring WebClient based reference implementations as well as allows for custom implementations using other engines. Type-safe data models are generated at build time by the GraphQL Kotlin Gradle and Maven plugins.
9+
10+
To generate Ktor based GraphQL client add following to your Gradle build file:
11+
12+
```kotlin
13+
// build.gradle.kts
14+
import com.expediagroup.graphql.plugin.generator.GraphQLClientType
15+
import com.expediagroup.graphql.plugin.gradle.graphql
16+
17+
plugins {
18+
id("com.expediagroup.graphql") version $latestGraphQLKotlinVersion
19+
}
20+
21+
dependencies {
22+
implementation("com.expediagroup:graphql-kotlin-ktor-client:$latestGraphQLKotlinVersion")
23+
}
24+
25+
graphql {
26+
client {
27+
// target GraphQL endpoint
28+
endpoint = "http://localhost:8080/graphql"
29+
// package for generated client code
30+
packageName = "com.example.generated"
31+
clientType = GraphQLClientType.KTOR
32+
}
33+
}
34+
```
35+
36+
By default, GraphQL Kotlin plugin will look for query files under `src/main/resources`. Given `helloWorld: String!` query we can add following `HelloWorldQuery.graphql` sample query to our repo:
37+
38+
```graphql
39+
query HelloWorldQuery {
40+
helloWorld
41+
}
42+
```
43+
44+
Plugin will generate following client code:
45+
46+
```kotlin
47+
package com.example.generated
48+
49+
import com.expediagroup.graphql.client.GraphQLKtorClient
50+
import com.expediagroup.graphql.types.GraphQLResponse
51+
import kotlin.String
52+
53+
const val HELLO_WORLD_QUERY: String = "query HelloWorldQuery {\n helloWorld\n}"
54+
55+
class HelloWorldQuery(
56+
private val graphQLClient: GraphQLKtorClient<*>
57+
) {
58+
suspend fun execute(requestBuilder: HttpRequestBuilder.() -> Unit = {}): GraphQLResponse<HelloWorldQuery.Result> =
59+
graphQLClient.execute(HELLO_WORLD_QUERY, "HelloWorldQuery", null, requestBuilder)
60+
61+
data class Result(
62+
val helloWorld: String
63+
)
64+
}
65+
```
66+
67+
We can then execute the client
68+
69+
```kotlin
70+
package com.example.client
71+
72+
import com.expediagroup.graphql.client.GraphQLKtorClient
73+
import com.expediagroup.graphql.generated.HelloWorldQuery
74+
import kotlinx.coroutines.runBlocking
75+
import java.net.URL
76+
77+
fun main() {
78+
val client = GraphQLKtorClient(url = URL("http://localhost:8080/graphql"))
79+
val helloWorldQuery = HelloWorldQuery(client)
80+
runBlocking {
81+
val result = helloWorldQuery.execute()
82+
println("hello world query result: ${result.data?.helloWorld}")
83+
}
84+
client.close()
85+
}
86+
```
87+
88+
See [graphql-kotlin docs](https://expediagroup.github.io/graphql-kotlin/docs/getting-started) for additial details.
Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,40 @@
11
---
22
name: graphql-kotlin
3-
description: A set of libraries for running GraphQL server in Kotlin.
3+
description: A set of libraries for running GraphQL client and server in Kotlin.
44
url: https://github.com/ExpediaGroup/graphql-kotlin/
55
github: ExpediaGroup/graphql-kotlin
66
---
77

8+
GraphQL Kotlin follows a code first approach for generating your GraphQL schemas. Given the similarities between Kotlin and GraphQL, such as the ability to define nullable/non-nullable types, a schema can be generated from Kotlin code without any separate schema specification. To create a reactive GraphQL web server add following dependency to your Gradle build file:
89

10+
```kotlin
11+
// build.gradle.kts
12+
implementation("com.expediagroup", "graphql-kotlin-spring-server", latestVersion)
13+
```
14+
15+
We also need to provide a list of supported packages that can be scanned for exposing your schema objects through reflections. Add following configuration to your `application.yml` file:
16+
17+
```yaml
18+
graphql:
19+
packages:
20+
- "com.your.package"
21+
```
22+
23+
With the above configuration we can now create our schema. In order to expose your queries, mutations and/or subscriptions in the GraphQL schema you simply need to implement corresponding marker interface and they will be automatically picked up by `graphql-kotlin-spring-server` auto-configuration library.
24+
25+
```kotlin
26+
@Component
27+
class HelloWorldQuery : Query {
28+
fun helloWorld() = "Hello World!!!"
29+
}
30+
```
31+
32+
This will result in a reactive GraphQL web application with following schema:
33+
34+
```graphql
35+
type Query {
36+
helloWorld: String!
37+
}
38+
```
39+
40+
See [graphql-kotlin docs](https://expediagroup.github.io/graphql-kotlin/docs/getting-started) for additial details.

0 commit comments

Comments
 (0)