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/types/input-objects.md
+66-53Lines changed: 66 additions & 53 deletions
Original file line number
Diff line number
Diff line change
@@ -122,48 +122,6 @@ input Input_Donut {
122
122
}
123
123
```
124
124
125
-
126
-
## Required Fields And Default Values
127
-
Add `[Required]` (from System.ComponentModel) to any property to force a user to supply the field in a query document.
128
-
129
-
Any non-required field will automatically be assigned a default value if not supplied. This default value is equivilant to the property value of the object when its instantiated via its public, parameterless constructor.
We assigned a recipe in the class's constructor to use as the default value.
211
171
212
-
Any non-nullable field, that does not have the `[Required]` attribute, MUST have a default value assigned to it that is not `null`.
213
-
214
-
A `GraphTypeDeclarationException` will be thrown at startup if this is not the case.
172
+
Any non-nullable field, that does not have the `[Required]` attribute (see below), MUST have a default value assigned to it that is not `null`. A `GraphTypeDeclarationException` will be thrown at startup if this is not the case.
215
173
:::
216
174
217
-
#### Combine Non-Null and [Required]
218
-
Combine the [Required] attribute with a custom type expression to force a user to supply a non-null value for the field on a query document.
Add `[Required]` (from System.ComponentModel) to any property to force a user to supply the field in a query document.
178
+
179
+
Any non-required field will automatically be assigned a default value if not supplied on a query. This default value is equivilant to the property value of the object when its instantiated via its public, parameterless constructor.
By Definition (spec § [5.6.4](https://spec.graphql.org/October2021/#sec-Input-Object-Required-Fields)), a nullable field without a
215
+
declared default value is still considered optional and does not need to be supplied. That is to say that if a query does not include a field that is nullable, it will default to `null` regardless of the use of the `[Required]` attribute.
216
+
217
+
These two property declarations for an input object are identical as far as graphql is concerned:
218
+
219
+
```csharp title='Example Input object Fields'
220
+
public InputEmployee
221
+
{
222
+
public string FirstName { get; set; }
226
223
224
+
[Required]
225
+
public string LastName { get; set; }
226
+
}
227
+
```
228
+
229
+
Both `FirstName` and `LastName` are of type `string`, which is nullable. GraphQL will ignore the required attribute and still allow a query to process even if neither value is supplied on a query.
230
+
231
+
### Required Reference Types
232
+
Combine the [Required] attribute with a non-nullable type expression to force an otherwise nullable field to be required.
# No Default Value is supplied. owner must be supplied on a query
244
+
# No Default Value is supplied.
245
+
# the 'owner' must be supplied on a query
235
246
input Input_Bakery {
236
247
owner: Input_Person!
237
248
}
238
249
```
239
250
251
+
`Owner` is of type Person, which is a reference type, which is nullable by default. By augmenting its type expression to be non-null and adding the `[Required]` attribute graphql will not supply a default value require it to be supplied on a query.
252
+
240
253
## Enum Fields and Coercability
241
254
242
255
Any default value declared for an input field must be coercible by its target graph type in the target schema. Because of this there is a small got'cha situation with enum values.
0 commit comments