Skip to content

index.Variable.CacheMode

github-actions[bot] edited this page Nov 30, 2026 · 3 revisions

Variable: CacheMode

const CacheMode: {
  Contextual: "contextual";
  NoCache: "no_cache";
  Strict: "strict";
};

Defined in: packages/graphql-casl/src/rules.ts:226

How long one rule may reuse a check's answer.

Type Declaration

Contextual

readonly Contextual: "contextual" = 'contextual';

NoCache

readonly NoCache: "no_cache" = 'no_cache';

Strict

readonly Strict: "strict" = 'strict';

Remarks

The default is 'no_cache': a rule attached to a type runs once per field per object, so a 100-row list with 5 selected fields evaluates it 500 times. That is free for a synchronous role check and expensive for the async rules this library encourages — a policy-engine round trip, a row load.

  • 'no_cache' — evaluate every time. The safe default; a check that reads something mutable, or that must observe every field, needs this.
  • 'contextual' — once per request. Correct when the answer depends only on the context: isAuthenticated, hasRole, an org-membership lookup.
  • 'strict' — once per request per (parent, args). Correct when the answer depends on the row being authorized, and still collapses the fields of one row into a single evaluation. The field name is not part of the key: a check that reads info.fieldName needs 'no_cache' or a CacheKey.

A CacheKey function replaces 'strict''s key with one of your own.

Caching is per rule and per request: entries hang off the context object in a WeakMap, so they are unreachable once the request is. A context that is not an object cannot be a WeakMap key, so such a rule is simply never cached.

The object names each mode: cache: CacheMode.Contextual.

API Reference

Clone this wiki locally