Skip to content

index.Interface.RequireCan

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

Interface: RequireCan()<TSubjectMap>

Defined in: packages/graphql-casl/src/createCan.ts:212

The rule builder returned by createCan when a buildSubject tagger is provided.

Remarks

Supports both bare-subject checks and condition checks where the subject instance is built from resolver args via getSubjectData.

The subject name K narrows getSubjectData's return to that subject's fields (Partial<TSubjectMap[K]>); annotate getSubjectData's args parameter with your generated *Args type to type the extraction end to end.

getSubjectData also receives the resolver's parent, which is what a field-level rule needs — "read User.email only when it is your own user" is a condition on the parent User, not on the field's args. Annotate it with ParentOf<UserResolvers['email']> to type it. See the caveat on createCan about resolvers that project by selection set.

Type Parameters

TSubjectMap

TSubjectMap extends Record<string, object>

The subject map, e.g. SubjectMap<Resolvers, ResolversTypes>.

RequireCan<K, TArgs, TParent>(
   action, 
   subject, 
   getSubjectData?, 
   options?): CheckableRule;

Defined in: packages/graphql-casl/src/createCan.ts:213

The rule builder returned by createCan when a buildSubject tagger is provided.

Type Parameters

K

K extends string

TArgs

TArgs extends Record<string, unknown> = Record<string, unknown>

TParent

TParent = unknown

Parameters

action

Action

subject

K

getSubjectData?

(args, parent) => Partial<TSubjectMap[K]>

options?

CanRuleOptions

Returns

CheckableRule

Remarks

Supports both bare-subject checks and condition checks where the subject instance is built from resolver args via getSubjectData.

The subject name K narrows getSubjectData's return to that subject's fields (Partial<TSubjectMap[K]>); annotate getSubjectData's args parameter with your generated *Args type to type the extraction end to end.

getSubjectData also receives the resolver's parent, which is what a field-level rule needs — "read User.email only when it is your own user" is a condition on the parent User, not on the field's args. Annotate it with ParentOf<UserResolvers['email']> to type it. See the caveat on createCan about resolvers that project by selection set.

Properties

fields

readonly fields: RequireCanFields<TSubjectMap>;

Defined in: packages/graphql-casl/src/createCan.ts:237

Builds one rule that guards every field of a type, using CASL's field-level permissions.

See

RequireCanFields


onResult

readonly onResult: RequireCanOnResult<TSubjectMap>;

Defined in: packages/graphql-casl/src/createCan.ts:229

Builds a rule that authorizes the resolved value instead of the args.

See

RequireCanOnResult

API Reference

Clone this wiki locally