我有一个SiteResourceGraphType
类,它继承自ObjectGraphType
. SiteResourceGraphType
类接受IGenericRepository<Site, Guid>
以解析items
字段。
当我启动我的应用程序时,我收到错误“没有为类型 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 文档的查询组织技术
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": [...]
},
...
}
我已经查看了来自 stackoverflow、github 的很多关于 graphql-dotnet 中 DI 的帖子,我尝试了这个库的不同版本(现在我使用的是“3.0.0-preview-1271”版本)。
但是关于无参数 ctor 的问题仍然存在。我真的厌倦了弄清楚我错过了什么。请帮忙!