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

commonMain.com.apollographql.apollo3.testing.MockSubscription.kt Maven / Gradle / Ivy

There is a newer version: 4.0.0-beta.7
Show newest version
package com.apollographql.apollo3.testing

import com.apollographql.apollo3.api.ResponseAdapterCache
import com.apollographql.apollo3.api.Operation
import com.apollographql.apollo3.api.ResponseField
import com.apollographql.apollo3.api.Subscription
import com.apollographql.apollo3.api.AnyResponseAdapter
import com.apollographql.apollo3.api.ResponseAdapter
import com.apollographql.apollo3.api.json.JsonReader
import com.apollographql.apollo3.api.json.JsonWriter

@Deprecated("Tests using MockQuery are very fragile to codegen changes, use integration tests instead")
class MockSubscription(
    private val queryDocument: String = "subscription MockSubscription { name }",
    private val variables: Map = emptyMap(),
    private val name: String = "MockSubscription",
) : Subscription {

  override fun queryDocument(): String = queryDocument

  override fun serializeVariables(writer: JsonWriter, responseAdapterCache: ResponseAdapterCache) {
    AnyResponseAdapter.toResponse(writer, variables)
  }

  override fun adapter(): ResponseAdapter {
    return object : ResponseAdapter {
      override fun fromResponse(reader: JsonReader, responseAdapterCache: ResponseAdapterCache): Data {
        reader.beginObject()
        reader.nextName()
        return Data(
            name = reader.nextString()!!
        ).also {
          reader.endObject()
        }
      }

      override fun toResponse(writer: JsonWriter, responseAdapterCache: ResponseAdapterCache, value: Data) {
        TODO("Not yet implemented")
      }
    }
  }

  override fun name(): String = name

  override fun operationId(): String = name.hashCode().toString()

  data class Data(val name: String) : Subscription.Data

  override fun responseFields(): List {
    return emptyList()
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy