All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.apollographql.apollo3.api.Executables.kt Maven / Gradle / Ivy

There is a newer version: 4.0.0-beta.7
Show newest version
@file:JvmName("Executables")

package com.apollographql.apollo3.api

import com.apollographql.apollo3.annotations.ApolloInternal
import com.apollographql.apollo3.api.json.BufferedSinkJsonWriter
import com.apollographql.apollo3.api.json.MapJsonWriter
import okio.Buffer
import kotlin.jvm.JvmName

@Suppress("UNCHECKED_CAST")
fun  Executable.variables(customScalarAdapters: CustomScalarAdapters): Executable.Variables {
  return variables(customScalarAdapters, false)
}

fun  Executable.variablesJson(customScalarAdapters: CustomScalarAdapters): String {
  val buffer = Buffer()
  BufferedSinkJsonWriter(buffer).apply {
    beginObject()
    serializeVariables(this, customScalarAdapters.serializeVariablesWithDefaultBooleanValues())
    endObject()
  }
  return buffer.readUtf8()
}

@Suppress("UNCHECKED_CAST")
@ApolloInternal
fun  Executable.variables(
    customScalarAdapters: CustomScalarAdapters,
    withDefaultBooleanValues: Boolean,
): Executable.Variables {
  val valueMap = MapJsonWriter().apply {
    beginObject()
    serializeVariables(this, customScalarAdapters.let { if (withDefaultBooleanValues) it.serializeVariablesWithDefaultBooleanValues() else it })
    endObject()
  }.root() as Map
  return Executable.Variables(valueMap)
}

private fun CustomScalarAdapters.serializeVariablesWithDefaultBooleanValues() = newBuilder()
    .adapterContext(
        adapterContext.newBuilder()
            .serializeVariablesWithDefaultBooleanValues(true)
            .build()
    )
    .build()




© 2015 - 2025 Weber Informatics LLC | Privacy Policy