Flutter: graphql_flutter FetchPolicy.noCache returns cached data from different user

2.2k views Asked by At

I'm using the graphql_flutter package and am having an issue with what seems to be caching. The client passes over cached data from one user to another, even when the FetchPolicy is set to noCache.

I’ve outlined below what my graphql client setup looks like and then the custom method I use for sending a query to the server (it fetches the items favorited by the user). I’ve looked through the Policies in the official documentation and believe my setup is correct. The issue (and the steps to reproduce) are the following:

  1. I sign in to my app with a test user account (using Amplify Cognito for authentication) and add some items to the user's favorites list (with a mutation request).
  2. I then navigate to a ‘Favorites’ summary page, where I call the below outlined fetchFavoritesData method to fetch all the items that were favorited by the user. Works like a charm.
  3. I then log out and create a completely new user account and log in with this new user (i.e. different access token and all).
  4. Before adding any items to the favorites list for this new user, if I check their ‘Favorites’ summary page, which calls the fetchFavoritesData method and should return null (because this user has yet to add any), the result actually returns the exact items that were favorited by the first user. Odd.

Been playing around with the Policies, tried different fetch policies and so on, but the problem remains. Am I missing something (sorry — totally new to this package) or could this be a bug?

GraphQLClient client = GraphQLClient(
  link: link,
  cache: GraphQLCache(), // declared cache here because I'm using the client for a few different requests, some where caching is preferred.
  );
}

Future<void> fetchFavoritesData() async {
  final QueryOptions options = QueryOptions(
    document: gql(queryFavoritesData),
    // only return result from network
    fetchPolicy: FetchPolicy.noCache,
    // ignore cache data if any
    cacheRereadPolicy: CacheRereadPolicy.ignoreAll,
  );

  final QueryResult result = await GraphQLConfiguration.client.query(options);
}

26/8/2021 UPDATE:

I've migrated over to Firebase for authentication and the error is no longer occurring. Not sure whether the authentication package was the causation or just a correlation but this is no longer an issue.

1

There are 1 answers

0
Mohammad Enawe On

Authentication not is the problem you can change default policies by initializing client like this:

client = GraphQLClient(
  link: HttpLink(
    GqlConstants.gql_endpoint,
    defaultHeaders: header,
  ),
  defaultPolicies: DefaultPolicies(
    query: Policies(
      cacheReread: CacheRereadPolicy.ignoreAll,
      fetch: FetchPolicy.noCache,
    ),
  ),
  cache: GraphQLCache(store: InMemoryStore()),
);

so by this code you can customize the policies one time. but also you can do it for single query by that:

QueryResult result = await client.query(
  QueryOptions(
    ...
    fetch: FetchPolicy.noCache,
  ),
);