GQL012: Illegal method usage
Value | |
---|---|
Rule ID | GQL012 |
Category | Usage |
Default severity | Error |
Enabled by default | Yes |
Code fix provided | No |
Introduced in | v8.0 |
Note: This rule replaces GQL005
Cause
This rule triggers when FiledBuilder
's method is used to build a field of not
appropriate type.
Rule description
GraphQL.NET utilizes the FieldBuilder
type for constructing fields of various
types. Certain builder methods are applicable to input, output, and interface
fields, while others are constrained for use with specific graph types. For
example, the Resolve
method is exclusively meant for output types,
ParseValue
is designed for input types, and Argument
is applicable to either
output or interface types.
How to fix violations
Remove the illegal method invocation.
Example of a violation
In the given example, the Argument
method is employed for the input graph
type, the Resolve
method for the interface graph type, and the Validate
method for the output graph type.
public class MyInputGraphType : InputObjectGraphType<User>
{
public MyInputGraphType()
{
Field<StringGraphType>("Name")
// Argument is only allowed on output and interface types
// but used on input type
.Argument<BooleanGraphType>("fullName");
}
}
public class MyInterfaceGraphType : InputObjectGraphType<User>
{
public MyInterfaceGraphType()
{
Field<StringGraphType>("Name")
// Resolve is only allowed on output types
// but used on input type
.Resolve(context => context.Source.Name);
}
}
public class MyOutput : ObjectGraphType<User>
{
public MyOutput()
{
Field<StringGraphType>("Name")
// Validate is only allowed on input types
// but used on output type
.Validate(name =>
{
if (((string)name).Length < 20)
throw new InvalidOperationException();
})
.Resolve(context => context.Source.Name);
}
}
Example of how to fix
Remove illegal method invocations
public class MyInputGraphType : InputObjectGraphType<User>
{
public MyInputGraphType()
{
Field<StringGraphType>("Name");
}
}
public class MyInterfaceGraphType : InputObjectGraphType<User>
{
public MyInterfaceGraphType()
{
Field<StringGraphType>("Name");
}
}
public class MyOutput : ObjectGraphType<User>
{
public MyOutput()
{
Field<StringGraphType>("Name")
.Resolve(context => context.Source.Name);
}
}
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 GQL012
// The code that's violating the rule is on this line.
#pragma warning restore GQL012
To disable the rule for a file, folder, or project, set its severity to none
in the
configuration file.
[*.cs]
dotnet_diagnostic.GQL012.severity = none
For more information, see How to suppress code analysis warnings.