In this post you’ll learn about authorizing data access in GraphQL and the unique challenges it presents.
Consider this user type of a banking app:
We want the socialSecurityNumber
to only be accessible to the user themselves and the bank’s admins.
We also want the creditScore
to only be accessible to the bank’s admins.
Why authorize at the field level?
Let’s explore scenarios where the User
type is used, and you’ll see why authorization must be done at the field level.
Showing the user’s profile
We want to support this query:
But not this, since credit scores are only accessible to admins:
Guarding access at the Query.me
level is not enough.
Showing transfer recipients
Let’s say a user in the app wants to send money to another user.
They need to get a list of transfer recipients before sending money.
Providing the user ids and names should be enough for the feature to work, but there’s a problem.
A malicious user can also query the socialSecurityNumber
and creditScore
of other users which is a big privacy issue.
This access validation is also not possible at the Query.transferRecipients
level.
The only way to solve this problem is to authorize access at the field level.
How to do field level authorization
In order to authorize access at the field level, we need to implement resolvers at the User
type.
This way we don’t have to worry about how the User
type is used.
Access to the fields are guaranteed to be valid.
Conclusion
GraphQL’s flexibility poses unique challenges to authorization not found in REST.
Complex applications with different roles and permissions often wants to restrict access to certain fields.
To ensure maximum security, authorization must be done at the field level.