Skip to content

Commit 6da823f

Browse files
author
Dariusz Kuc
committed
[java/kotlin client] update graphql-kotlin client documentation
We completely rewrote `graphql-kotlin` client code in 4.0.0 release. Updating documentation to match the latest version.
1 parent 62cede4 commit 6da823f

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

src/content/code/language-support/java-kotlin-android/client/graphql-kotlin.md

+22-25
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@ url: https://github.com/ExpediaGroup/graphql-kotlin/
55
github: ExpediaGroup/graphql-kotlin
66
---
77

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.
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. Jackson and kotlinx-serialization type-safe data models are generated at build time by the provided Gradle and Maven plugins.
99

10-
To generate Ktor based GraphQL client add following to your Gradle build file:
10+
To generate Jackson models that will be used with GraphQL Kotlin Spring WebClient, add following to your Gradle build file:
1111

1212
```kotlin
1313
// build.gradle.kts
14-
import com.expediagroup.graphql.plugin.generator.GraphQLClientType
1514
import com.expediagroup.graphql.plugin.gradle.graphql
1615

1716
plugins {
1817
id("com.expediagroup.graphql") version $latestGraphQLKotlinVersion
1918
}
2019

2120
dependencies {
22-
implementation("com.expediagroup:graphql-kotlin-ktor-client:$latestGraphQLKotlinVersion")
21+
implementation("com.expediagroup:graphql-kotlin-spring-client:$latestGraphQLKotlinVersion")
2322
}
2423

2524
graphql {
@@ -28,61 +27,59 @@ graphql {
2827
endpoint = "http://localhost:8080/graphql"
2928
// package for generated client code
3029
packageName = "com.example.generated"
31-
clientType = GraphQLClientType.KTOR
3230
}
3331
}
3432
```
3533

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:
34+
By default, GraphQL Kotlin plugins will look for query files under `src/main/resources`. Given `HelloWorldQuery.graphql` sample query:
3735

3836
```graphql
3937
query HelloWorldQuery {
4038
helloWorld
4139
}
4240
```
4341

44-
Plugin will generate following client code:
42+
Plugin will generate classes that are simple POJOs implementing GraphQLClientRequest interface and represent a GraphQL request.
4543

4644
```kotlin
4745
package com.example.generated
4846

49-
import com.expediagroup.graphql.client.GraphQLKtorClient
50-
import com.expediagroup.graphql.types.GraphQLResponse
47+
import com.expediagroup.graphql.client.types.GraphQLClientRequest
5148
import kotlin.String
49+
import kotlin.reflect.KClass
5250

5351
const val HELLO_WORLD_QUERY: String = "query HelloWorldQuery {\n helloWorld\n}"
5452

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)
53+
class HelloWorldQuery: GraphQLClientRequest<HelloWorldQuery.Result> {
54+
override val query: String = HELLO_WORLD_QUERY
6055

61-
data class Result(
62-
val helloWorld: String
63-
)
56+
override val operationName: String = "HelloWorldQuery"
57+
58+
override fun responseType(): KClass<HelloWorldQuery.Result> = HelloWorldQuery.Result::class
59+
60+
data class Result(
61+
val helloWorld: String
62+
}
6463
}
6564
```
6665

67-
We can then execute the client
66+
We can then execute our queries using target client.
6867

6968
```kotlin
7069
package com.example.client
7170

72-
import com.expediagroup.graphql.client.GraphQLKtorClient
71+
import com.expediagroup.graphql.client.spring.GraphQLWebClient
7372
import com.expediagroup.graphql.generated.HelloWorldQuery
7473
import kotlinx.coroutines.runBlocking
75-
import java.net.URL
7674

7775
fun main() {
78-
val client = GraphQLKtorClient(url = URL("http://localhost:8080/graphql"))
79-
val helloWorldQuery = HelloWorldQuery(client)
76+
val client = GraphQLWebClient(url = "http://localhost:8080/graphql")
8077
runBlocking {
81-
val result = helloWorldQuery.execute()
78+
val helloWorldQuery = HelloWorldQuery()
79+
val result = client.execute(helloWorldQuery)
8280
println("hello world query result: ${result.data?.helloWorld}")
8381
}
84-
client.close()
8582
}
8683
```
8784

88-
See [graphql-kotlin docs](https://expediagroup.github.io/graphql-kotlin/docs/getting-started) for additional details.
85+
See [graphql-kotlin client docs](https://opensource.expediagroup.com/graphql-kotlin/docs/client/client-overview) for additional details.

0 commit comments

Comments
 (0)