Repository navigation
index.Function.applyPermissions
function applyPermissions<TResolvers>(
schema,
permissions,
options?): GraphQLSchema;Defined in: packages/graphql-casl/src/applyPermissions.ts:540
Applies a PermissionsMap to an executable schema via graphql-middleware.
TResolvers = AnyResolvers
Your generated Resolvers type.
GraphQLSchema
The executable schema to guard.
PermissionsMap<NoInfer<TResolvers>>
The permissions map to enforce.
Optional ApplyPermissionsOptions.
GraphQLSchema
The schema wrapped with the permission middleware — or, with
inPlace, the same schema, now guarded.
PermissionsError if the map does not line up with the schema.
The map is validated against the schema first: unknown types and fields,
non-function rules, and entries that would be silently inert (introspection
types, a named field on a union) all raise a PermissionsError listing
every problem at once. This catches what PermissionsMap's compile-time keys
cannot — rules loaded from a database, built in plain JavaScript, or written
against a schema that has since drifted.
An entry keyed on an interface guards that field on every type implementing
it, and a '*' entry on a union guards every field of every member; an
implementor's own entry overrides either. Two interfaces that both cover a
field of one implementor are ambiguous and rejected until the implementor
chooses — see PermissionsMap for the precedence table.
Types not named in the map are left unguarded — the map is a whitelist of what to guard, not a schema-coverage guarantee. Pass ApplyPermissionsOptions.fallbackRule to invert that.
This is also where the error-control options apply, since they have to wrap every guarded field: ApplyPermissionsOptions.fallbackError replaces the generic denial error, ApplyPermissionsOptions.allowExternalErrors governs whether resolver errors reach the client, and ApplyPermissionsOptions.debug surfaces a rule's own failures instead of reporting them as denials, and ApplyPermissionsOptions.onDeny filters or masks a denied field rather than raising an error.
The returned schema is a guarded copy built by graphql-middleware. That
rebuild is where all the time goes on a big schema;
ApplyPermissionsOptions.inPlace guards the schema you passed instead
and skips it.
const schema = applyPermissions<Resolvers>(makeExecutableSchema({ typeDefs, resolvers }), permissions);