使用 KMM Apollo Client 时如何从 Swift 提供突变输入

问题描述 投票:0回答:0

我正在使用 Kotlin Multiplatform Mobile 和 Apollo-Kotlin 库。

举个例子,假设我有以下突变:

mutation InsertItem($input: ItemInsertInput!) {
    insertIntoItemCollection(objects: [$input]) {
        affectedCount
        records {
            ...Item
        }
    }
}

ItemInsertInput
包含 20 个可选字段,称为
prop1
,
prop2
, ...,
prop20
.

Apollo 将为

ItemInsertInput
生成以下类,其中所有属性都是只读的:

public data class ItemInsertInput(
  public val prop1: Optional<String?> = Optional.Absent,
  public val prop2: Optional<String?> = Optional.Absent,
  // ...
  public val prop20: Optional<String?> = Optional.Absent,
)

然后我在共享的 Kotlin 代码中有一个存储库,其中包含一个可以从 Swift 调用以插入项目的方法:

class ItemRepository {
  suspend fun insertItem(input: ItemInsertInput) {
    val response = apolloClient
      .mutation(InsertItemMutation(input))
      .execute()
  }
}

在 Swift 中,我想调用此方法并提供

ItemInsertInput
,通常只有 2 或 3 个实际设置的 20 个属性。

我尝试在 Swift 中实例化

ItemInsertInput
的新实例,但我需要为所有可选字段提供一个值,这在有 20 个属性时不适合。

此外,由于 Apollo 生成的

ItemInsertInput
只能访问所有属性,我无法在 Kotlin 代码中实例化
ItemInsertInput
的默认实例,然后稍后在 Swift 中设置这些属性。

我似乎需要某种生成器,我可以在其中实例化一个

ItemInsertInput
实例,同时仅指定 2 或 3 个属性,但我不确定如何在不为每个突变输入类编写大量手动样板的情况下执行此操作。

KMM 中是否有关于我应该如何从 Swift 提供变异输入的既定模式?

swift kotlin kotlin-multiplatform
© www.soinside.com 2019 - 2024. All rights reserved.