Android Jetpack Navigation | Passing arguments between fragments with codegen classes

Hi!
I just started using Apollo Kotlin Client for my project and I was trying to pass data between Android Fragments using NavArgs and Apollo Codegen Classes but I’m getting an error when building the project.

Caused by: java.lang.ClassNotFoundException: Didn’t find class “com.domain.app.ChargesQuery.Item” on path: DexPathList[[zip file “/system/framework/org.apache.http.legacy.boot.jar”, zip file “/data/app/com.domain.app-hkYvA2acDY3-UrickENP5g==/base.apk”],nativeLibraryDirectories=[/data/app/com.domain.app-hkYvA2acDY3-UrickENP5g==/lib/arm64, /system/lib64, /system/vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)

The class generated (simplified without body to don’t extend the post):
public data class ChargesQuery(
public val filter: Optional<SearchableChargeFilterInput?> = Optional.Absent,
public val size: Optional<Int?> = Optional.Absent,
public val from: Optional<Int?> = Optional.Absent,
) : Query<ChargesQuery.Data> {
public data class Data(
public val charges: Charges,
) : Query.Data

public data class Charges(
public val items: List,
public val total: Int,
)

public data class Item(
public val __typename: String,
/**
* Synthetic field for ‘ChargeFields’
*/
public val chargeFields: ChargeFields,
)
}

And here Im defining the argument in the navigation graph with the class:



Is it possible you use generated classes to pass data like I’m trying to do?

Thanks!!

Hi! :wave:

I’m afraid this shouldn’t work because you’re only allowed to pass classes that implement Serializable or Parcelable in your navigation graph (doc), which is not the case of the data classes generated by Apollo.

To be fair, it’s also recommended to only pass simple data to the destinations, and have them load the full data accordingly.

1 Like

One workaround if you want to pass the generated models between fragments would be to serialize to/from Json Strings since we have parsers already. See parsing a response body for some details how to do this.

But overall I agree with @Bod. The typical way to do navigation arguments would be to pass an “id” and retrieve the data from either the normalized cache or a ViewModel in your app.

1 Like

Thanks @Benoit_Lubek and @mbonnin for the support!! :grinning:
Talking with my colleague who manages the Backend GraphQL server, we decided to just pass the id as a String and retrieve the data from the normalized cache. Is working now!

One thing to mention is that I had to extend the GraphQL Schemma using the file extend.graphqls but even that the project build and it works, In Android studio I can see error for @ typePolicy and for @ fieldPolicy

Screenshot 2022-04-19 at 15.27.05

Oh that is because these directives are specific to Apollo Kotlin and the GraphQL plugin for Android Studio doesn’t know about them. One workaround is to suppress the warning, since it is a “real” issue, by adding this comment to the top of your file:

# noinspection GraphQLUnresolvedReference
1 Like

Thanks @Benoit_Lubek ! I added that comment but still have some errors

Screenshot 2022-04-19 at 17.45.29

Oh yeah in fact there are 2 types of warning basically about the same issue :sweat_smile: You can ignore both like this:

# noinspection GraphQLUnresolvedReference,GraphQLMissingType

Amazing @Benoit_Lubek ! Thanks for the update!! :smiley: