Skip to content

index.Function.wrap

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

Function: wrap()

function wrap(...rules): Rule;

Defined in: packages/graphql-casl/src/combinators.ts:430

Composes rules as nested middleware: each one receives the next as its resolve, and the last receives the field's real resolver.

Parameters

rules

...Rule[]

The rules to nest, outermost first. At least one.

Returns

Rule

A rule; not checkable.

Remarks

This is the escape hatch from the combinators' one restriction. and, or, not, chain and race all need to ask an operand for a verdict without side effects, so they accept only CheckableRules. wrap never asks — it just nests — so it accepts anything, including the rules that decide by running the resolver: createCan(...).onResult, scopeArgs(...), validateArgs(...) and grants(...).

Query: {
  notes: wrap(isNotBanned, scopeArgs(canUser, Actions.read, 'Note', { adapter })),
}

isNotBanned runs first. If it passes it calls what it thinks is the resolver, which is really the scoping rule, which narrows where and calls the resolver for real. Stacking works in both directions, so a field can be scoped and have its result re-checked:

Query: {
  notes: wrap(scopeArgs(canUser, Actions.read, 'Note', { adapter }),
              canUser.onResult(Actions.read, 'Note')),
}

The result is a plain Rule, never a CheckableRule, even when every operand happens to be checkable — a wrapper's verdict is only knowable by running it. So a wrap cannot itself be an operand of and / or / not / chain / race. When every operand is checkable, prefer chain: it means the same thing, costs no resolver nesting, and stays combinable.

Order matters and is left to right, outermost first. A rule that never calls its resolve stops the chain there, exactly as it would if it were the only rule on the field.

Example

Mutation: { publish: wrap(chain(isAuthenticated, isNotBanned), auditTrail) }

API Reference

Clone this wiki locally