Repository navigation
apollo
The Apollo Server integration — the optional entry point,
@vantreeseba/graphql-casl/apollo.
On Apollo Server the schema is wrapped up front with applyPermissions, so
enforcement needs nothing from here. What applyPermissions cannot do is see
the finished response — and under onDeny: 'filter' some denials can only be
reported there: a non-null list resolves to [], which cannot also be an
error, and under report: 'extensions' every denial waits for the response.
Those are held per request until reportDenials merges them in.
reportDenialsPlugin is that call, made from Apollo's own
willSendResponse hook, so there is nothing left to wire by hand.
The plugin is typed structurally against the slice of Apollo Server's plugin
contract it uses, so @apollo/server is not a dependency of this package,
optional or otherwise. Apollo Server 4 and 5 share that contract.
import { ApolloServer } from '@apollo/server';
import { applyPermissions } from '@vantreeseba/graphql-casl';
import { reportDenialsPlugin } from '@vantreeseba/graphql-casl/apollo';
const server = new ApolloServer<Context>({
schema: applyPermissions<Resolvers>(schema, permissions, { onDeny: 'filter' }),
plugins: [reportDenialsPlugin()],
});