From 6da823f9bb1c81976ffe9c6b5042d29ab56f38be Mon Sep 17 00:00:00 2001 From: Dariusz Kuc Date: Tue, 3 Aug 2021 10:20:53 -0500 Subject: [PATCH] [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. --- .../client/graphql-kotlin.md | 47 +++++++++---------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/src/content/code/language-support/java-kotlin-android/client/graphql-kotlin.md b/src/content/code/language-support/java-kotlin-android/client/graphql-kotlin.md index 02a227ff95..7bb48c96ee 100644 --- a/src/content/code/language-support/java-kotlin-android/client/graphql-kotlin.md +++ b/src/content/code/language-support/java-kotlin-android/client/graphql-kotlin.md @@ -5,13 +5,12 @@ url: https://github.com/ExpediaGroup/graphql-kotlin/ github: ExpediaGroup/graphql-kotlin --- -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. +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. -To generate Ktor based GraphQL client add following to your Gradle build file: +To generate Jackson models that will be used with GraphQL Kotlin Spring WebClient, add following to your Gradle build file: ```kotlin // build.gradle.kts -import com.expediagroup.graphql.plugin.generator.GraphQLClientType import com.expediagroup.graphql.plugin.gradle.graphql plugins { @@ -19,7 +18,7 @@ plugins { } dependencies { - implementation("com.expediagroup:graphql-kotlin-ktor-client:$latestGraphQLKotlinVersion") + implementation("com.expediagroup:graphql-kotlin-spring-client:$latestGraphQLKotlinVersion") } graphql { @@ -28,12 +27,11 @@ graphql { endpoint = "http://localhost:8080/graphql" // package for generated client code packageName = "com.example.generated" - clientType = GraphQLClientType.KTOR } } ``` -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: +By default, GraphQL Kotlin plugins will look for query files under `src/main/resources`. Given `HelloWorldQuery.graphql` sample query: ```graphql query HelloWorldQuery { @@ -41,48 +39,47 @@ query HelloWorldQuery { } ``` -Plugin will generate following client code: +Plugin will generate classes that are simple POJOs implementing GraphQLClientRequest interface and represent a GraphQL request. ```kotlin package com.example.generated -import com.expediagroup.graphql.client.GraphQLKtorClient -import com.expediagroup.graphql.types.GraphQLResponse +import com.expediagroup.graphql.client.types.GraphQLClientRequest import kotlin.String +import kotlin.reflect.KClass const val HELLO_WORLD_QUERY: String = "query HelloWorldQuery {\n helloWorld\n}" -class HelloWorldQuery( - private val graphQLClient: GraphQLKtorClient<*> -) { - suspend fun execute(requestBuilder: HttpRequestBuilder.() -> Unit = {}): GraphQLResponse = - graphQLClient.execute(HELLO_WORLD_QUERY, "HelloWorldQuery", null, requestBuilder) +class HelloWorldQuery: GraphQLClientRequest { + override val query: String = HELLO_WORLD_QUERY - data class Result( - val helloWorld: String - ) + override val operationName: String = "HelloWorldQuery" + + override fun responseType(): KClass = HelloWorldQuery.Result::class + + data class Result( + val helloWorld: String + } } ``` -We can then execute the client +We can then execute our queries using target client. ```kotlin package com.example.client -import com.expediagroup.graphql.client.GraphQLKtorClient +import com.expediagroup.graphql.client.spring.GraphQLWebClient import com.expediagroup.graphql.generated.HelloWorldQuery import kotlinx.coroutines.runBlocking -import java.net.URL fun main() { - val client = GraphQLKtorClient(url = URL("http://localhost:8080/graphql")) - val helloWorldQuery = HelloWorldQuery(client) + val client = GraphQLWebClient(url = "http://localhost:8080/graphql") runBlocking { - val result = helloWorldQuery.execute() + val helloWorldQuery = HelloWorldQuery() + val result = client.execute(helloWorldQuery) println("hello world query result: ${result.data?.helloWorld}") } - client.close() } ``` -See [graphql-kotlin docs](https://expediagroup.github.io/graphql-kotlin/docs/getting-started) for additional details. +See [graphql-kotlin client docs](https://opensource.expediagroup.com/graphql-kotlin/docs/client/client-overview) for additional details.