Repository navigation
index.TypeAlias.RequireCanOnResult
type RequireCanOnResult<TSubjectMap> = <K, TResult, TParent>(action, subject, getSubjectData?) => Rule;Defined in: packages/graphql-casl/src/createCan.ts:324
Builds a rule that runs the resolver first and authorizes what it returned.
TSubjectMap extends Record<string, object>
The subject map, e.g. SubjectMap<Resolvers, ResolversTypes>.
K extends keyof TSubjectMap & string
TResult = unknown
TParent = unknown
K
(result, parent) => Partial<TSubjectMap[K]>
The pre-execution form checks what the client asserted — a condition built
from args.userId says nothing about the record the resolver actually loads,
which is how a caller passes their own userId alongside someone else's id.
This form has the real record in hand, so can('read', typed('Note', note))
evaluates the ability's conditions against the row that is about to be
returned. That is what CASL conditions are for.
When the field resolves to a list, every element must pass or the whole field
is denied. Filtering a list down to the permitted rows is a different
operation, and is not what a gate does. A null result is returned as-is —
there is no subject to authorize.
Without getSubjectData the resolved value is the subject data, which is the
common case. Supply one to pull the subject out of a wrapper, or to authorize
a projection of the record. It receives each candidate individually, so a list
calls it once per element, plus the resolver's parent as a second argument.
A rule built this way is not a CheckableRule: its verdict requires the resolved value, so it cannot be evaluated speculatively and the combinators reject it as an operand.
The resolver runs before the check. That is inherent — the check needs the result. It makes this form unsuitable for anything with side effects, so a rule built here refuses to guard a root mutation field, before the resolver runs rather than after. For those, check the args up front with the pre-execution form, or write a Rule by hand.
Query: {
// authorizes the Note the resolver actually loaded, not `args.id`
note: canUser.onResult(Actions.read, Subject.Note),
},