Skip to content

index.TypeAlias.RequireCanFields

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

Type Alias: RequireCanFields<TSubjectMap>

type RequireCanFields<TSubjectMap> = <K, TParent, TArgs>(action, subject, getSubjectData?) => CheckableRule;

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

Builds a single rule that guards every field of a type, deciding each one from CASL's field-level permissions rather than from a hand-written entry.

Type Parameters

TSubjectMap

TSubjectMap extends Record<string, object>

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

Type Parameters

K

K extends keyof TSubjectMap & string

TParent

TParent = unknown

TArgs

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

Parameters

action

Action

subject

K

getSubjectData?

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

Returns

CheckableRule

Remarks

can('read', 'User', ['id', 'name']) already says which fields may be read. Restating that as a PermissionsMap entry per field duplicates it, and the two drift. Attached to a type, this rule checks ability.can(action, subject, info.fieldName) for whichever field is being resolved, so one ability rule drives them all:

// abilities
can(Actions.read, Subject.User, ['id', 'name']);
can(Actions.read, Subject.User, ['email'], { id: userId }); // only your own

// permissions map — one entry, every field of User guarded
User: canUser.fields(Actions.read, Subject.User),

The subject defaults to the resolver's parent — for a field of User that is the User being read, which is what a field-level condition is about. Supply getSubjectData to project it. A buildSubject tagger is required, since the parent carries no __typename; when the parent is not an object (a root Query/Mutation field) the check falls back to the bare subject name, which asks whether the field is readable at all.

A field with no matching rule is denied, so this is deny-by-default across the type's fields — unlike the PermissionsMap, where an unnamed field is unguarded.

API Reference

Clone this wiki locally