首页 > 解决方案 > GraphQL - Apollo - Android - Kotlin - 自定义类型即使在 build.gradle 中指定后也不会生成

问题描述

我正在为我的项目使用 Apollo Client 和 Graph Ql,我正在尝试使用CustomTypeAdapter其中一个字段并将其映射schema到自定义字段。但是生成的自定义类型不包含我在build.gradle.

以下是我的应用程序级别build.gradle

 apollo {
        generateKotlinModels.set(true) // or false for Java models
        customTypeMapping = [
                "PersonDetail": "com.task.databasepoc.models.Person"
        ]
    }

下面是我的项目级 build.gradle:

dependencies {
        classpath "com.apollographql.apollo:apollo-gradle-plugin:2.0.3"
    }

下面是我的CustomTypeAdapter

object TypeAdapter : CustomTypeAdapter<Person> {
        override fun decode(value: CustomTypeValue<*>): Person {
            return value as Person
        }

        override fun encode(value: Person): CustomTypeValue<*> {
            return CustomTypeValue.fromRawValue(value)
        }


    }

我正在尝试构建ApolloClient,如下所示:

fun getApolloClient(): ApolloClient {
        val httpLoggingInterceptor = HttpLoggingInterceptor()
        httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY

        val okHttpClient = OkHttpClient.Builder()
            .addInterceptor(httpLoggingInterceptor)
            //.addInterceptor(AuthHeaderInterceptor(authStorageService))
            //.addInterceptorInterceptor(CorrelationIdInterceptor(correlationIdProvider))
            .connectTimeout(10000, TimeUnit.MILLISECONDS)
            .writeTimeout(30000, TimeUnit.MILLISECONDS)
            .readTimeout(30000, TimeUnit.MILLISECONDS)
            .build()
        return ApolloClient.builder()
            .serverUrl("serverUrl")
            .okHttpClient(okHttpClient)
            .addCustomTypeAdapter(CustomType.PersonDetail, TypeAdapter)
            .build()
    }

CustomType类不包含PersonDetail.

我试过了rebuildingsyncing一切都没有用。

谁能告诉我我在这里缺少什么?

标签: androidkotlingraphqlbuild.gradle

解决方案


就我而言,问题是在我将库从1.XX升级到2.5.7并尝试了许多解决方案之后,这对我有用

apollo {
    generateKotlinModels = false
    customTypeMapping = ['JSONObject' :"org.json.JSONObject" ]
}

还需要更改您的导入

import com.apollographql.apollo.response.CustomTypeAdapter
import com.apollographql.apollo.response.CustomTypeValue

import com.apollographql.apollo.api.CustomTypeAdapter
import com.apollographql.apollo.api.CustomTypeValue

推荐阅读