Authorization in GraphQL APIs: field-level permissions that scale
REST makes authorization feel manageable because each endpoint is a chokepoint: one URL, one handler, one place to check whether the caller is allowed in. GraphQL dissolves that chokepoint. A single query can fan out across dozens of resolvers, walk relationships the client composed on the fly, and return a shape you never explicitly designed. The question "is this caller allowed to see this?" stops being a decision you make once at the door and becomes a decision you must make at every field the query touches—including fields reached by nesting through objects the caller legitimately owns into ones they don't.
Why endpoint-level checks fail here
Teams coming from REST often guard the single /graphql endpoint—authenticate the request, then let the resolvers run. That authenticates but doesn't authorize. Consider:
query {
me {
organization {
members { # fine, I can see my org's members
email # ...but can I see every member's email?
salary # ...and definitely not this
auditLog { # nested two levels into data I don't own
ipAddress
}
}
}
}
}Every object here is reachable from me, so a start-at-the-root check waves the whole thing through. The sensitive data isn't exposed by a missing endpoint guard—it's exposed by the traversal. Authorization in GraphQL has to live where the data is resolved, not where the request arrives.
Push authorization into the resolver, at the field
The unit of authorization is the field. Each resolver that returns something sensitive checks whether the current caller, given the specific parent object being resolved, may read that field. The parent matters: "can read email" is meaningless; "can read this member's email given who I am" is the real question.
const resolvers = {
Member: {
email: (member, _args, ctx) => {
// self, or an admin of the same org
if (ctx.user.id === member.id) return member.email;
if (ctx.can("member:read_pii", { orgId: member.orgId })) return member.email;
return null; // or throw a ForbiddenError
},
salary: (member, _args, ctx) => {
if (!ctx.can("member:read_comp", { orgId: member.orgId })) {
throw new ForbiddenError("not authorized");
}
return member.salary;
},
},
};Returning null versus throwing is a real design choice. Throwing fails the whole field path loudly, which is right for "you shouldn't have asked." Returning null (for a nullable field) lets the rest of the query succeed while hiding the value—better UX when partial results are legitimate. Pick deliberately and be consistent, because the difference is observable to a client probing your schema.
Make the schema carry the rule. Scattering ctx.can(...) by hand invites the one resolver someone forgets. A schema directive—salary: Float @requires(permission: "member:read_comp")—lets you declare the requirement next to the field and enforce it with a single piece of middleware that wraps every resolver. The policy becomes reviewable in the schema diff instead of buried across the codebase.
The N+1 permission-check problem
Field-level checks have an obvious failure mode. Resolve 500 members, each with three guarded fields, and a naive implementation runs 1,500 permission evaluations—many of them asking the same thing ("is this caller an admin of org 42?") over and over. If each check hits your authorization store, you've turned one query into a storm of lookups and recreated the N+1 problem in the security layer.
Batch and memoize per request
The fix is the same pattern GraphQL already uses for data loading: coalesce and cache within the request. Load the caller's relevant grants once at the start of the request into the context, then make each field check a pure, in-memory function against that snapshot. For checks that genuinely need per-object lookups (row-level ownership), batch them with a DataLoader so a hundred "can I read member X" questions become one query.
function buildContext(req): Context {
const user = authenticate(req);
// one load per request, not per field
const grants = loadGrantsForUser(user.id); // roles, org memberships
const can = (perm: string, scope: { orgId: string }) =>
grants.some(g => g.permission === perm && g.orgId === scope.orgId);
// batched row-level ownership, deduped across the whole query
const ownershipLoader = new DataLoader(
(memberIds: string[]) => checkOwnershipBatch(user.id, memberIds)
);
return { user, can, ownershipLoader };
}Now the 1,500 checks become one grant load plus, at most, one batched ownership query—and most checks don't touch the network at all.
Query depth and cost are an authorization concern too. An attacker doesn't need to breach a field to hurt you; a deeply nested or cyclic query (members { organization { members { organization { ... }}}}) can fan out into millions of resolver calls and take your service down. Enforce a maximum query depth and a query-cost limit before execution. Authorization that only runs per field never gets the chance if the query is rejected at the gate.
Introspection and error leakage
Two side channels undermine careful field checks. First, schema introspection: in production, consider restricting it, because a published schema hands an attacker the exact map of fields to probe. Second, error messages—a ForbiddenError that says "user 88 lacks member:read_comp on org 42" confirms that org 42 and member 88 exist and names your permission taxonomy. Keep authorization errors generic to the client and detailed only in your server-side audit log.
Where the policy should live
Resolvers are the right place to enforce authorization, but they're the wrong place to define it. If each resolver hand-rolls its own notion of "admin," your rules drift and no one can answer "who can read compensation data?" without grepping the codebase. Keep the policy—roles, permissions, scopes—in one authoritative place, and let resolvers ask it questions. The resolver decides where to check; the policy engine decides what the answer is.
Field-level authorization done well is invisible: callers see exactly the data they're entitled to, nested however they like, with no extra round trips and no leaks through traversal. Getting there is far easier when your identity system exposes permissions as something you can query cheaply per request rather than a black box you call over the network per field. Owning that layer lets you shape the grant model to match your schema; Bastionary exposes roles and fine-grained permissions through a local check API built for exactly this batched, per-request pattern.
Run it yourself
Bastionary is free, open-source software. Follow the quick start to have it running on your own server, or browse the releases on Git.