Skip to content

Commit 0a8a63c

Browse files
committed
Update authorization docs.
1 parent c822ab4 commit 0a8a63c

File tree

1 file changed

+72
-32
lines changed

1 file changed

+72
-32
lines changed

src/pages/learn/authorization.mdx

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,92 @@
11
# Authorization
22

3-
> Delegate authorization logic to the business logic layer
3+
<p className="learn-subtitle">Delegate authorization logic to the business logic layer</p>
4+
5+
Most APIs will need to secure access to certain types of data depending on who requested it, and GraphQL is no different. GraphQL execution should begin after [authentication](/graphql-js/authentication-and-express-middleware/) middleware confirms the user's identity and passes that information to the GraphQL layer. But after that, you still need to determine if the authenticated user is allowed to view the data provided by the specific fields that were included in the request. On this page, we'll explore how a GraphQL schema can support authorization.
6+
7+
## Type and field authorization
48

59
Authorization is a type of business logic that describes whether a given user/session/context has permission to perform an action or see a piece of data. For example:
610

711
_"Only authors can see their drafts"_
812

9-
Enforcing this kind of behavior should happen in the [business logic layer](/learn/thinking-in-graphs/#business-logic-layer). It is tempting to place authorization logic in the GraphQL layer like so:
13+
Enforcing this behavior should happen in the [business logic layer](/learn/thinking-in-graphs/#business-logic-layer). Let's consider the following `Post` type defined in a schema:
14+
15+
```graphql
16+
type Post {
17+
authorId: ID!
18+
body: String
19+
}
20+
```
21+
22+
In this example, we can imagine that when a request initially reaches the server, authentication middleware will first check the user's credentials and add information about their identity to the `context` object of the GraphQL request so that this data is available in every field resolver for the duration of its execution.
23+
24+
If a post's body should only be visible to the user who authored it, then we will need to check that the authenticated user's ID matches the post's `authorId` value. It may be tempting to place authorization logic in the resolver for the post's `body` field like so:
1025

1126
```js
12-
const postType = new GraphQLObjectType({
13-
name: 'Post',
14-
fields: {
15-
body: {
16-
type: GraphQLString,
17-
resolve(post, args, context, { rootValue }) {
18-
// return the post body only if the user is the post's author
19-
if (context.user && (context.user.id === post.authorId)) {
20-
return post.body
21-
}
22-
return null
23-
}
24-
}
27+
function Post_body(obj, args, context, info) {
28+
// return the post body only if the user is the post's author
29+
if (context.user && (context.user.id === obj.authorId)) {
30+
return obj.body
2531
}
26-
})
32+
return null
33+
}
2734
```
2835

29-
Notice that we define "author owns a post" by checking whether the post's `authorId` field equals the current user’s `id`. Can you spot the problem? We would need to duplicate this code for each entry point into the service. Then if the authorization logic is not kept perfectly in sync, users could see different data depending on which API they use. Yikes! We can avoid that by having a [single source of truth](/learn/thinking-in-graphs/#business-logic-layer) for authorization.
36+
Notice that we define "author owns a post" by checking whether the post's `authorId` field equals the current user’s `id`. Can you spot the problem? We would need to duplicate this code for each entry point into the service. Then if the authorization logic is not kept perfectly in sync, users could see different data depending on which API they use. Yikes! We can avoid that by having a [single source of truth](/learn/thinking-in-graphs/#business-logic-layer) for authorization, instead of putting it the GraphQL layer.
3037

31-
Defining authorization logic inside the resolver is fine when learning GraphQL or prototyping. However, for a production codebase, delegate authorization logic to the business logic layer. Here’s an example:
38+
Defining authorization logic inside the resolver is fine when learning GraphQL or prototyping. However, for a production codebase, delegate authorization logic to the business logic layer. Here’s an example of how authorization of the `Post` type's fields could be implemented separately:
3239

3340
```js
34-
// Authorization logic lives inside postRepository
35-
const postRepository = require('postRepository');
36-
37-
const postType = new GraphQLObjectType({
38-
name: 'Post',
39-
fields: {
40-
body: {
41-
type: GraphQLString,
42-
resolve(post, args, context, { rootValue }) {
43-
return postRepository.getBody(context.user, post)
44-
}
41+
// authorization logic lives inside `postRepository`
42+
export const postRepository = {
43+
getBody({ user, post }) {
44+
if (user?.id && (user.id === post.authorId)) {
45+
return post.body
4546
}
47+
return null
4648
}
47-
})
49+
}
50+
```
51+
52+
The resolver function for the post's `body` field would then call a `postRepository` method instead of implementing the authorization logic directly:
53+
54+
```js
55+
import { postRepository } from 'postRepository'
56+
57+
function Post_body(obj, args, context, info) {
58+
// return the post body only if the user is the post's author
59+
return postRepository.getBody({ user: context.user, post: obj })
60+
}
4861
```
4962
50-
In the example above, we see that the business logic layer requires the caller to provide a user object. If you are using GraphQL.js, the User object should be populated on the `context` argument or `rootValue` in the fourth argument of the resolver.
63+
In the example above, we see that the business logic layer requires the caller to provide a user object, which is available in the `context` object for the GraphQL request. We recommend passing a fully-hydrated user object instead of an opaque token or API key to your business logic layer. This way, we can handle the distinct concerns of [authentication](/graphql-js/authentication-and-express-middleware/) and authorization in different stages of the request processing pipeline.
64+
65+
## Using type system directives
66+
67+
In the example above, we saw how authorization logic can be delegated to the business logic layer through a function that is called in a field resolver.
68+
69+
Another approach when implementing authorization checks for a GraphQL API is to use [type system directives](/learn/schema/#directives), where a directive such as `@auth` is defined in the schema with arguments that can indicate what roles or permissions a user must have to access the data provided by the and fields where the directive is applied. For example:
70+
71+
```graphql
72+
directive @auth(rule: Rule) on FIELD_DEFINITION
73+
74+
enum Rule {
75+
IS_AUTHOR
76+
}
77+
78+
type Post {
79+
authorId: ID!
80+
body: String @auth(rule: IS_AUTHOR)
81+
}
82+
```
83+
84+
It would be up to the GraphQL implementation to determine how an `@auth` directive affects execution when a client makes a request that includes the `body` field for `Post` type. However, the authorization logic should remain delegated to the business logic layer.
85+
86+
## Recap
87+
88+
To recap these recommendations for authorization in GraphQL:
5189
52-
We recommend passing a fully-hydrated User object instead of an opaque token or API key to your business logic layer. This way, we can handle the distinct concerns of [authentication](/graphql-js/authentication-and-express-middleware/) and authorization in different stages of the request processing pipeline.
90+
- Authorization logic should be delegated to the business logic layer, not the GraphQL layer
91+
- After execution begins, a GraphQL server should make decisions about whether the client that made the request is authorized to access data for the included fields
92+
- Type system directives may be defined and added to the types and fields in a schema to apply generalized authorization rules

0 commit comments

Comments
 (0)