GQL004: Don't use obsolete Field methods

Rule ID GQL004
Category Usage
Default severity Warning
Enabled by default Yes
Code fix provided Yes
Introduced in v7.7


One of the deprecated FieldXXX methods that return FieldType were 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.

Example of a violation

    "Field description",
    resolve: context => context.Source!.Name);

    resolve: async context => await data.GetDroidByIdAsync("3"));

    arguments: new QueryArguments(new QueryArgument<NonNullGraphType<StringGraphType>>
        Name = "id",
        Description = "id of the human"
    resolve: async context => await data.GetHumanByIdAsync(context.GetArgument<string>("id"))

Example of how to fix

    .Description("Field description")
    .Resolve(context => context.Source!.Name);

    .ResolveAsync(async context => await data.GetDroidByIdAsync("3"));

    .Argument<NonNullGraphType<StringGraphType>>("id", "id of the human")
    .ResolveAsync(async context => await data.GetHumanByIdAsync(context.GetArgument<string>("id")));

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:

    .Resolve(context => "text");

Configure formatting

The reformat configuration option will guide the code fix to apply code reformatting. The default value is false.

dotnet_diagnostic.GQL004.reformat = true

The earlier code snippet will undergo the following reformatting:

    .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.

dotnet_diagnostic.GQL004.skip_nulls = false

The earlier code snippet will undergo the following reformatting:

    .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 GQL004

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

dotnet_diagnostic.GQL004.severity = none

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