Why Apollo-android is not generating custom types even after adding custom mapping in the gradle?

1.7k Views Asked by At

Steps i am following to implement custom types in Android

  1. I have added customTypeMapping in the app's gradle file.

  2. Also created a customtype Adapater.

Problem

Next i am trying to add it to the apollo client where i have to pass customtype and the adapter but the problem is that i am not getting any custom type that i am generating. Apollo only has ID,Numeric,Date etc there but the custom type i am mapping isn't being generated by the apollo-client.

I am using official docs https://github.com/apollographql/apollo-android#custom-scalar-types to implement it

2

There are 2 best solutions below

1
On

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

You will need to write the code for the adapter that you pass to addCustomTypeAdapter() in addition to specifying customTypeMapping in your gradle file. See https://github.com/apollographql/apollo-android#custom-scalar-types for an exemple of adapter for java.util.Date

Apollo cannot generate the custom typse as by definition this is a type you control.

PS: please do not cross post on stack overflow + github as it duplicates the effort to reply and scatters information.

0
On

In your apps build.gradle add this:


android {
   ...

    apollo {
        customTypeMapping = [ "customtype":"java.lang.String"] //or the appropriate type mapping
    }
}

In where you build your apollo client:

        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
    }