Skip to main content
Version: 7.x.x

Client Serialization

GraphQL Kotlin build plugins can generate GraphQL client data classes that are compatible with Jackson (default) or kotlinx.serialization data models. By default, GraphQL clients will attempt to pick up the appropriate serializer from a classpath - graphql-kotlin-spring-client defines implicit dependency on Jackson based serializer and graphql-kotlin-ktor-client define a dependency on a kotlinx.serialization.

GraphQLClientSerializer is a service provider interface that expose generic serialize/deserialize methods that are used by the GraphQL clients to serialize requests to String and deserialize responses from String. By utilizing Java ServiceLoader mechanism we can dynamically load appropriate serializer from the classpath. If there are multiple providers on the classpath, we default to select the first one available one on the classpath.

GraphQL Kotlin Spring Client

Using Jackson

Jackson is the default serializer used by the build plugins and by GraphQL Kotlin Spring Client.

// build.gradle.kts
import com.expediagroup.graphql.plugin.gradle.graphql

dependencies {
implementation("com.expediagroup", "graphql-kotlin-spring-client", $graphQLKotlinVersion) {
}

graphql {
client {
endpoint = "http://localhost:8080/graphql"
packageName = "com.example.generated"
}
}

By default, ServiceLoader mechanism will load the first available GraphQL client serializer from the classpath.

val client = GraphQLWebClient(
url = "http://localhost:8080/graphql"
serializer = GraphQLClientJacksonSerializer()
)

Using Kotlinx Serialization

In order to use kotlinx.serialization we need to

  • add dependency on graphql-kotlin-client-serialization
  • configure GraphQL plugin to generate kotlinx.serialization compatible data models
  • configure corresponding compiler plugin
  • explicitly specify the target serializer during client construction OR exclude graphql-kotlin-client-jackson dependency
// build.gradle.kts
import com.expediagroup.graphql.plugin.gradle.config.GraphQLSerializer
import com.expediagroup.graphql.plugin.gradle.graphql

plugins {
kotlin("plugin.serialization") version $kotlinVersion
}

dependencies {
implementation("com.expediagroup", "graphql-kotlin-spring-client", $graphQLKotlinVersion) {
exclude("com.expediagroup", "graphql-kotlin-client-jackson")
}
implementation("com.expediagroup", "graphql-kotlin-client-serialization", $graphQLKotlinVersion)
}

graphql {
client {
endpoint = "http://localhost:8080/graphql"
packageName = "com.example.generated"
serializer = GraphQLSerializer.KOTLINX
}
}

By default, ServiceLoader mechanism will load the first available GraphQL client serializer from the classpath. We can also explicitly specify serializer during client construction

val client = GraphQLWebClient(
url = "http://localhost:8080/graphql"
serializer = GraphQLClientKotlinxSerializer()
)

GraphQL Kotlin Ktor Client

Using Kotlinx Serialization

kotlinx.serialization is the default serializer used by the GraphQL Kotlin Ktor Client. Build plugins default to use Jackson so we have to explicitly configure the tasks/mojos to use appropriate serializer.

// build.gradle.kts
import com.expediagroup.graphql.plugin.gradle.config.GraphQLSerializer
import com.expediagroup.graphql.plugin.gradle.graphql

plugins {
kotlin("plugin.serialization") version $kotlinVersion
}

dependencies {
implementation("com.expediagroup", "graphql-kotlin-ktor-client", $graphQLKotlinVersion) {
}

graphql {
client {
endpoint = "http://localhost:8080/graphql"
packageName = "com.example.generated"
serializer = GraphQLSerializer.KOTLINX
}
}

By default, ServiceLoader mechanism will load the first available GraphQL client serializer from the classpath.

val client = GraphQLKtorClient(
url = URL("http://localhost:8080/graphql")
serializer = GraphQLClientKotlinxSerializer()
)

Using Jackson

In order to use Jackson we need to

  • add dependency on graphql-kotlin-client-jackson
  • explicitly specify the target serializer during client construction OR exclude graphql-kotlin-client-serialization dependency
// build.gradle.kts
import com.expediagroup.graphql.plugin.gradle.graphql

dependencies {
implementation("com.expediagroup", "graphql-kotlin-ktor-client", $graphQLKotlinVersion) {
exclude("com.expediagroup", "graphql-kotlin-client-serialization")
}
implementation("com.expediagroup", "graphql-kotlin-client-jackson", $graphQLKotlinVersion)
}

graphql {
client {
endpoint = "http://localhost:8080/graphql"
packageName = "com.example.generated"
}
}

By default, ServiceLoader mechanism will load the first available GraphQL client serializer from the classpath. We can also explicitly specify serializer during client construction

val client = GraphQLKtorClient(
url = URL("http://localhost:8080/graphql")
serializer = GraphQLClientJacksonSerializer()
)