'No parameterless constructor defined for type SiteResourceGraphType' 消息,将存储库注入 SiteResourceGraphType 时

'No parameterless constructor defined for type SiteResourceGraphType' message, when injecting repository into SiteResourceGraphType

我有一个SiteResourceGraphTypeclass,继承自ObjectGraphTypeSiteResourceGraphType class 接受 IGenericRepository<Site, Guid> 以解析 items 字段。

当我启动我的应用程序时,出现错误 'No parameterless constructor defined for type SiteResourceGraphType'。

启动

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ServiceDbContext>(options => options
            .UseLazyLoadingProxies()
            .UseInMemoryDatabase("GraphQLServiceDatabase"));

        services.AddScoped(typeof(IGenericRepository<,>), typeof(GenericRepository<,>));

        services.AddScoped<IServiceProvider>(x => new FuncServiceProvider(type => x.GetRequiredService(type)));
        services.AddScoped<GraphQLServiceSchema>();
        services.AddScoped<SiteResourceGraphType>();
        services.AddScoped<SiteGraphType>();
        services.AddScoped<LocationGraphType>();
        services.AddScoped<LocationInputGraphType>();

        services
            .AddGraphQL(x =>
            {
                x.ExposeExceptions = true;
            })
            .AddGraphTypes(ServiceLifetime.Scoped)
            .AddDataLoader();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseGraphQL<GraphQLServiceSchema>();
        app.UseGraphQLPlayground(new GraphQLPlaygroundOptions());
    }
}

架构

public class GraphQLServiceSchema : Schema
{
    public GraphQLServiceSchema(IServiceProvider serviceProvider) : base(serviceProvider)
    {
        Query = (IObjectGraphType) serviceProvider.GetService(typeof(GraphQLServiceQuery));
        Mutation = (IObjectGraphType) serviceProvider.GetService(typeof(GraphQLServiceMutation));
    }
}

查询 我使用了来自 graphql-dotnet docs

Query Organization 技术
public class GraphQLServiceQuery : ObjectGraphType
{
    public GraphQLServiceQuery()
    {
        Field<SiteResourceGraphType>("sites", resolve: context => new { });
    }
}

目标图形类型

public class SiteResourceGraphType : ObjectGraphType
{
    public SiteResourceGraphType(IGenericRepository<Site, Guid> siteRepository)
    {
        Field<StringGraphType>("resourceCode",
            resolve: context => { return "RCODE1"; });

        Field<ListGraphType<SiteGraphType>>("items",
            resolve: context => siteRepository.GetAll());
    }
}

SiteResourceGraphType 的目的是提供如下响应结构:

{
   "sites": {
        "resourceCode": "CODE1",
        "items": [...]
    },
    ...
}

我已经查看了很多来自 Whosebug 的帖子,github 关于 graphql-dotnet 中的 DI,我尝试了这个库的不同版本(现在我正在使用“3.0.0-preview -1271" 版本).

但是关于无参数构造器的问题仍然存在。 我真的很累弄清楚我错过了什么。请帮忙!

我必须为此道歉post。问题出在我这边的外部服务中,它注册了我的 GraphQL 服务。我无法弄清楚这一点,因为错误没有指向外部服务,这对我来说并不明显。