apollographql / apollo-client

:rocket:  A fully-featured, production ready caching GraphQL client for every UI framework and GraphQL server.
https://apollographql.com/client
MIT License
19.38k stars 2.66k forks source link

Observable doesn't fire when `notifyOnNetworkStatusChange` is true and data is reloaded but doesn't change #12069

Open PhoebeSzmucer opened 2 months ago

PhoebeSzmucer commented 2 months ago

Issue Description

Hi. I believe I found a bug in ObservaleQuery.subscribe. I'm attaching a CodeSandbox repro, but it's essentially this code:

const observable = client.watchQuery({
  query: ALL_PEOPLE,
  notifyOnNetworkStatusChange: true,
});
observable.subscribe({
  next: result => {
    console.log("received result", result);
  },
});

// Some time later
client.cache.modify({
  fields: { people: (_, { DELETE }) => DELETE },
});

// Outputs:
//
// received result {data: {}, loading: true, networkStatus: 1, partial: true}
// ...and then nothing :(

Essentially we receive a "loading" event with no data, and then nothing afterwards. This means that the consumer of this observable is stuck in a loading state.

This works correctly if the if the data actually changes after a reload, which means that it's likely because of an optimization that prevents calling next when nothing changed. I think the logic of isDifferentFromLastResult needs to be modified.

When notifyOnNetworkStatusChange is false no event is fired, but that seems correct.

Link to Reproduction

https://codesandbox.io/p/devbox/tender-framework-68wm4d?workspaceId=7e27fe59-1d44-40f8-b1d0-beb23c6580e8

Reproduction Steps

No response

@apollo/client version

3.11.8

alessbell commented 1 month ago

Hi there @PhoebeSzmucer 👋

Thanks for opening this issue! The reproduction is very clear, and this indeed looks like a bug. I've gone ahead and opened draft PR #12071 with a test that reproduces the issue. Our team is in the midst of conference season right now, but I hope to take a closer look soon. Thanks again!