android 将2个具有相同类型的查询graphql响应放入数组中

mnemlml8  于 2023-11-15  发布在  Android
关注(0)|答案(1)|浏览(149)

我有2个查询具有相同的类型


的数据
我尝试像这样将findOrderByOid插入到[FindOrderByRestaurantId]

var orderListData: ArrayList<FindOrderByRestaurantIdQuery.FindOrderByRestaurantId?> = ArrayList()
    fun getOrderList(){
        scope.launch {
            apolloBuilder.apolloBuild().query(
                FindOrderByRestaurantIdQuery(
                    id = "1"
                )
            ).execute().apply {
                orderListData = data?.findOrderByRestaurantId?.toCollection(ArrayList()) ?: ArrayList()
            }
        }
    }
    
    fun getOrderDetail(){
        scope.launch {
            apolloBuilder.apolloBuild().query(
                FindOrderByOidQuery(
                    oid = "15"
                )
            ).execute().apply {
                data?.findOrderByOid?.let {
                    orderListData.add(it)
                }
            }
        }
    }

字符串
但是,

Type mismatch.
Required: FindOrderByRestaurantIdQuery.FindOrderByRestaurantId?
Found: FindOrderByOidQuery.FindOrderByOid


我不能创建新的data class,然后逐个解析,因为OrderResult有近百个变量。
我如何得到返回响应为OrderResult??
这里我gradle:

plugins {
    id 'com.android.application' version '8.1.2' apply false
    id 'org.jetbrains.kotlin.android' version "1.9.0" apply false
    id 'com.apollographql.apollo3' version '3.8.2' apply false
    id 'org.jetbrains.kotlin.jvm' version '1.9.10' apply false
    ...
}
plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'com.apollographql.apollo3'
    ...
}

android {
    ...
    compileSdk 34
    defaultConfig {
        ...
        minSdk 21
        targetSdk 34
        ...
    }
    compileOptions {
        sourceCompatibility 17
        targetCompatibility 17
    }
    kotlinOptions {
        jvmTarget = '17'
    }
}

apollo {
    service("myapp") {
        packageName.set("com.myapp.app")
//        codegenModels.set("responseBased")
        introspection {
            endpointUrl.set("https://endpoint.com/data/")
            schemaFile.set(file("src/main/graphql/sg/com/myapp/app/schema.graphqls"))
        }
        customTypeMapping = [
                "Date"    : "kotlin.String",
                "DateTime": "kotlin.String",
                "Time"    : "kotlin.String",
                "BigInt"  : "kotlin.Long",
                "Double"  : "kotlin.Double"
        ]
    }
}

dependencies {
    ...
    implementation 'com.apollographql.apollo3:apollo-runtime:3.8.2'
    implementation 'com.apollographql.apollo3:apollo-api:3.8.2'
    ...
}

的字符串
如果我错过了什么,让我知道
谢谢

6tqwzwtp

6tqwzwtp1#

在我将问题插入apollo-kotlin后,我得到了答案
将字段graphql设置为片段

fragment orderFields on OrderResult {
  allServed
  billUrl
  # etc.
}

query FindOrderByOidQuery($oid: ID!) {
  findOrderByOid(oid: $oid) {
    ...allOrderFields
  }
}

query FindOrderByRestaurantIdQuery($id: ID!) {
  findOrderByRestaurantIdQuery(id: $id) {
    ...allOrderFields
  }
}

字符串
然后在Kotlin中,像这样设置响应

var orderListData: ArrayList<OrderField> = ArrayList()
    fun getOrderList(){
        scope.launch {
            apolloBuilder.apolloBuild().query(
                FindOrderByRestaurantIdQuery(
                    id = "1"
                )
            ).execute().apply {
                orderListData = data?.findOrderByRestaurantId?.toFilterNotNull().map { it.orderField }.toCollection(ArrayList()) ?: ArrayList()
            }
        }
    }
    
    fun getOrderDetail(){
        scope.launch {
            apolloBuilder.apolloBuild().query(
                FindOrderByOidQuery(
                    oid = "15"
                )
            ).execute().apply {
                data?.findOrderByOid?.let {
                    orderListData.add(it.orderField)
                }
            }
        }
    }

相关问题