Fetch Policies
A request's FetchPolicy
determines how it is resolved by Ferry, either from the gql_link
, from the Cache, or both.
For example, executing the following reviewsReq
will bypass the cache and return results only from the network.
final reviewsReq = GReviewsReq(
(b) => b
..fetchPolicy = FetchPolicy.NetworkOnly
..vars.first = 10
..vars.offset = 0,
);
Valid options for FetchPolicy
include:
CacheFirst
: Return result from cache. Only fetch from network if cached result is not available.CacheAndNetwork
: Return result from cache first (if it exists), then return network result once it's available.NetworkOnly
: Return result from network, fail if network call doesn't succeed, save to cache.CacheOnly
: Return result from cache.NoCache
: Return result from network, fail if network call doesn't succeed, don't save to cache
Default FetchPolicies
Ferry uses the following defaults for each GraphQL operation type:
- Query:
CacheFirst
- Mutation:
NetworkOnly
- Subscription:
CacheAndNetwork
You can override these defaults by including a defaultFetchPolicies
map when initializing the client.
import 'package:ferry/ferry.dart';
import 'package:gql/ast.dart';
final client = Client(
link: MyLink(),
defaultFetchPolicies: {
OperationType.query: FetchPolicy.CacheAndNetwork,
},
);