Skip to content

Commit b3ab94b

Browse files
authored
docs(router): Add authorization documentation and specification (#7302)
1 parent 58c015d commit b3ab94b

File tree

4 files changed

+560
-0
lines changed

4 files changed

+560
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
---
2+
title: 'authorization'
3+
---
4+
5+
# authorization
6+
7+
The `authorization` configuration lets you control fine-grained access to your GraphQL schema using
8+
directives like `@authenticated` and `@requiresScopes`. This allows you to restrict which fields
9+
authenticated users can access based on their authentication status or specific scopes.
10+
11+
For practical examples and a guide to authorization concepts, see
12+
**["Authorization"](../security/authorization)** in the documentation.
13+
14+
## Directives
15+
16+
Access control is defined within the supergraph schema using the following directives:
17+
18+
- `@authenticated` - restricts access to authenticated users only
19+
- `@requiresScopes(scopes: [[String]])` - restricts access based on specific scopes
20+
21+
## Configuration Options
22+
23+
### `enabled`
24+
25+
- **Type:** `boolean`
26+
- **Default:** `true`
27+
28+
Whether to enable authorization directives processing. Set to `false` to disable authorization
29+
checks entirely.
30+
31+
### `unauthorized.mode`
32+
33+
- **Type:** `string`
34+
- **Allowed values:** `"filter"` or `"reject"`
35+
- **Default:** `"filter"`
36+
37+
Controls how the router handles unauthorized field access:
38+
39+
- `"filter"`: Remove unauthorized fields and continue processing (returns errors for removed fields)
40+
- `"reject"`: Reject the entire request if any unauthorized fields are accessed
41+
42+
## Examples
43+
44+
### Filter Mode (Default)
45+
46+
With `filter` mode, unauthorized fields are silently removed from the operation, but the query
47+
continues to execute and return the data the user can access:
48+
49+
```yaml filename="router.config.yaml"
50+
authorization:
51+
directives:
52+
enabled: true
53+
unauthorized:
54+
mode: filter
55+
```
56+
57+
**Request:**
58+
59+
```graphql
60+
query {
61+
publicData
62+
me {
63+
name
64+
email
65+
}
66+
}
67+
```
68+
69+
If the user is not authenticated, the response might look like:
70+
71+
```json
72+
{
73+
"data": {
74+
"publicData": "available",
75+
"me": null
76+
},
77+
"errors": [
78+
{
79+
"message": "Unauthorized field or type",
80+
"extensions": {
81+
"code": "UNAUTHORIZED_FIELD_OR_TYPE",
82+
"affectedPath": "me"
83+
}
84+
}
85+
]
86+
}
87+
```
88+
89+
### Reject Mode
90+
91+
With `reject` mode, if any field is unauthorized, the entire request is rejected:
92+
93+
```yaml filename="router.config.yaml"
94+
authorization:
95+
directives:
96+
enabled: true
97+
unauthorized:
98+
mode: reject
99+
```
100+
101+
**Request (same as above):**
102+
103+
```graphql
104+
query {
105+
publicData
106+
me {
107+
name
108+
email
109+
}
110+
}
111+
```
112+
113+
**Response:**
114+
115+
```json
116+
{
117+
"data": null,
118+
"errors": [
119+
{
120+
"message": "Unauthorized field or type",
121+
"extensions": {
122+
"code": "UNAUTHORIZED_FIELD_OR_TYPE"
123+
}
124+
}
125+
]
126+
}
127+
```

packages/web/docs/src/content/router/configuration/index.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Some configuration variables can be overridden at runtime using
1313
This page covers all the main configuration options available. Each option links to a detailed page
1414
that explains how to use that feature.
1515

16+
- [`authorization`](./configuration/authorization): Define field-level access control using
17+
directives.
1618
- [`cors`](./configuration/cors): Control cross-origin requests to your API.
1719
- [`csrf`](./configuration/csrf): Protect against cross-site request forgery attacks.
1820
- [`headers`](./configuration/headers): Modify HTTP headers between clients and subgraphs.

packages/web/docs/src/content/router/security/_meta.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export default {
2+
authorization: 'Authorization',
23
cors: 'Configuring CORS',
34
csrf: 'CSRF Prevention',
45
'jwt-authentication': 'JWT Authentication',

0 commit comments

Comments
 (0)