update since v0.5
Updates Query after Mutation execution.
Formulae
import { update } from '@farfetched/core';
update(query, { on, by: { success, failure } });Arguments
query- Query to updateon- Mutation to subscribe toby.success- a rule to update the Query on Mutation successby.failure?- a rule to update the Query on Mutation failure
Update rule
An update rule can be either:
- a function that accepts the Query and Mutation state and returns an object for new Query state
- an object with the following fields:
Arguments
An update rule function accepts two arguments:
state- an object with the following fields:source- current value of thesourcefield of the update rule object
state.query and state.mutation have the following structure:
type QueryState =
| null
| {
result: QueryResult;
params: QueryParams;
}
| {
error: QueryError;
params: QueryParams;
};
type MutationState =
| {
result: MutationResult;
params: MutationParams;
}
| {
error: MutationError;
params: MutationParams;
};Return value
A return value of the update rule has the following structure:
type RuleResult =
| {
result: QueryResult;
refresh: boolean | { params: QueryParams };
}
| {
error: QueryError;
refresh: boolean | { params: QueryParams };
};If
refreshistrue, the Query will be immediately re-fetched with the same parameters as it was fetched before the Mutation execution. If it was not fetched before, it will not be re-fetched.If
refreshis an object, the Query will be immediately re-fetched with the givenparams.While the Query is re-fetching, it will be marked as stale.
In case of returned
result, it will replace the current data in the Query.In case of returned
error, it will replace the current error in the Query.