Repository navigation
index.Function.accessibleBy
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.
TSubjectMap extends Record<string, object>
The subject map, e.g. SubjectMap<Resolvers, ResolversTypes>.
K extends string
GraphQLAbility<TSubjectMap>
The caller's ability.
The action to filter for, e.g. Actions.read.
K
The subject name to filter, e.g. 'Note'.
| AccessibleFilter<TSubjectMap[K]>
| null
The filter, or null if no row is accessible.
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.
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: () => ({}),
};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.
TSubjectMap extends Record<string, object>
The subject map, e.g. SubjectMap<Resolvers, ResolversTypes>.
K extends string
TFilter
The dialect's filter type.
GraphQLAbility<TSubjectMap>
The caller's ability.
The action to filter for, e.g. Actions.read.
K
The subject name to filter, e.g. 'Note'.
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.
TFilter | null
The filter, or null if no row is accessible.