Skip to content

Stale network responses for previous variables are emitted when updating watchQuery variables in combination with FetchPolicy.cacheAndNetwork #1512

@jorg1piano

Description

@jorg1piano

When updating the variables of an observable, when using FetchPolicy.cacheAndNetwork responses on the network from the first requests are still emitted after the changing the variables.
The bug

final observable = client.watchQuery(
  WatchQueryOptions(
    document: someDocument,
    variables: {"id": 1},
    fetchPolicy: FetchPolicy.cacheAndNetwork,
  ),
);
observable.variables = {"id": 1}
observable.fetchResults(fetchPolicy: FetchPolicy.cacheAndNetwork);
// Cached result for id 1 returns immediately

observable.variables = {"id": 2}
observable.fetchResults(fetchPolicy: FetchPolicy.cacheAndNetwork);
// Cached result for id 2 returns immediately
// Then the network result for id 1 returns (this is the problem)
// Then network result for id 2 returns

Expected result:

observable.variables = {"id": 1}
observable.fetchResults(fetchPolicy: FetchPolicy.cacheAndNetwork);
// Cached result for id 1 returns immediately

observable.variables = {"id": 2} 
// Response to the previous fetch should be cancelled
observable.fetchResults(fetchPolicy: FetchPolicy.cacheAndNetwork);
// Cached result for id 2 returns immediately
// Network result returns for id 2

device / execution context
Device running Android 16
graphql_flutter: 5.2.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions