|
| 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. |
0 commit comments