Repository navigation
index.Interface.ApplyPermissionsOptions
Defined in: packages/graphql-casl/src/applyPermissions.ts:62
Options for applyPermissions.
optional allowExternalErrors?: boolean;Defined in: packages/graphql-casl/src/applyPermissions.ts:119
Whether an error thrown by the resolver of a permitted field reaches the client unchanged.
Defaults to true.
Setting it to false replaces those errors with fallbackError, so an
internal failure — a database message, a stack-revealing library error —
cannot leak through a guarded field. It has no effect without a
fallbackError to replace them with.
This default is the opposite of graphql-shield's, which masks by
default. Masking is the safer behaviour, but it is not what this library has
done since 1.0, and silently swallowing resolver errors on upgrade would be
worse than leaving the choice explicit. Set it to false deliberately, or
set strict, which defaults it to false along with the other 2.0
default.
optional debug?: boolean;Defined in: packages/graphql-casl/src/applyPermissions.ts:135
Whether an error raised inside a rule is rethrown untouched.
Defaults to false.
A rule that breaks — a getAbility that throws, a check with a bug — is not
a denial, but a fallbackError replaces it just like one, so internals do
not leak. That makes it indistinguishable from a legitimate denial while
debugging. debug: true lets it through with its original message and
stack, as having no fallbackError does.
Note this only bypasses fallbackError; the rule still denied the field.
optional disabled?: boolean;Defined in: packages/graphql-casl/src/applyPermissions.ts:277
Switches enforcement off while keeping validation on.
Defaults to false.
A test that wants the schema without its rules — to seed a fixture, or to
prove a failure is the resolver's and not authorization — should not have
to hand-roll a bypass. With disabled: true, applyPermissions still
checks the map against the schema, so a stale type or field still throws
PermissionsError and drift is still caught, but returns the schema
unguarded: the one you passed, untouched, with or without inPlace. The
envelop plugin likewise validates and wraps nothing.
A test-only switch. Do not wire it to an environment variable you do not control: a misconfigured deploy would ship with every rule off and nothing to say so.
optional fallbackError?: FallbackError;Defined in: packages/graphql-casl/src/applyPermissions.ts:98
Replaces the error thrown by a denial that did not name its own.
Error('Forbidden') says nothing a client can act on and carries no code.
Supply a GraphQLError with extensions.code, a message, or a mapper that
builds one from the field being guarded.
A denial that did name its error — a check that returned a string or an
Error, or a CASL cannot(...).because(...) reason — is left alone. The
rule author was specific on purpose.
fallbackError: (_err, _parent, _args, _ctx, info) =>
new GraphQLError(`Not authorized to read ${info.parentType.name}.${info.fieldName}`, {
extensions: { code: 'FORBIDDEN' },
}),optional fallbackRule?: Rule;Defined in: packages/graphql-casl/src/applyPermissions.ts:76
Rule for every field no map entry covers — the deny-by-default switch.
Without it the map is a whitelist of what to guard, so a type or field it
does not name is left completely unguarded, and a field added to the schema
later ships unprotected. fallbackRule: deny inverts that: every field is
guarded unless the map says otherwise. Introspection is unaffected either
way.
This is the lowest-precedence entry — every map entry, wildcards included, overrides it.
optional inPlace?: boolean;Defined in: packages/graphql-casl/src/applyPermissions.ts:238
Whether to guard the schema you passed in, instead of a guarded copy.
Defaults to false.
By default applyPermissions hands the map to graphql-middleware, which
rebuilds the schema. That rebuild is the whole cost of applying: on a
1,000-type schema it is tens of milliseconds, and on a large generated CRUD
schema it is seconds. inPlace: true skips it — the rules are resolved
exactly as before, then each guarded field's resolver is replaced on the
schema itself, in a single walk. Enforcement is identical: the same fields
are guarded, the same resolver (a field's own, a subscription's
subscribe, or the default resolver) is wrapped.
This saves apply time only; per-request cost is the same either way. A
server that builds its schema once gains a few tens of milliseconds at
startup and should keep the default. It is meant for the places
applyPermissions runs repeatedly — a test suite guarding a fresh schema
per test, hot reload, per-tenant schemas, a recomposing gateway.
The schema is mutated and returned for convenience. Apply once per schema — guarding a schema that is already guarded throws, since stacking two maps is almost always a test reusing one base schema. Leave this off when you need the unguarded original too, or when something else already holds the schema and expects it to stay as built.
Ignored by resolvePermissions and the envelop plugin, which apply nothing.
optional maskDenials?: boolean;Defined in: packages/graphql-casl/src/applyPermissions.ts:207
Shorthand for onDeny: 'mask', from before onDeny existed. Setting
the two to different things is rejected.
optional onDeny?: DenialMode;Defined in: packages/graphql-casl/src/applyPermissions.ts:182
What a denied field does.
Defaults to 'reject'.
-
'reject'throws the denial. GraphQL null propagation then applies: deny one field oftodos: [Todo!]!and the entiredatapayload becomesnull, so an unauthorized corner of a query destroys the authorized rest of it. -
'filter'resolves the denied field tonull, or to[]where it is a non-null list, keeps executing, and reports the denial under the standard code UNAUTHORIZED_FIELD_OR_TYPE with the field's path — the partial-response contract Apollo Router's authorization directives set, so clients that handle those handle this. Where it lands is report.fallbackErrorstill rewords a generic denial first, and a denial that names its own code keeps it. -
'mask'resolves the field the same way and says nothing, so "you may not read this" and "this does not exist" become the same response. That is the point when the existence of a record is itself privileged, and a support burden otherwise.
Two limits bound both 'filter' and 'mask':
- A non-null field that is not a list —
id: ID!— has no value that satisfies it, so it still rejects (under the standard code with'filter') and propagates to the nearest nullable ancestor, exactly as the router's do. Filtering is bounded by the schema. - Only denials are filtered or masked. A rule that threw a bug of its own, or a resolver that failed, still surfaces its error; silently nulling those would hide outages as permission decisions.
Under 'filter', a denial the field cannot carry itself — a non-null list
resolved to [], and everything under report: 'extensions' — is held per
request until reportDenials merges it into the result. The
/envelop entry point does that for you; under applyPermissions, call it
from your server's response hook. Without that call those denials are
silently masked, which is the one way 'filter' degrades. The record keys
on the context value, so it must be an object; with any other context the
denial rejects instead.
The default stays 'reject' for compatibility. 'filter' is the better
choice for new code and becomes the default in 2.0 — or today, under
strict.
optional report?: DenialReport;Defined in: packages/graphql-casl/src/applyPermissions.ts:201
Where a filtered denial is reported.
Defaults to 'errors'. Only applies under onDeny: 'filter', and is
rejected alongside any other mode.
-
'errors'puts it in the response'serrorsarray, as Apollo Router does by default. A nullable field carries it by rejecting —nullplus an error at that path, no hook needed — and a non-null list's[]goes through reportDenials. -
'extensions'keepserrorsclean and lists them underextensions.authorizationErrorsinstead — the router's key — for clients that treat any entry inerrorsas a failed request but still want to know which parts of the query were filtered. Every denial goes through reportDenials in this mode.
optional strict?: boolean;Defined in: packages/graphql-casl/src/applyPermissions.ts:257
The defaults planned for 2.0, today: onDeny: 'filter' and
allowExternalErrors: false.
Defaults to false.
Both 1.x defaults are compatibility choices — 'reject' is what 1.0 did,
and letting resolver errors through is what this library has always done —
and both are the weaker choice for new code. strict: true picks the
stricter pair without you having to remember which two, and is what 2.0
will do without it.
It moves defaults, never mandates. A key you pass still wins:
{ strict: true, onDeny: 'reject' } rejects, and maskDenials: true —
itself a choice of mode — still masks.