|
| 1 | +--- |
| 2 | +id: version-4.0.0-alpha.7-client-overview |
| 3 | +title: Client Overview |
| 4 | +original_id: client-overview |
| 5 | +--- |
| 6 | + |
| 7 | +GraphQL Kotlin provides a set of lightweight type-safe GraphQL HTTP clients. The library provides [Ktor HTTP client](https://ktor.io/clients/index.html) |
| 8 | +and [Spring WebClient](https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-webclient) |
| 9 | +based reference implementations as well as allows for custom implementations using other engines, see [client customization](client-customization.md) |
| 10 | +documentation for additional details. Type-safe data models are generated at build time by the GraphQL Kotlin [Gradle](../plugins/gradle-plugin.md) |
| 11 | +and [Maven](../plugins/maven-plugin.md) plugins. |
| 12 | + |
| 13 | +Client Features: |
| 14 | +* Supports query and mutation operations |
| 15 | +* Automatic generation of type-safe Kotlin models |
| 16 | +* Custom scalar support - defaults to String but can be configured to deserialize to specific types |
| 17 | +* Supports default enum values to gracefully handle new/unknown server values |
| 18 | +* Native support for coroutines |
| 19 | +* Easily configurable Ktor and Spring WebClient based HTTP Clients |
| 20 | +* Documentation generated from the underlying GraphQL schema |
| 21 | + |
| 22 | +## Project Configuration |
| 23 | + |
| 24 | +GraphQL Kotlin provides both Gradle and Maven plugins to automatically generate your client code at build time. In order |
| 25 | +to auto-generate the client code, plugins require target GraphQL schema and a list of query files to process. |
| 26 | + |
| 27 | +GraphQL schema can be provided as |
| 28 | + |
| 29 | +* result of introspection query (default) |
| 30 | +* downloaded from an SDL endpoint |
| 31 | +* local file |
| 32 | + |
| 33 | +See [Gradle](https://expediagroup.github.io/graphql-kotlin/docs/plugins/gradle-plugin) and [Maven](https://expediagroup.github.io/graphql-kotlin/docs/plugins/maven-plugin) |
| 34 | +plugin documentation for additional details. |
| 35 | + |
| 36 | +Once your data classes are generated, you can then execute their underlying GraphQL operations using one of the provided |
| 37 | +clients. By default, generated client code will rely on the generic interface which means that you will need to either |
| 38 | +implement a custom client OR pull in one of the reference implementations. |
| 39 | + |
| 40 | +> NOTE: If you are going to be using one of the reference implementations it is highly recommended to also configure the |
| 41 | +plugin to generate client type specific code as otherwise you will have limited customization options. See [client customization](client-customization.md) |
| 42 | +for additional details. |
| 43 | + |
| 44 | +Example below configures the project to use introspection query to obtain the schema and uses Ktor based HTTP client. |
| 45 | + |
| 46 | +### Build Configuration |
| 47 | + |
| 48 | +<!--DOCUSAURUS_CODE_TABS--> |
| 49 | +<!--Gradle--> |
| 50 | + |
| 51 | +Basic `build.gradle.kts` Gradle configuration that executes introspection query against specified endpoint to obtain target |
| 52 | +schema and then generate the clients under `com.example.generated` package name: |
| 53 | + |
| 54 | +```kotlin |
| 55 | +import com.expediagroup.graphql.plugin.generator.GraphQLClientType |
| 56 | +import com.expediagroup.graphql.plugin.gradle.graphql |
| 57 | + |
| 58 | +plugins { |
| 59 | + id("com.expediagroup.graphql") version $latestGraphQLKotlinVersion |
| 60 | +} |
| 61 | + |
| 62 | +dependencies { |
| 63 | + implementation("com.expediagroup:graphql-kotlin-ktor-client:$latestGraphQLKotlinVersion") |
| 64 | +} |
| 65 | + |
| 66 | +graphql { |
| 67 | + client { |
| 68 | + endpoint = "http://localhost:8080/graphql" |
| 69 | + packageName = "com.example.generated" |
| 70 | + clientType = GraphQLClientType.KTOR |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +<!--Maven--> |
| 76 | + |
| 77 | +Basic Maven `pom.xml` configuration that executes introspection query against specified endpoint to obtain target |
| 78 | +schema and then generate the clients under `com.example.generated` package name: |
| 79 | + |
| 80 | +```xml |
| 81 | +<?xml version="1.0" encoding="UTF-8"?> |
| 82 | +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 83 | + <modelVersion>4.0.0</modelVersion> |
| 84 | + |
| 85 | + <groupId>com.example</groupId> |
| 86 | + <artifactId>graphql-kotlin-maven-client-example</artifactId> |
| 87 | + <version>1.0.0-SNAPSHOT</version> |
| 88 | + |
| 89 | + <properties> |
| 90 | + <graphql-kotlin.version>$latestGraphQLKotlinVersion</graphql-kotlin.version> |
| 91 | + </properties> |
| 92 | + |
| 93 | + <dependencies> |
| 94 | + <dependency> |
| 95 | + <groupId>com.expediagroup</groupId> |
| 96 | + <artifactId>graphql-kotlin-ktor-client</artifactId> |
| 97 | + <version>${graphql-kotlin.version}</version> |
| 98 | + </dependency> |
| 99 | + </dependencies> |
| 100 | + |
| 101 | + <build> |
| 102 | + <plugins> |
| 103 | + <plugin> |
| 104 | + <groupId>com.expediagroup</groupId> |
| 105 | + <artifactId>graphql-kotlin-maven-plugin</artifactId> |
| 106 | + <version>${graphql-kotlin.version}</version> |
| 107 | + <executions> |
| 108 | + <execution> |
| 109 | + <id>generate-graphql-client</id> |
| 110 | + <goals> |
| 111 | + <goal>introspectSchema</goal> |
| 112 | + <goal>generateClient</goal> |
| 113 | + </goals> |
| 114 | + <configuration> |
| 115 | + <endpoint>http://localhost:8080/graphql</endpoint> |
| 116 | + <packageName>com.example.generated</packageName> |
| 117 | + <schemaFile>${project.build.directory}/schema.graphql</schemaFile> |
| 118 | + <clientType>KTOR</clientType> |
| 119 | + </configuration> |
| 120 | + </execution> |
| 121 | + </executions> |
| 122 | + </plugin> |
| 123 | + </plugins> |
| 124 | + </build> |
| 125 | +</project> |
| 126 | +``` |
| 127 | + |
| 128 | +<!--END_DOCUSAURUS_CODE_TABS--> |
| 129 | + |
| 130 | +See [graphql-kotlin-client-example](https://github.com/ExpediaGroup/graphql-kotlin/tree/master/examples/client) project for complete |
| 131 | +working examples of Gradle and Maven based projects. |
| 132 | + |
| 133 | +### Generating GraphQL Client |
| 134 | + |
| 135 | +By default, GraphQL Kotlin build plugins will attempt to generate GraphQL clients from all `*.graphql` files located under |
| 136 | +`src/main/resources`. Queries are validated against the target GraphQL schema, which can be manually provided, retrieved by |
| 137 | +the plugins through introspection (as configured in examples above) or downloaded directly from a custom SDL endpoint. |
| 138 | +See our documentation for more details on supported [Gradle tasks](../plugins/gradle-plugin.md) |
| 139 | +and [Maven Mojos](../plugins/maven-plugin.md). |
| 140 | + |
| 141 | +When creating your GraphQL queries make sure to always specify an operation name and name the files accordingly. Each |
| 142 | +one of your query files will generate a corresponding Kotlin file with a class matching your operation |
| 143 | +name that will act as a wrapper for all corresponding data classes. For example, given `HelloWorldQuery.graphql` with |
| 144 | +`HelloWorldQuery` as the operation name, GraphQL Kotlin plugins will generate a corresponding `HelloWorldQuery.kt` file |
| 145 | +with a `HelloWorldQuery` class under the configured package. |
| 146 | + |
| 147 | +For example, given a simple schema |
| 148 | + |
| 149 | +```graphql |
| 150 | +type Query { |
| 151 | + helloWorld: String |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +And a corresponding `HelloWorldQuery.graphql` query |
| 156 | + |
| 157 | +```graphql |
| 158 | +query HelloWorldQuery { |
| 159 | + helloWorld |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +Plugins will generate following client code |
| 164 | + |
| 165 | +```kotlin |
| 166 | +package com.example.generated |
| 167 | + |
| 168 | +import com.expediagroup.graphql.client.GraphQLKtorClient |
| 169 | +import com.expediagroup.graphql.types.GraphQLResponse |
| 170 | +import kotlin.String |
| 171 | + |
| 172 | +const val HELLO_WORLD_QUERY: String = "query HelloWorldQuery {\n helloWorld\n}" |
| 173 | + |
| 174 | +class HelloWorldQuery( |
| 175 | + private val graphQLClient: GraphQLKtorClient<*> |
| 176 | +) { |
| 177 | + suspend fun execute(requestBuilder: HttpRequestBuilder.() -> Unit = {}): GraphQLResponse<HelloWorldQuery.Result> = |
| 178 | + graphQLClient.execute(HELLO_WORLD_QUERY, "HelloWorldQuery", null, requestBuilder) |
| 179 | + |
| 180 | + data class Result( |
| 181 | + val helloWorld: String |
| 182 | + ) |
| 183 | +} |
| 184 | +``` |
| 185 | + |
| 186 | +Generated classes requires an instance of `GraphQLKtorClient` and exposes a single `execute` suspendable method that executes |
| 187 | +the underlying GraphQL operation using the provided client. |
| 188 | + |
| 189 | +### Executing Queries |
| 190 | + |
| 191 | +Your auto generated classes accept an instance of `GraphQLKtorClient` which is a thin wrapper around Ktor HTTP client that |
| 192 | +ensures proper serialization and deserialization of your GraphQL objects. `GraphQLKtorClient` requires target URL to be |
| 193 | +specified and defaults to fully asynchronous non-blocking [Coroutine-based IO engine](https://ktor.io/clients/http-client/engines.html#cio). |
| 194 | + |
| 195 | +```kotlin |
| 196 | +package com.example.client |
| 197 | + |
| 198 | +import com.expediagroup.graphql.client.GraphQLKtorClient |
| 199 | +import com.expediagroup.graphql.generated.HelloWorldQuery |
| 200 | +import kotlinx.coroutines.runBlocking |
| 201 | +import java.net.URL |
| 202 | + |
| 203 | +fun main() { |
| 204 | + val client = GraphQLKtorClient(url = URL("http://localhost:8080/graphql")) |
| 205 | + val helloWorldQuery = HelloWorldQuery(client) |
| 206 | + runBlocking { |
| 207 | + val result = helloWorldQuery.execute() |
| 208 | + println("hello world query result: ${result.data?.helloWorld}") |
| 209 | + } |
| 210 | + client.close() |
| 211 | +} |
| 212 | +``` |
0 commit comments