Repository navigation
index.TypeAlias.PermissionsMap
type PermissionsMap<TResolvers> = { [TypeName in keyof TResolvers | Wildcard]?: Rule | { [FieldName in (TypeName extends keyof TResolvers ? FieldNamesOf<TResolvers[TypeName]> : AnyFieldName<TResolvers>) | Wildcard]?: Rule } };Defined in: packages/graphql-casl/src/rules.ts:633
The permissions map applied to a schema via applyPermissions.
TResolvers = AnyResolvers
Your generated Resolvers type. Defaults to
AnyResolvers, which accepts any type and field name and leaves the
checking to applyPermissions's runtime schema walk.
Every key is validated against your generated Resolvers: type names come
from keyof TResolvers and field names from each type's resolver keys, so a
mistyped or unknown type/field is a compile error. Each type key is optional
and maps to either a single Rule (applied to every field of the type)
or a per-field map of rules.
The abstract-type discriminators __isTypeOf and __resolveType are excluded
from the field keys — graphql-middleware never wraps them, so a rule attached
to one would never run.
An interface is a type key too: its field keys are the fields it declares (as
typescript-resolvers emits them on the interface's resolver type), and a
rule under it guards that field on every implementing type. A union's resolver
type has no field keys, so only '*' can be attached to one.
Wildcard ('*') is accepted in either position. Wildcards never
compose: exactly one rule guards a field, and the most specific entry wins.
From highest precedence to lowest, for a Note implementing Node:
-
{ Note: { body: rule } }— a named field of a named type -
{ Node: { body: rule } }— a named field of an interface the type implements -
{ Note: { '*': rule } }(or{ Note: rule }) — any field of a named type -
{ Node: { '*': rule } }(or{ Node: rule }) — any field of any implementor (a union's'*'sits here too, for its members) -
{ '*': { body: rule } }— a named field of any type -
{ '*': { '*': rule } }(or{ '*': rule }) — any field of any type
Below all of those sits applyPermissions's fallbackRule option, and below
that, no guard at all. Two interfaces that both cover a field of one
implementor, at the same tier, are ambiguous: applyPermissions rejects the
map until the implementor's own entry chooses.
const permissions: PermissionsMap<Resolvers> = {
Query: { me: canUser(Actions.read, Subject.User) },
Mutation: { requestMagicLink: accept, deleteNotes: deny },
Note: { '*': canUser(Actions.read, Subject.Note), id: accept },
};