Apollo3 Kotlin code generation backwards compatibility

I have a project that uses Apollo v2 right now and I’m exploring the option of migrating to Apollo3. The migration doc looks pretty sweet, where it says

It also allows to run version 2 and version 3 side by side if needed.

And with the codegen compat mode

Similar to operationBased, except it generates models that are compatible with Apollo Android v2. This codegen helps projects upgrade from v2 to v3.

This seems to be very useful since I can slowly convert parts of my project to v3. But after some experiments, I’m getting very confused because, it doesn’t matter what I put in my gradle file, the generated codes seem to always import from apollo3 com.apollographql.apollo3. But I can’t use these classes with the v2 ApolloClient. So how exactly is Apollo3 backwards compatible and how can I use v3 and v2 side by side? If anyone can provide some examples, that’d be much appreciated. Thank you!

Btw in gradle file, I tried

apollo {
    useVersion2Compat()
}

I also tried

apollo {
    packageNamesFromFilePaths()
    codegenModels.set("compat")
}

Hi :wave:

You can use (v2 codegen + v2 runtime) and (v3 codegen + v3 runtime) side by side but you can’t runt (v3 codegen + v2 runtime), that won’t work.

If you want to migrate progressively, I would recommend doing another module that uses the v3 codegen and start moving your models there progressively. Your app module can then have both v2 and v3 runtime and use one or the other depending what version the models are generated with.

I put together a small repo there: GitHub - martinbonnin/apollo-side-by-side. It’s very basic but it shows an app module with both v2 and v3 clients using queries from either the app module (v2) or the apollo3 module (v3).

All in all it all depends how many queries you have but unless you have a lot, you can also move everything all at once and save you the trouble of the intermediate state.

Hope this helps, let us know how that goes!

Hi @mbonnin , that make sense! So I’m guessing when using v3 codegen compat mode, you still have to use v3 runtime but it will be less code change needed compare to the other modes? And in that way, it helps upgrading v2 to v3?

Also, thank you so much for the sample app! :pray:

you still have to use v3 runtime but it will be less code change needed compare to the other modes

Exactly! It turns on a few options in codegen that make the models closer to v2. It’s not perfect but it’ll ease the transition. Then you can turn individual options off when you’re ready.

1 Like