commonMain.com.apollographql.apollo3.api.CompositeAdapter.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of apollo-api-jvm Show documentation
Show all versions of apollo-api-jvm Show documentation
Apollo GraphQL API classes
package com.apollographql.apollo3.api
import com.apollographql.apollo3.api.json.JsonReader
import com.apollographql.apollo3.api.json.JsonWriter
import okio.IOException
import kotlin.jvm.JvmField
/**
* A [CompositeAdapter] is responsible for adapting Kotlin-generated GraphQL composite types to/from their Json representation.
*
* It is used to
* - deserialize network responses
* - normalize models into records that can be stored in cache
* - deserialize records
*
* This class is implemented by the generated code, it shouldn't be used directly.
*/
interface CompositeAdapter {
@Throws(IOException::class)
fun fromJson(reader: JsonReader, adapterContext: CompositeAdapterContext): T
@Throws(IOException::class)
fun toJson(writer: JsonWriter, value: T, adapterContext: CompositeAdapterContext)
}
class CompositeAdapterContext private constructor(
@JvmField
val customScalarAdapters: CustomScalarAdapters,
@JvmField
val falseVariables: Set,
@JvmField
val deferredFragmentIdentifiers: Set?,
) {
class Builder {
private var customScalarAdapters: CustomScalarAdapters? = null
private var falseVariables: Set? = null
private var deferredFragmentIdentifiers: Set? = null
fun customScalarAdapters(customScalarAdapters: CustomScalarAdapters) = apply {
this.customScalarAdapters = customScalarAdapters
}
fun falseVariables(falseVariables: Set?) = apply {
this.falseVariables = falseVariables
}
fun deferredFragmentIdentifiers(deferredFragmentIdentifiers: Set?) = apply {
this.deferredFragmentIdentifiers = deferredFragmentIdentifiers
}
fun build(): CompositeAdapterContext {
return CompositeAdapterContext(
customScalarAdapters ?: CustomScalarAdapters.Empty,
falseVariables ?: emptySet(),
deferredFragmentIdentifiers
)
}
}
}
fun CompositeAdapter.toJson(writer: JsonWriter, customScalarAdapters: CustomScalarAdapters, value: T) {
toJson(writer, value, CompositeAdapterContext.Builder().customScalarAdapters(customScalarAdapters).build())
}
fun CompositeAdapter.fromJson(reader: JsonReader, customScalarAdapters: CustomScalarAdapters): T {
return fromJson(reader, CompositeAdapterContext.Builder().customScalarAdapters(customScalarAdapters).build())
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy