kamilkisiela / apollo-angular

A fully-featured, production ready caching GraphQL client for Angular and every GraphQL server 🎁
https://apollo-angular.com
MIT License
1.5k stars 311 forks source link

`watch` does not update query results unless a `fetchPolicy` is supplied #1648

Closed andreialecu closed 3 years ago

andreialecu commented 3 years ago

Describe the bug

This is a pretty weird one. We have a query that we're using .watch() on.

The APIs are generated by graphql-code-generator

We've been troubleshooting a mutation not updating a watched query. After randomly trying things, it appears that what fixed it was setting an explicit fetchPolicy.

Even using the default fetchPolicy seems to work, as long as something is specified. It cannot be undefined.

To Reproduce Steps to reproduce the behavior:

export class GetAppUserClubPermissionsGQL extends Apollo.Query<GetAppUserClubPermissionsQuery, GetAppUserClubPermissionsQueryVariables> {
  // ...
}

// appClubPermissions is of type GetAppUserClubPermissionsGQL

// this works
this.appClubPermissions.watch(
  { clubId },
  { fetchPolicy: "cache-first" }, // also network-only, network-first work
);

// this does NOT trigger observable updates
this.appClubPermissions.watch(
  { clubId },
);

// send a mutation now, and watch how just the first subscription updates

appClubPermissions

Expected behavior

fetchPolicy should be optional, and the query should update by default.

Environment:

Additional context

Not sure if relevant, but the type itself defines custom keyFields in the cache, like:

  const cache = new InMemoryCache({
    typePolicies: {
      AppUserPermissions: {
        keyFields: ['email'],
      },
    },
  });
andreialecu commented 3 years ago

Nevermind, the problem was that this project had configured no-cache as the default cache policy:

  const defaultOptions = {
    watchQuery: {
      fetchPolicy: 'no-cache',
      errorPolicy: 'ignore',
    },
    query: {
      fetchPolicy: 'no-cache',
      errorPolicy: 'all',
    },
  };