Optimistic Updates on Android looks different from Docs

I tried to implement optimistic updates into my android app, here is what the docs say:

apolloClient.mutation(SetTodocompletedMutation(id = "1", completed = true))
  .optimisticUpdates(
    SetTodocompletedMutation.Data(
      id = "1",
      completed = true,
    )
  )
  .execute()

For me I cannot simply pass my arguments in Data(), in my case I can only pass a Data class:

graphqlAPI.mutation(SetFavoriteMutation(favoritedPost.id))
                                .optimisticUpdates(
                                    SetFavoriteMutation.Data(
                                        SetFavoriteMutation.SetFavorite(name = "Test", favorites = 20, id = favoritedPost.id, "Post")
                                    )
                                )
                                .execute()

Why is it different in my case? I’m also forced to pass a __typename as the last parameter of the data class instance which is also different to the explanation in the docs.

Here is my generated apollo mutation class:

public data class Data(
    public val setFavorite: SetFavorite?,
  ) : Mutation.Data

  public data class SetFavorite(
    public val name: String,
    public val favorites: Int?,
    public val id: Int,
    public val __typename: String,
  )

My watchers get notified but the Post is not getting updated with the name “Test” that I specified in the optimistic SetFavoriteMutation.
Why does my generated class looks different to the one in the docs? Why cant I pass the arguments directly and have to pass a dataclass instead with __typename parameter

Hi!

Just to be clear, you can execute a mutation without the optimistic option. In your case it would simply be:

graphqlAPI.mutation(SetFavoriteMutation(favoritedPost.id)).execute()

With the optimistic update option, you need 2 elements:

  • the mutation you are executing (with its arguments)
  • the piece of data you want to be in the cache temporarily (which can be a complex type), while the mutation is being executed

About the __typename field: it is not always generated, but is automatically added in selections when the parser will need to determine the type of a returned object. For instance when a field type is an interface, the actual concrete type will be known thanks to this field. Other than that I’m not sure what is different from the doc in your example?

That being said you should definitely get the “Test” value in your watchers. Could you share a bit more code so we can try to understand why it’s not working?