GQL004: Don't use obsolete Field methods
| Value | |
|---|---|
| Rule ID | GQL004 |
| Category | Usage |
| Default severity | Warning |
| Enabled by default | Yes |
| Code fix provided | Yes |
| Introduced in | v7.7 |
Cause
- One of the deprecated
FieldXXXmethods that returnFieldTypewere used. - Expression based
Fieldbuilder withnullableandtypeparameters was used.
Rule description
A bunch of FieldXXX APIs were deprecated and will be removed in future
version. For more information see
v7 Migration Guide.
How to fix violations
You will need to change a way of setting fields on your graph types. Instead of
many FieldXXX overloads, start configuring your field with one of the Field
methods defined on ComplexGraphType. All such methods define a new field and
return an instance of FieldBuilder<T,U>. Then continue to configure the field
with rich APIs provided by the returned builder.
For the expression based field builder, you need to use an overload with only
nullable or type parameters.
Example of a violation
Field<NonNullGraphType<StringGraphType>>(
"name",
"Field description",
resolve: context => context.Source!.Name);
FieldAsync<CharacterInterface>(
"hero",
resolve: async context => await data.GetDroidByIdAsync("3"));
FieldAsync<HumanType>(
"human",
arguments: new QueryArguments(new QueryArgument<NonNullGraphType<StringGraphType>>
{
Name = "id",
Description = "id of the human"
}),
resolve: async context => await data.GetHumanByIdAsync(context.GetArgument<string>("id"))
);
Field(x => Name, true, typeof(StringGraphType));Example of how to fix
Field<NonNullGraphType<StringGraphType>>("name")
.Description("Field description")
.Resolve(context => context.Source!.Name);
Field<CharacterInterface>("hero")
.ResolveAsync(async context => await data.GetDroidByIdAsync("3"));
Field<HumanType>("human")
.Argument<NonNullGraphType<StringGraphType>>("id", "id of the human")
.ResolveAsync(async context => await data.GetHumanByIdAsync(context.GetArgument<string>("id")));
// remove the 'nullable' argument because if was ignored by the obsolete overload
Field(x => Name, typeof(StringGraphType));Configure code fix
The given diagnostic rule offers an automatic code fix. By default, it attempts
to retain the original user code formatting as much as possible but it will
remove unnecessary null values. For instance, consider the subsequent code
snippet:
Field<StringGraphType>("name", "description", null,
context => "text");This will be transformed into:
Field<StringGraphType>("name").Description("description")
.Resolve(context => "text");Configure formatting
The reformat configuration option will guide the code fix to apply code
reformatting. The default value is false.
[*.cs]
dotnet_diagnostic.GQL004.reformat = trueThe earlier code snippet will undergo the following reformatting:
Field<StringGraphType>("name")
.Description("description")
.Resolve(context => "text");Configure null values handling
The skip_nulls option can be set to false to preserve null values
assignments. The default value is true.
[*.cs]
dotnet_diagnostic.GQL004.skip_nulls = falseThe earlier code snippet will undergo the following reformatting:
Field<StringGraphType>("name").Description("description").Arguments(null)
.Resolve(context => "text");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 GQL004
// The code that's violating the rule is on this line.
#pragma warning restore GQL004To disable the rule for a file, folder, or project, set its severity to none
in the
configuration file.
[*.cs]
dotnet_diagnostic.GQL004.severity = noneFor more information, see How to suppress code analysis warnings.