docs2/site/docs/analyzers/gql011.md
IGraphType| Value | |
|---|---|
| Rule ID | GQL011 |
| Category | Usage |
| Default severity | Error |
| Enabled by default | Yes |
| Code fix provided | No |
| Introduced in | v7.7 |
This rule triggers when a type implementing IGraphType used as the type
argument when the type argument must NOT be convertible to IGraphType.
Multiple generic types and methods in the GraphQL library receive unconstrained
type arguments. This diagnostic rule simulates where T : not IGraphType
constraint which is not supported in .NET.
Use appropriate type argument.
In the following example the PersonGraphType is incorrectly used as the type
argument of the InterfaceGraphType<TSource> type.
public class MyInterfaceGraphType : InterfaceGraphType<PersonGraphType>
{
}
public class PersonGraphType : ObjectGraphType<Person>
{
}
public class Person { }
In this example, the StringGraphType is incorrectly used as the return type of
the resolver
public class MyGraphType : ObjectGraphType
{
public MyGraphType()
{
Field<StringGraphType>("name")
.Returns<StringGraphType>()
.Resolve(context => null);
}
}
Use Person as the argument type of the InterfaceGraphType<TSource> type
public class MyInterfaceGraphType : InterfaceGraphType<Person>
{
}
public class PersonGraphType : ObjectGraphType<Person>
{
}
public class Person { }
Use string type as the return type of the resolver
public class MyGraphType : ObjectGraphType
{
public MyGraphType()
{
Field<StringGraphType>("name")
.Returns<string>()
.Resolve(context => null);
}
}
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 GQL011
// The code that's violating the rule is on this line.
#pragma warning restore GQL011
To disable the rule for a file, folder, or project, set its severity to none
in the
configuration file.
[*.cs]
dotnet_diagnostic.GQL011.severity = none
For more information, see How to suppress code analysis warnings.