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/advanced/directives.md
+98-19Lines changed: 98 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -77,7 +77,7 @@ Directives can't directly return data or resolve a field. They can only indicate
77
77
*`this.Cancel()`:
78
78
* The directive failed and the schema should not be generated or the target field should be dropped.
79
79
80
-
> throwing an exception within an action method of a directive will cause the current query to fail completely. Use `this.Cancel()` to only drop the single targeted field.
80
+
> Throwing an exception within an action method of a directive will cause the current query to fail completely. Use `this.Cancel()` to discard only the currently resolving field. Normal nullability validation rules still apply.
81
81
82
82
### Directive Target
83
83
The `this.DirectiveTarget` property will contain either an `ISchemaItem` for type system directives or the resolved field value for execution directives. This value is useful in performing additional operations such as extending a field resolver during schema generation or taking further action against a resolved field.
@@ -176,7 +176,7 @@ It is recommended that your directives act independently and be self contained.
176
176
177
177
178
178
## Type System Directives
179
-
### Example: @ToUpper
179
+
### Example: @toUpper
180
180
181
181
This directive will extend the resolver of a field to turn any strings into upper case letters.
182
182
@@ -215,13 +215,44 @@ This directive will extend the resolver of a field to turn any strings into uppe
215
215
216
216
This Directive:
217
217
218
-
* Targets a FIELD_DEFINITION and can be applied to any field of any type.
218
+
* Targets any FIELD_DEFINITION.
219
219
* Ensures that the target field returns returns a string.
220
220
* Extends the field's resolver to convert the result to an upper case string.
221
221
* The directive is executed once per field its applied to when the schema is created. The extension method is executed on every field resolution.
222
222
* If an exception is thrown the schema will fail to create and the server will not start.
223
223
* if the action method returns a cancel result (e.g. `this.Cancel()`) the schema will fail to create and the server will not start.
* Marks the field or enum value as deprecated and attaches the provided deprecation reason
254
+
* The directive is executed once per field and enum value its applied to when the schema is created.
255
+
225
256
### Applying Type System Directives
226
257
227
258
#### Using the `[ApplyDirective]` attribute
@@ -284,7 +315,7 @@ type Person @monitor {
284
315
285
316
<br/>
286
317
<br/>
287
-
**Adding Arguments**
318
+
**Adding Arguments with [ApplyDirective]**
288
319
289
320
Arguments added to the apply directive attribute will be passed to the directive in the order they are encountered. The supplied values must be coercable into the expected data types for an input parameters.
290
321
@@ -293,9 +324,11 @@ Arguments added to the apply directive attribute will be passed to the directive
293
324
294
325
```csharp
295
326
// Person.cs
296
-
[ApplyDirective("monitor", "trace")]
297
327
publicclassPerson
298
328
{
329
+
[ApplyDirective(
330
+
"deprecated",
331
+
"Names don't matter")]
299
332
publicstringName{ get; set; }
300
333
}
301
334
```
@@ -305,21 +338,23 @@ public class Person
305
338
306
339
```javascript
307
340
// GraphQL Type Definition Equivilant
308
-
type Person @monitor(level:"trace") {
309
-
name:String
341
+
type Person {
342
+
name:String @deprecated("Names don't matter")
310
343
}
311
344
```
312
345
313
346
</div>
314
347
</div>
315
348
316
-
<br/>
317
349
<br/>
318
350
319
351
#### Using Schema Options
320
352
321
353
Alternatively, instead of using attributes to apply directives you can apply directives during schema configuration:
DirectivesareinvokedasservicesthroughyourDIcontainerwhentheyareexecuted. Whenyouaddtypestoyourschemaduringitsinitialconfiguration, GraphQLASP.NETwillautomaticallyregisteranydirectivesitfindsasservicesinyour `IServiceCollection` instance. However, therearetimeswhenitcannotdothis, suchaswhenyouapplyadirectivebyitsname. Theselatebounddirectivesmaystillbediscoverableandgraphqlwillattempttoaddthemtoyourschemawheneveritcan. However, itmaydothisaftertheopportunitytoregisterthemwiththeDIcontainerhaspassed.
437
+
DirectivesareinvokedasservicesthroughyourDIcontainerwhentheyareexecuted. Whenyouaddtypestoyourschemaduringitsinitialconfiguration, GraphQLASP.NETwillautomaticallyregisteranydirectivesitfindsattachedtoyourobjectsandpropertiesasservicesinyour `IServiceCollection` instance. However, therearetimeswhenitcannotdothis, suchaswhenyouapplyadirectivebyitsstringdeclaredname. Theselate-bounddirectivesmaystillbediscoverablelaterandgraphqlwillattempttoaddthemtoyourschemawheneveritcan. However, itmaydothisaftertheopportunitytoregisterthemwiththeDIcontainerhaspassed.
368
438
369
-
Whenthisoccurs, ifyourdirectivecontainsapublic, parameterlessconstructoritwillstillinstantiateanduseyourdirectiveasnormal. Ifthedirectivecontainsdependenciesintheconstructorthatitcan't resolve, execution of that directive will fail and an exception will be thrown. To be safe, make sure to add any directives you may use to your schema during the `.AddGraphQL()` configuration. Directives are discoverable and will be included via the `options.AddAssembly()` helper method.
439
+
Whenthisoccurs, ifyourdirectivecontainsapublic, parameterlessconstructorgraphqlwillstillinstantiateanduseyourdirectiveasnormal. Ifthedirectivecontainsdependenciesintheconstructorthatitcan't resolve, execution of that directive will fail and an exception will be thrown. To be safe, make sure to add any directives you may use to your schema during the `.AddGraphQL()` configuration method. Directives are directly discoverable and will be included via the `options.AddAssembly()` helper method as well.
0 commit comments