GQL014: OneOf fields must not have default value

Value
Rule ID GQL014
Category Usage
Default severity Error
Enabled by default Yes
Code fix provided No
Introduced in v8.0

Cause

This rule triggers when any of the fields defined by the OneOf input graph type has default value.

Rule description

All the fields defined by the OneOf input graph type must not have default value. The rule runs on the code-first input types when IInputObjectGraphType.IsOneOf = true; and type-first models decorated with [OneOf] attribute. If the field is decorated with the [Ignore] attribute the rule violation is not reported.

How to fix violations

Remove default values from all the fields in the OneOf graph type.

Example of a violation

Code-first:

public class UserIdentifierInputGraphType : InputObjectGraphType<UserIdentifier>
{
    public UserIdentifierInputGraphType()
    {
        IsOneOf = true;
        Field(x => x.NickName, nullable: true).DefaultValue("Joe");
    }
}

Type-first:

[OneOf]
public class UserIdentifier
{
    [DefaultValue("Joe")]
    public string? NickName { get; set; }
}

Example of how to fix

Remove the default value.

Code-first:

public class UserIdentifierInputGraphType : InputObjectGraphType<UserIdentifier>
{
    public UserIdentifierInputGraphType()
    {
        IsOneOf = true;
        Field(x => x.NickName, nullable: true);
    }
}

Type-first:

[OneOf]
public class UserIdentifier
{
    public string? Name { get; set; }
}

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable GQL014
// The code that's violating the rule is on this line.
#pragma warning restore GQL014

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.cs]
dotnet_diagnostic.GQL014.severity = none

For more information, see How to suppress code analysis warnings.

GQL013: OneOf fields must be nullable