Error converting to Json when using normalized cache in apollo-kotlin

Im trying to use the normalized cache in apollo-kotlin. Im seeing the following error when trying to use it:
java.lang.RuntimeException: java.lang.IllegalStateException: Cannot write com.apollographql.apollo3.api.ImmutableMapBuilder@176bfd90 to Json
This happens because the JsonWriter extension doesnt handle the Builder type. I have no idea why that is passed in though. Any ideas?

Hey :wave:. Thanks for sending this! Can you share your code? JsonWriter usually only handles “simple” types like Int, Double, String, Boolean and Map and List. Looks like you’re missing something to turn your MapBuilder into a Map somewhere ?

Thank you for your quick response.

Its a bit hard to share any code because its a corporate project. I will try to add as much details as possible though.

The project is set up to compile queries with a huge introspection schema backing it. It is a prisma generated schema which is > 100mb. The project uses the Java codegen. Im setting up the client like so:

apolloClient = ApolloClient.Builder()
            .serverUrl(gqlUrl)
            .addInterceptor(myInterceptor)
            .httpEngine(JdkHttpEngine(httpClient))
            .normalizedCache(
                normalizedCacheFactory = MemoryCacheFactory(maxSizeBytes = 20 * 1024 * 1024, expireAfterMillis = 86400000),
                cacheKeyGenerator = myCacheKeyGen)
            .build()

The only place in the Apollo Java codegen code I can see the ImmutableMapBuilder is used is in the Collections.kt file. It seems to add a builder mapped to a type name. Im pretty sure this builder should call build somewhere to make it work with the JsonWriter but I cant find it anywhere.

Good catch, looks like there’s a build missing there: apollo-kotlin/Collections.kt at 09e654447c87dd8c4177b796c590878ce8ee380d · apollographql/apollo-kotlin · GitHub

Do you want to open a pull request?

Will do. Thanks for your help!

1 Like