Skip to content

index.Function.grants

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

Function: grants()

function grants(inner, scope): Rule;

Defined in: packages/graphql-casl/src/grants.ts:139

Wraps a rule so that, when it passes and the resolver returns, what it returned is granted scope for the rest of the request — the object itself, or each object of a list.

Parameters

inner

Rule

The rule that authorizes the field. Its verdict is unchanged.

scope

string | readonly string[]

The scope name, or a list of names, to grant.

Returns

Rule

A rule with inner's verdict that also tags what the field returned.

Remarks

A child field guarded by granted(scope) then passes on those objects without a check of its own.

Query: { posts: grants(canUser(Actions.read, Subject.Post), 'post') },
Post: race(granted('post'), canUser.fields(Actions.read, Subject.Post)),

The grant is made after the wrapped rule has let the resolver run and the resolver has answered, so a denial grants nothing, a resolver error grants nothing, and a post-execution rule (canUser.onResult) that rejects a row grants nothing either — only what the field actually hands to its children is tagged. null and scalars are ignored: they cannot be a parent.

Grants hang off the request's context object in a WeakMap, so they are unreachable once the request is and a second request sees none of them. A context that is not an object cannot be a WeakMap key, so such a request grants nothing and every granted rule in it denies — the safe direction.

A grant does not inherit transitively. Post.author returns a User; the User is not granted 'post', and User's fields still need their own rule — or Post.author grants in turn.

Any rule can be wrapped: one built by rule() or createCan, a combinator, canUser.onResult(...), scopeArgs(...), a wrap(...). The result is a plain Rule, never a CheckableRule: it decides by running the resolver, so it cannot be an operand of and / or / not / chain / race. Compose it with wrap instead, or combine the rule inside it.

API Reference

Clone this wiki locally