【问题标题】: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 中实现自定义类型的步骤

  1. 我在应用的 gradle 文件中添加了 customTypeMapping。

  2. 还创建了一个自定义类型的适配器。

问题

接下来我试图将它添加到我必须传递 customtype 和适配器的 apollo 客户端,但问题是我没有得到任何我正在生成的自定义类型。 Apollo 只有 ID、Numeric、Date 等,但我正在映射的自定义类型不是由 apollo-client 生成的。

我正在使用官方文档https://github.com/apollographql/apollo-android#custom-scalar-types 来实现它

【问题讨论】:

    标签: android graphql apollo apollo-android


    【解决方案1】:

    来自https://github.com/apollographql/apollo-android/issues/1702

    除了在 gradle 文件中指定 customTypeMapping 之外,您还需要为传递给 addCustomTypeAdapter() 的适配器编写代码。有关 java.util.Date 的适配器示例,请参阅 https://github.com/apollographql/apollo-android#custom-scalar-types

    Apollo 无法生成自定义类型,因为根据定义,这是您控制的类型。

    PS:请不要在堆栈溢出+ github上交叉发布,因为它重复了回复和分散信息的努力。

    【讨论】:

    • 这里是实现 gist.github.com/Nau4man/bd3f04713e1960a12411c2dc47bd57ae 但不是 apollo 客户端自动生成实现 inputType 的 CustomType.java 枚举,我们稍后会使用它添加到我们的 apollo 客户端,例如 .addCustomTypeAdapter(CustomType .#the-type-apollo-generate,我们的适配器)
    【解决方案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
        }
    

    【讨论】:

      猜你喜欢
      • 2011-10-15
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      • 2017-02-20
      • 2020-03-25
      • 2019-01-25
      • 2012-12-23
      • 1970-01-01
      相关资源
      最近更新 更多