convoyinc / apollo-cache-hermes

A cache implementation for Apollo Client, tuned for performance

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Stale data passed to Query prop after writeFragment operation

jync opened this issue · comments

commented

Summary:

We use the following stack:

    "apollo-cache-hermes": "0.6.3",
    "apollo-cache-persist": "0.1.1",
    "apollo-client": "2.3.5",

We have the following type structure

type AddressBook = {
   contacts: {
       pageInfo: PageInfo,
       edges: Array<ContactEdge>
   }
}

type PageInfo = {
  hasNextPage: boolean
  ... etc ...
}

type ContactEdge = {
   cursor: ?string,
   node: Contact
}

type Contact = {
  name: string
  ...etc...
}

When we execute a mutation to add a new contact, we run a mutation updater which updates the hermes cache by prepending the contact as an edge in the following manner:

const AddressBookContactFragment = gql`
  fragment AddressBookContact on Contact {
    id
    name
    mobile
  }
`;

const UpdateFragment = gql`
  fragment AddressBookFragment on AddressBook {
    id
    contacts(first: 1) @static {
       edges {
          cursor
          node {
            ...AddressBookContact
          }
       }
    }
  }
  ${AddressBookContactFragment}
`

hermes.writeFragment({
   id: <nodeId>,
   fragment: UpdateFragment,
   fragmentName: 'AddressBookFragment',
   data: {
     __typename: 'AddressBook',
     id: 'addressBookNodeId',
     contacts: {
       edges: [
         {
            __typename: 'ContactsEdge',
           cursor: null,
           node: {
             id: 'contactNodeIdNew',
             __typename: 'Contact'
            ... etc...
           }
         },
         { ... edge2... }, { ...edge3... }
       ]
     }
   }
})

This updates the Query prop but when I log out what is returned in the query prop, it shows only a contacts collection with { ... edge2... }, { ...edge3... } and not the newly created contactNodeIdNew contact.

If we reload the screen by navigating out and back in, the Query prop returns the expect total set of contacts including the newly created contactNodeIdNew.

Is there anything we might be missing that could be causing this issue? What would cause the data passed from Hermes to the Query prop to be stale when compared to it being queried directly when the Query prop mounts?

Expected Behavior:

The collection data returned from the query prop whether acquired directly from a graphql query on mount, or via update from hermes should be the same. Instead, updates via hermes appear to be stale data.

Actual Behavior:

Stale data tends to be passed to the Query prop after firing a writeFragment operation.

commented

Ok... figured out the problem, albeit it was hard to ascertain the link. The reason data was stale is because underlying the Query component is a network error. As a consequence, Apollo was returning the baseline data rather than the optimistic dataset.