Search code examples
c#.netgraphqlwarnings

What should I use instead of .net GraphQL FieldAsync method?


There is a web API service in .net that I developed with GraphQL. The libraries I use are as follows:

<PackageReference Include="GraphQL.MicrosoftDI" Version="8.0.2" />
<PackageReference Include="GraphQL.Server.Transports.AspNetCore" Version="8.0.2" />
<PackageReference Include="GraphQL.Server.Ui.Playground" Version="8.0.2" />
<PackageReference Include="GraphQL.SystemTextJson" Version="8.0.2" />

I have an IExampleService interface. Let's assume that there is a method in this interface that retrieves a list from the db. I have to return it in Task<List> format.

public class ExampleQuery : ObjectGraphType
{
    private readonly IExampleService _exampleService;

    public ExampleQuery(IExampleService exampleService)
    {
        _exampleService = exampleService;

        FieldAsync<ListGraphType<ExampleType>>(
            "Examples",
            resolve: async context =>
            {
                return await _exampleService.GetExamples();
            }
        );
    }
}

The above code works fine but gives warnings like this:

CS0618: 'ComplexGraphType<object?>.FieldAsync(string, string?, QueryArguments?, Func<IResolveFieldContext<object?>, Task<object?>>?, string?)' is obsolete: 'Please use one of the Field() methods returning FieldBuilder and the methods defined on it or just use AddField() method directly. This method will be removed in v9.' Example.API

GQL004: Don't use obsolete 'Field' methods Example.API

I already use the AddField method in synchronous functions. But when I need to use an asynchronous method, which method should I use instead of FieldAsync?


Solution

  • You can rewrite it like this

    Field<ListGraphType<ExampleType>>("Examples")
        .ResolveAsync(async => await _exampleService.GetExamples());
    

    Or just allow the provided automatic code fix to rewrite it for you enter image description here

    GQL004: Don't use obsolete Field methods