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

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

The newest version!
@file:JvmName("Executables")

package com.apollographql.apollo.api

import com.apollographql.apollo.annotations.ApolloInternal
import com.apollographql.apollo.api.json.BufferedSinkJsonWriter
import com.apollographql.apollo.api.json.JsonReader
import com.apollographql.apollo.api.json.JsonWriter
import com.apollographql.apollo.api.json.MapJsonWriter
import okio.Buffer
import kotlin.jvm.JvmName
import kotlin.jvm.JvmOverloads


/**
 * Returns a map of the variables as they would be sent over the wire. Use this to construct your own HTTP requests
 */
fun  Executable.variables(customScalarAdapters: CustomScalarAdapters): Executable.Variables {
  return variables(customScalarAdapters, false)
}

/**
 * Returns the variables as they would be sent over the wire. Use this to construct your own HTTP requests
 */
fun  Executable.variablesJson(customScalarAdapters: CustomScalarAdapters): String {
  val buffer = Buffer()
  BufferedSinkJsonWriter(buffer).apply {
    beginObject()
    serializeVariables(this, customScalarAdapters, false)
    endObject()
  }
  return buffer.readUtf8()
}

/**
 * Returns the [Set] of boolean variables that are **false** either explicitly or because there is a default value
 *
 * This inversion is needed for `@defer(if: Boolean! = true)` that default to true so absence of a value in the set
 * denotes true
 *
 * Order of precedence for a query like this: query($foo: Boolean = false)
 * - variables: {"foo": true} => bit not set
 * - variables: {"foo": false} => bit set
 * - variables: {} => bit set
 * Order of precedence for a query like this: query($foo: Boolean)
 * - variables: {"foo": true} => bit not set
 * - variables: {"foo": false} => bit set
 * - variables: {} => bit not set
 */
@ApolloInternal
fun  Executable.falseVariables(customScalarAdapters: CustomScalarAdapters): Set {
  return variables(customScalarAdapters, true).valueMap.filter { it.value == false }.keys
}

@Suppress("UNCHECKED_CAST")
@ApolloInternal
fun  Executable.variables(
    customScalarAdapters: CustomScalarAdapters,
    withDefaultValues: Boolean,
): Executable.Variables {
  val valueMap = MapJsonWriter().apply {
    beginObject()
    serializeVariables(this, customScalarAdapters, withDefaultValues)
    endObject()
  }.root() as VariablesJson
  return Executable.Variables(valueMap)
}

@JvmOverloads
fun  Executable.parseData(
    jsonReader: JsonReader,
    customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty,
    falseVariables: Set? = null,
    deferredFragmentIds: Set? = null,
    errors: List? = null
): D? {
  val customScalarAdapters1 = customScalarAdapters.newBuilder()
      .falseVariables(falseVariables)
      .deferredFragmentIdentifiers(deferredFragmentIds)
      .errors(errors)
      .build()
  return adapter().nullable().fromJson(jsonReader, customScalarAdapters1)
}


fun  Executable.composeData(
    jsonWriter: JsonWriter,
    customScalarAdapters: CustomScalarAdapters,
    value: D
) {
  adapter().toJson(jsonWriter, customScalarAdapters, value)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy