Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

offlineExchange doesn't apply cyclic optimistic mutations #2478

Closed
jdkula opened this issue Jun 3, 2022 · 2 comments · Fixed by #2489
Closed

offlineExchange doesn't apply cyclic optimistic mutations #2478

jdkula opened this issue Jun 3, 2022 · 2 comments · Fixed by #2489
Labels
bug 🐛 Oh no! A bug or unintented behaviour.

Comments

@jdkula
Copy link

jdkula commented Jun 3, 2022

urql version & exchanges:

  • urql 2.2.1
  • urql/exchange-graphcache 4.4.2

Using exchanges [dedupExchange, offlineExchange, fetchExchange]

Steps to reproduce

A minimal reproduction is available here: https://github.com/jdkula/urql-graphcache-layering-bug-reproduction

Example

  1. Use offlineExchange with optimistic mutations
  2. Go offline
  3. Mutate your data so it ends up in a cycle (e.g. set value to 0, set value to 1, set value to 0)
  4. Note that any additional mutations on that data don't change state

Expected behavior

Cyclical mutations should be allowed (e.g. set value to 0, set value to 1, set value to 0, set value to 1, ...)

Actual behavior

Additional mutations once the cycle is established seem to be lost (it appears to be updating the resultant optimistic result somewhere deeper in the cache's optimistic layers, according to the calculated hash/key of the mutation/operation).

@jdkula jdkula added the bug 🐛 Oh no! A bug or unintented behaviour. label Jun 3, 2022
@kitten
Copy link
Member

kitten commented Jun 3, 2022

Hm, this could be a regression in regards to:

@JoviDeCroock
Copy link
Collaborator

I have managed to reproduce this in a test but generally this feels like it has never worked, it only happens when the second mutation has the same key

it('supports updating an optimisticly updated entity', () => {
  const store = new Store({
    optimistic: {
      updateTodo: (args) => ({
        __typename: 'Todo',
        id: args.id,
        complete: args.completed,
      }),
    },
  });

  const todosData = {
    __typename: 'Query',
    todos: [
      { id: '0', complete: false, text: '0', __typename: 'Todo' },
      { id: '1', complete: false, text: '1', __typename: 'Todo' },
    ],
  };

  write(store, { query: Todos }, todosData);

  const updateTodo = gql`
    mutation($id: ID!, $completed: Boolean!) {
      __typename
      updateTodo(id: $id, completed: $completed) {
        __typename
        complete
        id
      }
    }
  `;

  writeOptimistic(store, { query: updateTodo, variables: { id: '0', completed: true } }, 1);

  let queryRes = query(store, { query: Todos });
  expect(queryRes.partial).toBe(false);
  expect(queryRes.data.todos[0].complete).toEqual(true);

  writeOptimistic(store, { query: updateTodo, variables: { id: '0', completed: false } }, 2);

  queryRes = query(store, { query: Todos });

  expect(queryRes.partial).toBe(false);
  expect(queryRes.data.todos[0].complete).toEqual(false);

  writeOptimistic(store, { query: updateTodo, variables: { id: '0', completed: true } }, 1);
  queryRes = query(store, { query: Todos });
  expect(queryRes.partial).toBe(false);
  expect(queryRes.data.todos[0].complete).toEqual(true);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Oh no! A bug or unintented behaviour.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants