Apollo Kotlin: How to manually serialize the generated data classes from a Query?

I need to (de)serialize to and from JSON some of the data classes that are autogenerated by Apollo Kotlin. Normally I would just use a library like Moshi or kotlinx.serialization and add the proper annotation to the class I want to serialize but, since this data classes are autogenerated, I can’t do that.

But Apollo Kotlin is already (de)serializing them so I was wondering if there is a way to use the internal serialization mechanism from Apollo Kotlin to (de)serialize these autogenerated data classes.

I saw that the Query has already something related to this:

public data class ExpertSearchQuery(
  public val searchString: Optional<String?> = Optional.Absent,
  public val page: Optional<Int?> = Optional.Absent,
  public val pageSize: Optional<Int?> = Optional.Absent,
) : Query<ExpertSearchQuery.Data> {
  public override fun id(): String = OPERATION_ID

  public override fun document(): String = OPERATION_DOCUMENT

  public override fun name(): String = OPERATION_NAME

  public override fun serializeVariables(writer: JsonWriter,
      customScalarAdapters: CustomScalarAdapters): Unit {
    ExpertSearchQuery_VariablesAdapter.toJson(writer, customScalarAdapters, this)
  }

  public override fun adapter(): Adapter<Data> = ExpertSearchQuery_ResponseAdapter.Data.obj()

But I need to (de)serialize only a single class from the above query:

  public data class Expert(
    public val __typename: String,
    public val profileId: String?,
    public val firstName: String?,
    public val middleName: String?,
    public val lastName: String?,
    public val imageUrl: String?,
    public val titles: List<String?>?,
    public val primaryAffiliation: PrimaryAffiliation?,
  )

Is it possible to do this?

1 Like