Skip to content

Commit 4c4dadf

Browse files
authored
[plugins] update plugins to allow generation of client code using kotlinx serialization (#1070)
This is a third and final PR in a series to introduce support for both `Jackson` and `kotlinx.serialization` data formats for GraphQL Kotlin clients. This PR updates Gradle and Maven plugins to allow generating client data models supporting both formats. This PR also updates Ktor client to by default depend on `kotlinx.serialization` instead of `Jackson`. Large size of the PR is driven primarily due to the formatting fixes in the Gradle plugin integration tests (updated build scripts templates to use `trimMargin` for consistent indentation). Related: * #1048 - this is an attempt to split it up to more manageable pieces * #1066 - first PR in the series * #1069 - second PR in the series * resolves #929 - add support for generating `kotlinx.serialization` data models
1 parent 667297f commit 4c4dadf

File tree

48 files changed

+1862
-820
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1862
-820
lines changed

clients/graphql-kotlin-ktor-client/build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ val wireMockVersion: String by project
99

1010
dependencies {
1111
api(project(path = ":graphql-kotlin-client"))
12-
api(project(path = ":graphql-kotlin-client-jackson"))
12+
api(project(path = ":graphql-kotlin-client-serialization"))
1313
api("io.ktor:ktor-client-cio:$ktorVersion")
14-
api("io.ktor:ktor-client-jackson:$ktorVersion")
15-
testImplementation(project(path = ":graphql-kotlin-client-serialization"))
14+
api("io.ktor:ktor-client-serialization:$ktorVersion")
15+
testImplementation(project(path = ":graphql-kotlin-client-jackson"))
1616
testImplementation("io.ktor:ktor-client-okhttp:$ktorVersion")
1717
testImplementation("io.ktor:ktor-client-logging:$ktorVersion")
1818
testImplementation("com.github.tomakehurst:wiremock-jre8:$wireMockVersion")

examples/client/gradle-client/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Example usage GraphQL Kotlin Client using Gradle
22

33
This project is a simple application that use [GraphQL Kotlin Gradle plugin](https://expediagroup.github.io/graphql-kotlin/docs/plugins/gradle-plugin)
4-
to auto-generate GraphQL client and then use it to communicate with the target GraphQL server. See [client documentation](https://expediagroup.github.io/graphql-kotlin/docs/client/client-overview)
5-
for details.
4+
to auto-generate GraphQL client data model compatible with `kotlinx.serialization` and then use Ktor based client to communicate
5+
with the target GraphQL server.
6+
7+
See [client documentation](https://expediagroup.github.io/graphql-kotlin/docs/client/client-overview) for additional details.
68

79
## Building locally
810

examples/client/gradle-client/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import com.expediagroup.graphql.plugin.gradle.config.GraphQLScalar
2+
import com.expediagroup.graphql.plugin.gradle.config.GraphQLSerializer
23
import com.expediagroup.graphql.plugin.gradle.graphql
34

45
description = "Example usage of Gradle plugin to generate GraphQL Kotlin Client"
56

67
plugins {
78
application
9+
kotlin("plugin.serialization")
810
id("com.expediagroup.graphql")
911
}
1012

@@ -30,6 +32,7 @@ graphql {
3032
allowDeprecatedFields = true
3133
headers = mapOf("X-Custom-Header" to "My-Custom-Header")
3234
customScalars = listOf(GraphQLScalar("UUID", "java.util.UUID", "com.expediagroup.graphql.examples.client.gradle.UUIDScalarConverter"))
35+
serializer = GraphQLSerializer.KOTLINX
3336
}
3437
}
3538
ktlint {

examples/client/gradle-client/src/main/kotlin/com/expediagroup/graphql/examples/client/gradle/Application.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.expediagroup.graphql.examples.client.gradle
1818

1919
import com.expediagroup.graphql.client.ktor.GraphQLKtorClient
20+
import com.expediagroup.graphql.client.types.GraphQLClientResponse
2021
import com.expediagroup.graphql.generated.AddObjectMutation
2122
import com.expediagroup.graphql.generated.ExampleQuery
2223
import com.expediagroup.graphql.generated.HelloWorldQuery
@@ -50,10 +51,14 @@ fun main() {
5051
}
5152
val client = GraphQLKtorClient(
5253
url = URL("http://localhost:8080/graphql"),
53-
httpClient = httpClient,
54+
httpClient = httpClient
5455
)
5556
println("HelloWorld examples")
5657
runBlocking {
58+
val helloWorldQuery = HelloWorldQuery(variables = HelloWorldQuery.Variables())
59+
val helloWorldResult = client.execute(helloWorldQuery)
60+
val helloWorldResultImplicit: GraphQLClientResponse<HelloWorldQuery.Result> = client.execute(helloWorldQuery)
61+
5762
val results = client.execute(
5863
listOf(
5964
HelloWorldQuery(variables = HelloWorldQuery.Variables(name = null)),

examples/client/maven-client/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Example usage GraphQL Kotlin Client using Maven
22

33
This project is a simple application that use [GraphQL Kotlin Maven plugin](https://expediagroup.github.io/graphql-kotlin/docs/plugins/maven-plugin)
4-
to auto-generate GraphQL client and then use it to communicate with the target GraphQL server. See [documentation](https://expediagroup.github.io/graphql-kotlin/)
5-
for details.
4+
to auto-generate GraphQL client data model compatible with `Jackson` and then use Spring Webclient based client to communicate
5+
with the target GraphQL server.
6+
7+
See [client documentation](https://expediagroup.github.io/graphql-kotlin/docs/client/client-overview) for additional details.
68

79
## Building locally
810

examples/client/maven-client/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
<converter>com.expediagroup.graphql.examples.client.maven.UUIDScalarConverter</converter>
8181
</customScalar>
8282
</customScalars>
83+
<serializer>JACKSON</serializer>
8384
</configuration>
8485
</execution>
8586
</executions>

examples/settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pluginManagement {
1010
plugins {
1111
kotlin("jvm") version kotlinVersion
1212
kotlin("plugin.spring") version kotlinVersion
13+
kotlin("plugin.serialization") version kotlinVersion
1314
id("io.gitlab.arturbosch.detekt") version detektVersion
1415
id("org.jlleitschuh.gradle.ktlint") version ktlintPluginVersion
1516
id("org.springframework.boot") version springBootVersion

plugins/graphql-kotlin-gradle-plugin/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ This extension can be used to configure global options instead of explicitly con
2323

2424
```kotlin
2525
import com.expediagroup.graphql.plugin.gradle.config.GraphQLScalar
26+
import com.expediagroup.graphql.plugin.gradle.config.GraphQLSerializer
2627
import com.expediagroup.graphql.plugin.gradle.graphql
2728

2829
graphql {
@@ -41,6 +42,8 @@ graphql {
4142
queryFileDirectory = "${project.projectDir}/src/main/resources/queries"
4243
// Optional list of query files to be processed, takes precedence over queryFileDirectory
4344
queryFiles = listOf(file("${project.projectDir}/src/main/resources/queries/MyQuery.graphql"))
45+
// JSON serializer that will be used to generate the data classes.
46+
serializer = GraphQLSerializer.JACKSON
4447
// GraphQL server SDL endpoint that will be used to download schema. Alternatively you can run introspection query against `endpoint`.
4548
sdlEndpoint = "http://localhost:8080/sdl"
4649
// Timeout configuration for introspection query/downloading SDL
@@ -97,6 +100,7 @@ resulting generated code will be automatically added to the project main source
97100
| `packageName` | String | yes | Target package name for generated code.<br/>**Command line property is**: `packageName`. |
98101
| `queryFiles` | FileCollection | | List of query files to be processed. Instead of a list of files to be processed you can specify `queryFileDirectory` directory instead. If this property is specified it will take precedence over the corresponding directory property. |
99102
| `queryFileDirectory` | String | | Directory file containing GraphQL queries. Instead of specifying a directory you can also specify list of query file by using `queryFiles` property instead.<br/>**Default value is:** `src/main/resources`.<br/>**Command line property is**: `queryFileDirectory`. |
103+
| `serializer` | GraphQLSerializer | | JSON serializer that will be used to generate the data classes.<br/>**Default value is:** `GraphQLSerializer.JACKSON`. |
100104
| `schemaFile` | File | `schemaFileName` or `schemaFile` has to be provided | GraphQL schema file that will be used to generate client code. |
101105
| `schemaFileName` | String | `schemaFileName` or `schemaFile` has to be provided | Path to GraphQL schema file that will be used to generate client code.<br/>**Command line property is**: `schemaFileName`. |
102106

@@ -143,6 +147,7 @@ test source set.
143147
| `packageName` | String | yes | Target package name for generated code.<br/>**Command line property is**: `packageName`. |
144148
| `queryFiles` | FileCollection | | List of query files to be processed. Instead of a list of files to be processed you can specify `queryFileDirectory` directory instead. If this property is specified it will take precedence over the corresponding directory property. |
145149
| `queryFileDirectory` | String | | Directory file containing GraphQL queries. Instead of specifying a directory you can also specify list of query file by using `queryFiles` property instead.<br/>**Default value is:** `src/test/resources`.<br/>**Command line property is**: `queryFileDirectory`. |
150+
| `serializer` | GraphQLSerializer | | JSON serializer that will be used to generate the data classes.<br/>**Default value is:** `GraphQLSerializer.JACKSON`. |
146151
| `schemaFile` | File | `schemaFileName` or `schemaFile` has to be provided | GraphQL schema file that will be used to generate client code. |
147152
| `schemaFileName` | String | `schemaFileName` or `schemaFile` has to be provided | Path to GraphQL schema file that will be used to generate client code.<br/>**Command line property is**: `schemaFileName`. |
148153

plugins/graphql-kotlin-gradle-plugin/src/main/kotlin/com/expediagroup/graphql/plugin/gradle/GraphQLGradlePlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class GraphQLGradlePlugin : Plugin<Project> {
9595
generateClientTask.queryFileDirectory.convention(queryFileDirectory)
9696
}
9797
generateClientTask.queryFiles.setFrom(extension.clientExtension.queryFiles)
98+
generateClientTask.serializer.convention(extension.clientExtension.serializer)
9899

99100
when {
100101
extension.clientExtension.endpoint != null -> {

plugins/graphql-kotlin-gradle-plugin/src/main/kotlin/com/expediagroup/graphql/plugin/gradle/GraphQLPluginExtension.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.expediagroup.graphql.plugin.gradle
1818

19+
import com.expediagroup.graphql.plugin.gradle.config.GraphQLSerializer
1920
import com.expediagroup.graphql.plugin.gradle.config.GraphQLScalar
2021
import com.expediagroup.graphql.plugin.gradle.config.TimeoutConfiguration
2122
import org.gradle.api.Action
@@ -70,6 +71,8 @@ open class GraphQLPluginClientExtension {
7071
var queryFiles: List<File> = emptyList()
7172
/** Directory containing GraphQL query files. */
7273
var queryFileDirectory: String? = null
74+
/** JSON serializer that will be used to generate the data classes. */
75+
var serializer: GraphQLSerializer = GraphQLSerializer.JACKSON
7376

7477
/** Connect and read timeout configuration for executing introspection query/download schema */
7578
internal val timeoutConfig: TimeoutConfiguration = TimeoutConfiguration()

0 commit comments

Comments
 (0)