[英]How to create a JSON CustomTypeAdapter in Apollo GraphQL on Android with Kotlin
我正在努力弄清楚如何向我的ApolloClient添加CustomTypeAdapter。
对于变异,我们的服务器需要json输入。 相应的iOS应用正在传递json字符串。
当我传递一个字符串时,我收到一条消息,询问是否忘记添加自定义类型。
这是我的尝试:
的build.gradle
apollo {
useSemanticNaming = true
customTypeMapping['ISOTime'] = "java.util.Date"
customTypeMapping['JSON'] = "java.lang.JSONObject"
}
这是实例化的地方。
val jsonCustomTypeAdapter = object : CustomTypeAdapter<JSONObject> {
override fun decode(value: CustomTypeValue<*>): JSONObject {
return JSONObject()
}
override fun encode(value: JSONObject): CustomTypeValue<*> {
return CustomTypeValue.GraphQLJsonString(value.toString())
}
}
mApolloClient = ApolloClient
.builder()
.serverUrl(baseUrl)
.addCustomTypeAdapter(jsonCustomTypeAdapter)
.normalizedCache(cacheFactory, CacheKeyResolver.DEFAULT)
.httpCache(ApolloHttpCache(cacheStore, null))
.okHttpClient(mHttpClient)
.build()
看来Apollo已经生成了实现ScalarType的CustomType枚举,但是我不确定是否或如何使用它。
@Generated("Apollo GraphQL")
public enum CustomType implements ScalarType {
JSON {
@Override
public String typeName() {
return "Json";
}
@Override
public Class javaType() {
return Object.class;
}
},
ID {
@Override
public String typeName() {
return "ID";
}
@Override
public Class javaType() {
return String.class;
}
}
}
我尝试了在apolloandroid github上给出的示例,但是它对我没有用,它是Java语言,将其转换为Kotlin后无法编译。
任何暗示或说服的方向将不胜感激。 谢谢。
事实证明,Apollo已经自动生成了类型,而我要做的就是在build.gradle中正确声明它。 我不需要向ApolloClient添加任何自定义类型的适配器。
注意:Json类型是由我们的服务器提供的。
apollo {
useSemanticNaming = true
customTypeMapping['ISOTime'] = "java.util.Date"
customTypeMapping['Json'] = "java.lang.String"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.