Skip to content
This repository has been archived by the owner on Jul 10, 2019. It is now read-only.

Cache updates do not persist with combined remote and local variables #312

Open
rickAllDev opened this issue Oct 5, 2018 · 0 comments
Open
Assignees

Comments

@rickAllDev
Copy link

Components: Apollo 2.1, apollo-cache-inmemory, apollo-link-state, apollo-link-rest

I am moving from React Redux to Apollo on client for a project and struggling a bit. I am reading a simple list from server and adding local state variable 'picked' which works fine. When I trigger a mutation to toggle 'picked', I can see the value of 'picked' change in the graphQL workbench. However, when I trigger another mutation, the data returned by the resolver query has the original value of 'picked', i.e., like the cache was never updated.

export const userList_QUERY = gql`
  query RestData($userID: String!) {
    List(userID: $userID)
      @rest(path: "user/devices/?{args}", method: "GET", type: "User") {
        __typename
        userID listID 
        picked @client
        }, 
  },
`;

The list is mapped to a series of checkboxes. When a checkbox is clicked, I am toggling the 'picked' variable using mutation and resolver:


export const UPDATE_LIST = gql`
  mutation ($userID: String!, $listID: String!, $state: Boolean!) {
    updateListPicks(userID: $userID, listID: $listID, state: $state ) @client 
  }
`;

updateListPicks: (_, { userID, listID, state }, { cache }) => {
      const previous = cache.readQuery({ query: userList_QUERY });
      var index = previous.List.map((item) => item.listID).indexOf(listID);
      let data = {};

      if(index !== -1) {
        previous.List[index].picked = state;
        data = { List: previous.List}
        cache.writeData({ data });
      }
      cache.writeData({ data });
      return null;
    },

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants