Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added documentation for optional properties. #5013

Merged
merged 2 commits into from
May 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,33 @@ We can also use record types, if we're on C# 9.0+. The equivalent to the above w
public record BookingInput(string Title, string Author);
```

## Optional Properties

If we want our input type classes to contain optional properties, we can use the `Optional<T>` type or mark the properties of the class as `nullable`. It is important to also define a default value for any non-nullable property that is using the `Optional<T>` type by adding the `[DefaultValue]` attribute, otherwise the field will still be required when defining the input.

```csharp
public class BookInput
{
[DefaultValue("")]
public Optional<string> Title { get; set; }
public string Author { get; set; }

public BookInput(string title, string author)
{
Title = title;
Author = author;
}
}

```

Also with record types, the equivalent of the above would be:

```csharp
public record BookInput([property:DefaultValue("")]Optional<string> Title, string Author);

```

## Oneof Input Objects

Oneof Input Objects are a special variant of Input Objects where the type system asserts that exactly one of the fields must be set and non-null, all others being omitted. This is represented in introspection with the \_\_Type.oneField: Boolean field, and in SDL via the @oneOf directive on the input object.
Expand Down