Skip to content

index.Function.accessibleBy

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

Function: accessibleBy()

Call Signature

function accessibleBy<TSubjectMap, K>(
   ability, 
   action, 
   subject): 
  | AccessibleFilter<TSubjectMap[K]>
  | null;

Defined in: packages/graphql-casl/src/accessibleBy.ts:163

The filter matching every row of subject the ability permits action on, or null when it permits none.

Type Parameters

TSubjectMap

TSubjectMap extends Record<string, object>

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

K

K extends string

Parameters

ability

GraphQLAbility<TSubjectMap>

The caller's ability.

action

Action

The action to filter for, e.g. Actions.read.

subject

K

The subject name to filter, e.g. 'Note'.

Returns

| AccessibleFilter<TSubjectMap[K]> | null

The filter, or null if no row is accessible.

Remarks

null is a decision, not an absence: it means deny all, and the resolver should return an empty list without querying. Any other value — including {}, which means "no restriction" — is a filter to pass to the data layer.

Field-level rules are ignored; this answers which rows are reachable.

Examples

const filter = accessibleBy(ability, Actions.read, 'Note');
if (filter === null) return [];
return db.notes.find(filter);

A Prisma-shaped dialect:

const prismaFilter: FilterAdapter<object> = {
  rule: (conditions, inverted) => (inverted ? { NOT: conditions } : conditions),
  and: (filters) => ({ AND: filters }),
  or: (filters) => ({ OR: filters }),
  everything: () => ({}),
};

const where = accessibleBy(ability, Actions.read, 'Note', prismaFilter);
return where === null ? [] : prisma.note.findMany({ where });

A leaf adapter, which translates the comparisons too:

const prismaLeaves: FilterAdapter<object> = {
  leaf: ({ path, operator, value }) =>
    nest(path, { $eq: { equals: value }, $in: { in: value } }[operator] ?? value),
  not: (filter) => ({ NOT: filter }),
  and: (filters) => ({ AND: filters }),
  or: (filters) => ({ OR: filters }),
  everything: () => ({}),
};

Call Signature

function accessibleBy<TSubjectMap, K, TFilter>(
   ability, 
   action, 
   subject, 
   adapter): TFilter | null;

Defined in: packages/graphql-casl/src/accessibleBy.ts:182

The filter matching every row of subject the ability permits action on, in the dialect adapter describes, or null when it permits none.

Type Parameters

TSubjectMap

TSubjectMap extends Record<string, object>

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

K

K extends string

TFilter

TFilter

The dialect's filter type.

Parameters

ability

GraphQLAbility<TSubjectMap>

The caller's ability.

action

Action

The action to filter for, e.g. Actions.read.

subject

K

The subject name to filter, e.g. 'Note'.

adapter

FilterAdapter<TFilter>

The filter dialect. A SkeletonAdapter (one with rule) controls only the combinators and passes conditions through as written; a LeafAdapter (one with leaf) also receives each comparison individually.

Returns

TFilter | null

The filter, or null if no row is accessible.

API Reference

Clone this wiki locally