Should Mutations Refresh Cache?


#1

I’m running into an issue where the returned data of mutations is not synchronizing with the query data in the Redux cache.

I have a simple example of a user profile form using Apollo. The container has 1 query viewer which returns a PublicUser type with a profile object. It also has a mutation called updateViewerProfile that updates the user’s profile. The mutation returns the type PublicUser with a profile object as well.

For example, when I fetch the user profile in the query it comes back as expected with a profile.name field of "Ted". Then when I call the mutation with an updated name field of "Bob" the response I get back in the mutation has a profile with the name of Bob. However, the cached data in the query has a name of Ted.

Is there a way around this? Having duped data on mutations and queries is a deal breaker for me but I wanted to check if anyone had a solution before I switched to the core apollo-client API.


#2

Do you define the dataIdFromObject option on the client? http://docs.apollostack.com/apollo-client/index.html#ApolloClient

I know it is not super intuitive and it is not super well documented, but as for now, Apollo Client has no way to correlate different objects in store without a unique identifier for every object.


#3

Ah thanks @slava for referring to this! I’m going to have to re-read the docs to catch up on new stuff :slight_smile:

So it sounds like if I return the id in both that might work… I’m hoping it might be as simple as (doc) => doc.id ? Once I get it working i’ll submit a PR for the docs :wink: :grinning:

Also I forgot to paste in the queries but it sounds like the above may do it (though i’ll need to return the id)

    query _($userId: ID!) {
      viewer(userId: $userId) {
        email
        username,
        profile {
          birthdate
          city
          country
          email
          firstName
          lastName
          state
          street
          telephone
          zipCode
        }
      }
    }


      mutation updateProfile {
        updateUserProfile(
          userId: "${state.viewer.id}",
          userProfile: {
            firstName: "${raw.first_name || ''}",
            lastName: "${raw.last_name || ''}",
            email: "${raw.email || ''}",
            birthdate: "${raw.birthdate || ''}",
            telephone: "${raw.telephone || ''}",
            street: "${raw.street || ''}",
            city: "${raw.city || ''}",
            state: "${raw.state || ''}",
            country: "${raw.country || ''}",
            zip_code: "${raw.zip_code || ''}",
          }
        ) {
          username, profile {
            birthdate, city, country, email, firstName, lastName,
            state, street, telephone, zipCode
          }
        }
      }

redux store (expanded keys)