How to incorporate Apollo-android in a project with many modules?

422 Views Asked by At

I have an Android project that uses graphql to communicate with the server.

It has many modules. Each has its own graphql queries / mutations, but they share the same schema.

At the moment I am running into an issue where Apollo automatically generates multiple type.CustomType classes, and it seems that the top-level "app" module would get compiled with an implementation of type.CustomType from an arbitrary module.

Depending on what the module's queries are, the content of type.CustomType varies, thus I get crashes such as FieldNotFoundException even though I am not using reflection and the project compiles fine.

e.g.

java.lang.NoSuchFieldError: No static field LONG of type Lapi/type/CustomType; in class Lapi/type/CustomType; or its superclasses (declaration of 'api.type.CustomType' appears in /data/app/com.functorz.app-oV7ixVEf0e0CIt1sWzT4uA==/base.apk!classes6.dex)
        at com.functorz.network.GraphQLRequestUtil.initialSubscriptionClient(GraphQLRequestUtil.java:151)
        at com.functorz.network.GraphQLRequestUtil.<init>(GraphQLRequestUtil.java:111)
        at com.functorz.network.GraphQLRequestUtil.<clinit>(GraphQLRequestUtil.java:48)
        at com.functorz.network.GraphQLRequestUtil.getInstance(GraphQLRequestUtil.java:162)

The code on GraphQLRequestUtil line 151 is quite simple, it's the first line that makes reference to CustomType, i.e.

.addCustomTypeAdapter(CustomType.LONG, TypeUtil.LONG_TYPE_ADAPTER)

in

apolloSubscriptionClient = ApolloClient.builder()
        .serverUrl(subscriptionUrl)
        .okHttpClient(subscriptionOkHttpClient)
        .subscriptionTransportFactory(new WebSocketSubscriptionTransport.Factory(socketUrl, subscriptionOkHttpClient))
        .addCustomTypeAdapter(CustomType.LONG, TypeUtil.LONG_TYPE_ADAPTER)
        .addCustomTypeAdapter(CustomType.OFFSETDATETIME, TypeUtil.DATE_TIME_TYPE_ADAPTER)
        .addCustomTypeAdapter(CustomType.LOCALDATE, TypeUtil.LOCAL_DATE_TYPE_ADAPTER)
        .addCustomTypeAdapter(CustomType.SQLDATE, TypeUtil.SQL_DATE_TYPE_ADAPTER)
        .addCustomTypeAdapter(CustomType.URL, TypeUtil.URL_TYPE_ADAPTER)
        .addCustomTypeAdapter(CustomType.BIGDECIMAL, TypeUtil.DECIMAL_TYPE_ADAPTER)
        .build();

I think I am modularizing my project incorrectly. Does anyone have suggestions regarding how one should split up a project into multiple modules when all of them depend on Apollo?

0

There are 0 best solutions below