NerdWalletOSS / apollo-cache-policies

An extension of the Apollo 3 cache with support for advanced cache policies.
Apache License 2.0
156 stars 22 forks source link

add policy action storage object #10

Closed danReynolds closed 4 years ago

danReynolds commented 4 years ago

Adds a new storage property to policy actions so that they can persist metadata across multiple policy action runs. This storage object is modeled after the Apollo 3 field policies storage option here: https://www.apollographql.com/docs/react/caching/cache-field-behavior/#fieldpolicy-api-reference and serves effectively the same purpose.

If there is metadata that you would like stored across write/evict policies, the storage object will store it uniquely for the entity being operated on. A common example of needing a storage option is to compare previous and current field values.

Without a storage option, a write policy would only let you access the current properties of an entity, after the write. Now clients will be able to compare changes to fields which can be helpful in a number of scenarios:

CreateEmployeeResponse: {
  onWrite: {
    EmployeesResponse: (
      { readField },
      { ref, storage }
    ) => {
      const isActive = readField(
        'active',
        ref
      );

      const hasBecomeActive = isActive && !storage.isActive;
      if (hasBecomeActive) {
        doSomething();
      }

      storage.isActive = isActive;
    },
  },
};
danReynolds commented 4 years ago

How will this help facilitate the detection of disparate field changes? Is there a guaranteed order fields are resolved in?

Discussed offline