【问题标题】:Why Apollo-android is not generating custom types even after adding custom mapping in the gradle?为什么即使在 gradle 中添加了自定义映射后,Apollo-android 也没有生成自定义类型?
【发布时间】:2020-02-19 13:50:02
【问题描述】:
我在 Android 中实现自定义类型的步骤
-
我在应用的 gradle 文件中添加了 customTypeMapping。
-
还创建了一个自定义类型的适配器。
问题
接下来我试图将它添加到我必须传递 customtype 和适配器的 apollo 客户端,但问题是我没有得到任何我正在生成的自定义类型。 Apollo 只有 ID、Numeric、Date 等,但我正在映射的自定义类型不是由 apollo-client 生成的。
【问题讨论】:
标签:
android
graphql
apollo
apollo-android
【解决方案2】:
在你的应用 build.gradle 添加这个:
android {
...
apollo {
customTypeMapping = [ "customtype":"java.lang.String"] //or the appropriate type mapping
}
}
在您构建 apollo 客户端的位置:
val apolloClient = ApolloClient.builder()
.addCustomTypeAdapter(CustomType.YOUTTYPE, customAdapter)
.serverUrl(BASE_URL)
.okHttpClient(okHttpClient)
.build()
private var customAdapter: CustomTypeAdapter<String> = object : CustomTypeAdapter<String> {
override fun decode(@NotNull value: CustomTypeValue<*>): String = value.value.toString()
@NotNull
override fun encode(@NotNull value: String): CustomTypeValue<*> =
CustomTypeValue.GraphQLString(value)
//appropriate type mapping
}