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

commonMain.com.apollographql.apollo3.testing.mockserver.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.AnyAdapter
import com.apollographql.apollo3.api.CustomScalarAdapters
import com.apollographql.apollo3.api.Operation
import com.apollographql.apollo3.api.composeJsonResponse
import com.apollographql.apollo3.api.json.buildJsonString
import com.apollographql.apollo3.api.toJson
import com.apollographql.apollo3.mockserver.MockResponse
import com.apollographql.apollo3.mockserver.MockServer
import com.apollographql.apollo3.mockserver.enqueueString

fun  MockServer.enqueue(
    operation: Operation,
    data: D,
    customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty,
    delayMs: Long = 0,
) {
  val json = buildJsonString {
    operation.composeJsonResponse(jsonWriter = this, data = data, customScalarAdapters = customScalarAdapters)
  }
  enqueueString(json, delayMs)
}

fun MockServer.enqueueData(
    data: Map,
    customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty,
    delayMillis: Long = 0,
    statusCode: Int = 200,
) {

  val response = buildJsonString {
    AnyAdapter.toJson(this, customScalarAdapters, mapOf("data" to data))
  }

  enqueue(MockResponse.Builder()
      .statusCode(statusCode)
      .body(response)
      .delayMillis(delayMillis)
      .build())
}


fun MockServer.enqueueData(
    data: Operation.Data,
    customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty,
    delayMillis: Long = 0,
    statusCode: Int = 200,
) {
  val response = buildJsonString {
    beginObject()
    name("data")
    data.toJson(this, customScalarAdapters)
    endObject()
  }
  enqueue(MockResponse.Builder()
      .statusCode(statusCode)
      .body(response)
      .delayMillis(delayMillis)
      .build())
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy