You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/schema-directives.md
+28-22Lines changed: 28 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -270,10 +270,9 @@ GraphQL is great for internationalization, since a GraphQL server can access unl
270
270
271
271
### Enforcing access permissions
272
272
273
-
To implement the`@auth`example mentioned in the [**Declaring schema directives**](schema-directives.html#Declaring-schema-directives) section below:
273
+
Imagine a hypothetical`@auth`directive that takes an argument `requires` of type `Role`, which defaults to `ADMIN`. This `@auth` directive can appear on an `OBJECT` like `User` to set default access permissions for all `User` fields, as well as appearing on individual fields, to enforce field-specific `@auth` restrictions:
274
274
275
-
```js
276
-
consttypeDefs=`
275
+
```gql
277
276
directive@auth(
278
277
requires: Role = ADMIN,
279
278
) onOBJECT | FIELD_DEFINITION
@@ -289,52 +288,57 @@ type User @auth(requires: USER) {
289
288
name: String
290
289
banned: Boolean@auth(requires: ADMIN)
291
290
canPost: Boolean@auth(requires: REVIEWER)
292
-
}`;
291
+
}
292
+
```
293
293
294
-
// Symbols can be a good way to store semi-hidden data on schema objects.
One drawback of this approach is that it does not guarantee fields will be wrapped if they are added to the schema after `AuthDirective` is applied, and the whole `getUser(context.headers.authToken)` is a made-up API that would need to be fleshed out. In other words, we’ve glossed over some of the details that would be required for a production-ready implementation of this directive, though we hope the basic structure shown here inspires you to find clever solutions to the remaining problems.
354
+
349
355
### Enforcing value restrictions
350
356
351
357
Suppose you want to enforce a maximum length for a string-valued field:
0 commit comments