IDependencyResolver and ResolveFieldContext cannot be resolved while using latest GraphQL library in asp.net core 3.1

Viewed 976

I am working on a project where I need to use GraphQL in my asp.net core 3.1 project and this is the first time I am working with GraphQL. I added all the required dependencies and when I try to build, it gives compilation error as follows:

PKIUYTSWS123:example-orders andy$ dotnet build
Microsoft (R) Build Engine version 16.7.1+52cd83677 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  All projects are up-to-date for restore.
Schema/OrdersSchema.cs(11,48): error CS0246: The type or namespace name 'IDependencyResolver' could not be found (are you missing a using directive or an assembly reference?) [/Users/andy/Documents/work/git/example-orders/Orders/Orders.csproj]
Schema/OrdersSubscription.cs(34,41): error CS0246: The type or namespace name 'ResolveFieldContext' could not be found (are you missing a using directive or an assembly reference?) [/Users/andy/Documents/work/git/example-orders/Orders/Orders.csproj]

Build FAILED.

Schema/OrdersSchema.cs(11,48): error CS0246: The type or namespace name 'IDependencyResolver' could not be found (are you missing a using directive or an assembly reference?) [/Users/andy/Documents/work/git/example-orders/Orders/Orders.csproj]
Schema/OrdersSubscription.cs(34,41): error CS0246: The type or namespace name 'ResolveFieldContext' could not be found (are you missing a using directive or an assembly reference?) [/Users/andy/Documents/work/git/example-orders/Orders/Orders.csproj]
    0 Warning(s)
    2 Error(s)

It looks like IDependencyResolver was removed in GraphQL.NET 3.0 and I am not sure on how can I fix this issue in my project. This is all the dependencies I have here -

Orders.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Compile Remove="Types\**" />
    <EmbeddedResource Remove="Types\**" />
    <None Remove="Types\**" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="GraphQL" Version="3.3.1" />
    <PackageReference Include="System.Reactive" Version="5.0.0" />
    <PackageReference Include="System.Reactive.Compatibility" Version="5.0.0" />
  </ItemGroup>
</Project>

Server.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFrameworks>netcoreapp3.1</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="GraphQL.Server.Transports.AspNetCore" Version="4.2.0" />
    <PackageReference Include="GraphQL.Server.Transports.WebSockets" Version="4.2.0" />
    <PackageReference Include="GraphQL.Server.Ui.GraphiQL" Version="4.2.0" />
    <PackageReference Include="GraphQL.Server.Ui.Playground" Version="4.2.0" />
    <PackageReference Include="GraphQL.Server.Ui.Voyager" Version="4.2.0" />
    <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\Orders\Orders.csproj" />
  </ItemGroup>
</Project>

Here is my OrdersSubscription.cs class and OrdersSchema.cs class.

I have put together an example here in this git repo which outlines this problem. Any thoughts why I am getting this error and how can I fix it? Any help will be appreciate in fixing this issue in my git repo.

I am gonna run this code in production so trying to figure out the right and efficient way to integrate GraphQL in asp.net core 3.1 project.

1 Answers

You are better use examples from this repo because it's up to date.

But to provide you info on what has changed:

  1. There is no more IDependencyResolver, IServiceProvider used instead. You should pass it to base constructor instead of setting it directly to DependencyResolver.

  2. Your mutations have code like this

    return await context.TryAsyncResolve(
        async c => await orders.CloseAsync(orderId));
    

    Not sure what is TryAsyncResolve but you should just call

    return await orders.CloseAsync(orderId));
    
  3. You should use interfaces IResolveEventStreamContext and IResolveFieldContext instead of ResolveEventStreamContext and ResolveFieldContext classes.

  4. You should install GraphQL.SystemTextJson or GraphQL.NewtonsoftJson and use chosen implementation.

  5. You should provide UserContext.

Check sample projects and documentation for more info.

Related