@@ -145,7 +145,10 @@ You can apply directives to:
145
145
146
146
- Fields
147
147
- Fragment spreads
148
+ - Named fragments
148
149
- Inline fragments
150
+ - Operations (query, mutation or subscription)
151
+ - Variable definitions
149
152
150
153
The following examples show how to apply directives:
151
154
@@ -253,6 +256,49 @@ Some common use cases for custom directives include:
253
256
- **Observability**: ` @log` , ` @tag (name: " important" )` , or ` @metrics (id: " xyz" )`
254
257
- **Execution control**: Mask or transform fields at runtime with schema visitors
255
258
259
+ ## When to avoid custom directives
260
+
261
+ Custom directives should not be used where an argument could achieve the same
262
+ goal. Custom directives in GraphQL requests complicate caching (particularly
263
+ normalized caches) and are less well understood by tooling such as GraphQL; by
264
+ using arguments instead we maximize compatibility and consistency. For example,
265
+ our ` @uppercase` directive would fit naturally as a field argument:
266
+
267
+ ` ` ` js
268
+ import {
269
+ graphql ,
270
+ buildSchema ,
271
+ } from ' graphql' ;
272
+
273
+ const schema = buildSchema (`
274
+ enum Format {
275
+ VERBATIM
276
+ UPPERCASE
277
+ }
278
+ type Query {
279
+ greeting(format: Format! = VERBATIM): String
280
+ }
281
+ ` );
282
+
283
+ const rootValue = {
284
+ greeting : (source , args ) => {
285
+ const result = ' Hello, world' ;
286
+
287
+ if (args .format === " UPPERCASE" ) {
288
+ return result .toUpperCase ();
289
+ }
290
+
291
+ return result;
292
+ },
293
+ };
294
+
295
+ const query = `
296
+ query {
297
+ greeting(format: UPPERCASE)
298
+ }
299
+ ` ;
300
+
301
+
256
302
## Best practices
257
303
258
304
When working with custom directives in GraphQL .js , keep the following best practices in mind:
@@ -261,6 +307,7 @@ When working with custom directives in GraphQL.js, keep the following best pract
261
307
manually.
262
308
- Weigh schema-driven logic against resolver logic. Directives can make queries more expressive, but they
263
309
may also hide behavior from developers reading the schema or resolvers.
310
+ - Always prefer field arguments over directives where possible.
264
311
- Keep directive behavior transparent and debuggable. Since directives are invisible at runtime unless
265
312
logged or documented, try to avoid magic behavior.
266
313
- Use directives when they offer real value. Avoid overusing directives to replace things that could be
@@ -272,4 +319,4 @@ writing custom validation rules to enforce correct usage.
272
319
273
320
- [GraphQL Specification: Directives](https://spec.graphql.org/draft/#sec-Language.Directives)
274
321
- The Guild' s guide on [Schema Directives](https: // the-guild.dev/graphql/tools/docs/schema-directives)
275
- - Apollo Server's guide on [Directives](https://www.apollographql.com/docs/apollo-server/schema/directives)
322
+ - Apollo Server' s guide on [Directives](https://www.apollographql.com/docs/apollo-server/schema/directives)
0 commit comments