Back to Graphql Dotnet

GQL016: Require parameterless constructor

docs2/site/docs/analyzers/gql016.md

8.8.42.2 KB
Original Source

GQL016: Require parameterless constructor

Value
Rule IDGQL016
CategoryUsage
Default severityError
Enabled by defaultYes
Code fix providedNo
Introduced inv8.0

Cause

This rule is triggered when a type implementing an interface is required to have a public parameterless constructor but does not have one.

Rule description

Certain APIs provided by the GraphQL.NET library require that types implementing an interface must have a parameterless constructor. This rule ensures compliance by verifying the presence of such a constructor and reporting an error if the type does not meet this requirement.

How to fix violations

Define parameterless constructor on the type or remove all the constructors.

Example of a violation

c#
public class MyComplexityAnalyzer : IFieldComplexityAnalyzer
{
    public MyComplexityAnalyzer(ILogger<MyComplexityAnalyzer> logger)
    {
    }

    public FieldComplexityResult Analyze(FieldImpactContext context)
    {
        return new FieldComplexityResult(10, 1);
    }
}

Example of how to fix

Define parameterless constructor on the type or remove all the constructors.

c#
public class MyComplexityAnalyzer : IFieldComplexityAnalyzer
{
    public FieldComplexityResult Analyze(FieldImpactContext context)
    {
        return new FieldComplexityResult(10, 1);
    }
}

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.

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

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

ini
[*.cs]
dotnet_diagnostic.GQL016.severity = none

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