Skip to content
On this page

update since v0.5

Updates Query after Mutation execution.

Formulae

ts
import { update } from '@farfetched/core';

update(query, { on, by: { success, failure } });

Arguments

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:
    • source - any external Store
    • fn - a function that accepts the Query, Mutation state and current value of the source and returns an object for new Query state

Arguments

An update rule function accepts two arguments:

  • state - an object with the following fields:

  • source - current value of the source field of the update rule object

state.query and state.mutation have the following structure:

ts
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:

ts
type RuleResult =
  | {
      result: QueryResult;
      refresh: boolean | { params: QueryParams };
    }
  | {
      error: QueryError;
      refresh: boolean | { params: QueryParams };
    };
  • If refresh is true, 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 refresh is an object, the Query will be immediately re-fetched with the given params.

  • 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.

Released under the MIT License.