Lesson 5 of 9 · 25m
Authorization at the Field Level
Why resolver-level checks are inevitable in GraphQL, how to centralize policy without scattering permissions through resolvers, and how object-level and field-level rules differ.
GraphQL authorization fails quietly. The query succeeds, the shape is valid, and one nested field exposes data because a resolver assumed the parent had already been checked. In a graph, clients compose paths you did not anticipate: workspace.projects.owner.privateEmail. If authorization lives only at the entry point, the graph becomes a confused-deputy machine.
Authenticate once, authorize everywhere
Authentication answers who the actor is. Authorization answers whether that actor may perform this action on this object. They are different layers:
type Context = {
actor: Actor | null;
policy: PolicyService;
loaders: Loaders;
};The actor belongs in context after verifying a session or token. Policy decisions should consume the actor, object, action, and sometimes the field:
policy.authorize(actor, "read", invoice);
policy.authorize(actor, "readSensitive", invoice);Object-level checks are not enough
Suppose any workspace member can read a member profile, but only admins can read recovery email addresses:
type Member {
displayName: String!
recoveryEmail: String
}The resolver must express that distinction:
Member: {
recoveryEmail: async (member, args, ctx) => {
if (!ctx.policy.can(ctx.actor, "member:readRecoveryEmail", member)) {
return null;
}
return member.recoveryEmail;
},
}Whether denial should be null or an error is a product decision. null avoids leaking that the field exists; an error makes misconfiguration visible. Pick semantics per sensitivity and document them.
Centralize policy, do not centralize object fetching into authorization
A good policy layer says yes or no using loaded objects. A bad one secretly performs hidden database calls in every check, recreating the N+1 problem. Pass loaded entities into policy or give policy access to request-scoped loaders. Do not let can() call SQL directly on every invocation.
const canViewInvoice = (actor, invoice) =>
actor.workspaceIds.includes(invoice.workspaceId) &&
(actor.role === "admin" || invoice.visibility === "MEMBERS");Mutations need authorization plus invariants
For mutations, check permission before performing side effects, then validate domain invariants:
async function refundInvoice(_, { input }, ctx) {
const invoice = await ctx.loaders.invoice.load(input.invoiceId);
if (!invoice) throw new NotFoundError("Invoice not found");
ctx.policy.require(ctx.actor, "invoice:refund", invoice);
if (invoice.status !== "PAID") {
return { invoice: null, errors: [{ code: "NOT_PAID", message: "Only paid invoices can be refunded" }] };
}
// perform refund transactionally
}Ordering matters: existence checks can leak data, so many APIs return the same not-found response when the object exists but is inaccessible.
Schema directives are metadata, not magic
@auth(requires: ADMIN) can be a useful declaration, but only if the execution engine enforces it and tests cover the matrix. A directive that merely decorates SDL gives teams false confidence. Prefer explicit policy calls for nuanced rules; use directives for coarse, mechanical guards.
Field notes
- The same object may be reachable through multiple paths. Authorization must travel with the object resolver, not the entry resolver.
- Deny-by-default is easier to reason about than remember-to-protect.
- Do not expose different field presence based on permission unless you intentionally accept the information leak.
- Include authorization tests for nested paths, not just direct queries.
- If a field is derived from sensitive data, it inherits the sensitivity even when it does not look sensitive.
Was this lesson helpful?