fromJson

open override fun fromJson(reader: JsonReader, customScalarAdapters: CustomScalarAdapters): Optional.Present<T>

Deserializes the given Json to the expected Kotlin type.

implementations may throw com.apollographql.apollo3.exception.JsonEncodingException or com.apollographql.apollo3.exception.JsonDataException on unexpected incoming data

Parameters

customScalarAdapters

configured instance of GraphQL operation response adapters cache. A global empty instance will be used by default.

Example:

override fun fromJson(reader: JsonReader, customScalarAdapters: CustomScalarAdapters): Hero {
var name: String? = null
var homeworld: String? = null

while (reader.hasNext()) {
when(reader.nextName()) {
"name" -> name = reader.nextString()
"homeworld" -> homeworld = reader.nextString()
}
}

return Hero(name!!, homeworld!!)
}

Alternatively, you can use the built-in AnyAdapter to simplify the parsing loop:

override fun fromJson(reader: JsonReader, customScalarAdapters: CustomScalarAdapters): Hero {
val map = AnyAdapter.fromJson(reader) as Map<String, String>

return Hero(map["name"]!!, map["homeworld"]!!)
}