Required service for type RootQuery not found in ASP.NET Core GraphQL

Viewed 160

I have two queries in my project - Trade and Entity. Those looks like below

 public class TradeQuery : ObjectGraphType
{
    public TradeQuery(ITradeService tradeservice)
    {
        Field<ListGraphType<TradeType>>("trades", resolve: r => { return tradeservice.GetTradesAsync(); });

        Field<TradeType>("trade", arguments: new QueryArguments(new QueryArgument<IntGraphType> { Name = "id" }),
            resolve: context =>
            {
                return tradeservice.GetTradeByIdAsync(context.GetArgument<int>("id"));
            });
    }
}
 public class EntityQuery : ObjectGraphType
{
    public EntityQuery(IEntityService tradeservice)
    {
        Field<ListGraphType<EntityType>>("Entitys", resolve: r => { return tradeservice.GetEntitysAsync(); });

        Field<EntityType>("Entity", arguments: new QueryArguments(new QueryArgument<IntGraphType> { Name = "id" }),
            resolve: context =>
            {
                return tradeservice.GetEntityByIdAsync(context.GetArgument<int>("id"));
            });
    }
}

and Designed a RootQuery for this as

public class RootQuery : ObjectGraphType
{
    public RootQuery()
    {
        Field<TradeQuery>("tradeQuery", resolve: r => new { });
        Field<EntityQuery>("EntityQuery", resolve: r => new { });
    }
}

And RootSchema is as below

public class RootSchema : Schema
{
    public RootSchema(IServiceProvider serviceProvider) : base(serviceProvider)
    {
        Query = serviceProvider.GetRequiredService<RootQuery>();
        Mutation = serviceProvider.GetRequiredService<RootMutation>();
    }
}

When I run this application it is giving an exception at below line

Query = serviceProvider.GetRequiredService<RootQuery>();

The exception is - Required service for type ProjectName.RootQuery not found

I am using GraphQL.Server.Transports.AspNetCore.SystemTextJson nuget 4.4.1 version

Can you please guide, what could be missing?

1 Answers

Add RootQuery to your service container.

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
     ...
     services.AddScoped<RootQuery>();

}
Related