Skip to content

Commit cb44b69

Browse files
authored
[client] open up GraphQL client (#811)
* [client] open up GraphQL client Makes `GraphQLClient` an open class so end users can override `execute` method to provide some additional custom logic as needed. Resolves: #800 * update docs to highlight that client can be extended
1 parent 5d3a320 commit cb44b69

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

docs/client/client-customization.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ val result = helloWorldQuery.execute(variables = HelloWorldQuery.Variables(name
5656
}
5757
```
5858

59+
### Custom GraphQL client
60+
61+
`GraphQLClient` is an open class which means you can also extend it to provide custom `execute` logic.
62+
63+
```kotlin
64+
class CustomGraphQLClient(url: URL) : GraphQLClient<CIOEngineConfig>(url = url, engineFactory = CIO) {
65+
66+
override suspend fun <T> execute(query: String, operationName: String?, variables: Any?, resultType: Class<T>, requestBuilder: HttpRequestBuilder.() -> Unit): GraphQLResponse<T> {
67+
// custom init logic
68+
val result = super.execute(query, operationName, variables, resultType, requestBuilder)
69+
// custom finalize logic
70+
return result
71+
}
72+
}
73+
```
74+
5975
## Jackson Customization
6076

6177
`GraphQLClient` relies on Jackson to handle polymorphic types and default enum values. You can specify your own custom

graphql-kotlin-client/src/main/kotlin/com/expediagroup/graphql/client/GraphQLClient.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import java.net.URL
4242
* A lightweight typesafe GraphQL HTTP client.
4343
*/
4444
@KtorExperimentalAPI
45-
class GraphQLClient<in T : HttpClientEngineConfig>(
45+
open class GraphQLClient<in T : HttpClientEngineConfig>(
4646
private val url: URL,
4747
engineFactory: HttpClientEngineFactory<T>,
4848
private val mapper: ObjectMapper = jacksonObjectMapper(),
@@ -71,7 +71,7 @@ class GraphQLClient<in T : HttpClientEngineConfig>(
7171
* default serialization would attempt to serialize results back to Any object. As a workaround we get raw results as String which we then
7272
* manually deserialize using passed in result type Class information.
7373
*/
74-
suspend fun <T> execute(query: String, operationName: String? = null, variables: Any? = null, resultType: Class<T>, requestBuilder: HttpRequestBuilder.() -> Unit = {}): GraphQLResponse<T> {
74+
open suspend fun <T> execute(query: String, operationName: String? = null, variables: Any? = null, resultType: Class<T>, requestBuilder: HttpRequestBuilder.() -> Unit = {}): GraphQLResponse<T> {
7575
// Variables are simple data classes which will be serialized as map.
7676
// By using map instead of typed object we can eliminate the need to explicitly convert variables to a map
7777
val graphQLRequest = mapOf(

0 commit comments

Comments
 (0)