GQL013: OneOf fields must be nullable
Value | |
---|---|
Rule ID | GQL013 |
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 is not nullable.
Rule description
All the fields defined by the OneOf
input graph type must be nullable. 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
Make all the fields in the OneOf
graph type nullable.
Example of a violation
Code-first:
public class UserIdentifierInputGraphType : InputObjectGraphType<UserIdentifier>
{
public UserIdentifierInputGraphType()
{
IsOneOf = true;
Field<NonNullGraphType<IdGraphType>>("id");
Field(x => x.Name); // 'nullable' is false by default
Field(x => x.NickName, nullable: false);
Field(x => x.FullName, type: typeof(NonNullGraphType<StringGraphType>));
// 'nullable' is ignored when 'type' is defined
Field(x => x.Email, nullable: true, type: typeof(NonNullGraphType<StringGraphType>));
}
}
Type-first:
[OneOf]
public class UserIdentifier
{
// non-nullable value type
public int Id { get; set; }
// Null Reference Types are fully supported
#nullable enable
public string Name { get; set; }
#nullable restore
}
Example of how to fix
Make all fields nullable
Code-first:
public class UserIdentifierInputGraphType : InputObjectGraphType<UserIdentifier>
{
public UserIdentifierInputGraphType()
{
IsOneOf = true;
Field<IdGraphType>("id");
Field(x => x.Name, nullable: true); // 'nullable' is false by default
Field(x => x.NickName, nullable: true);
Field(x => x.FullName, type: typeof(StringGraphType));
// 'nullable' is ignored when 'type' is defined
Field(x => x.Email, nullable: true, type: typeof(StringGraphType));
}
}
Type-first:
[OneOf]
public class UserIdentifier
{
public int? Id { get; set; }
// Nullable Reference Types (NRT) are fully supported
#nullable enable
public string? Name { get; set; }
#nullable restore
}
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 GQL013
// The code that's violating the rule is on this line.
#pragma warning restore GQL013
To disable the rule for a file, folder, or project, set its severity to none
in the
configuration file.
[*.cs]
dotnet_diagnostic.GQL013.severity = none
For more information, see How to suppress code analysis warnings.