[英]No service for type 'ProjectModels.Graphs.ProjectGraph' has been registered
I am trying to implement GraphQl with entityframework using GraphQL.EntityFramework and I am getting below error. 我正在尝试使用GraphQL.EntityFramework和EntityFramework来实现GraphQl,但出现错误。
GraphQL.ExecutionError: No service for type 'ProjectModels.Graphs.ProjectGraph' has been registered. ---> System.InvalidOperationException: No service for type 'ProjectModels.Graphs.ProjectGraph' has been registered.
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at GraphQL.Types.Schema.<CreateTypesLookup>b__56_2(Type type)
at GraphQL.Types.GraphTypesLookup.AddTypeIfNotRegistered(Type type, TypeCollectionContext context)
at GraphQL.Types.GraphTypesLookup.HandleField(Type parentType, FieldType field, TypeCollectionContext context)
at GraphQL.Types.GraphTypesLookup.AddType(IGraphType type, TypeCollectionContext context)
at GraphQL.Types.GraphTypesLookup.Create(IEnumerable`1 types, IEnumerable`1 directives, Func`2 resolveType, IFieldNameConverter fieldNameConverter)
at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
--- End of stack trace from previous location where exception was thrown ---
at System.Lazy`1.CreateValue()
at GraphQL.Types.Schema.get_AllTypes()
at GraphQL.Instrumentation.FieldMiddlewareBuilder.ApplyTo(ISchema schema)
at GraphQL.DocumentExecuter.ExecuteAsync(ExecutionOptions options)
--- End of inner exception stack trace ---
My ProjectGraph looks like below. 我的ProjectGraph如下所示。
using GraphQL.EntityFramework;
using ProjectModels.Master;
using ProjectModels;
namespace ProjectModels.Graphs
{
public class ProjectGraph : EfObjectGraphType<Project>
{
public ProjectGraph(IEfGraphQLService graphQlService) :
base(graphQlService)
{
Field(x => x.Id);
Field(x => x.Name);
Field(x => x.ProjectGroup, type: typeof(ProjectGroupGraph));
Field(x => x.ProjectUser, type: typeof(ProjectUserGraph));
AddNavigationListField(
name: "datasource",
resolve: context =>
{
return (context.UserContext as DataContextList).projectDbContext.Datasource;
});
}
}
}
My Query class looks like below: 我的查询类如下所示:
public class Query :
QueryGraphType
{
public Query(IEfGraphQLService graphQlService) :
base(graphQlService)
{
AddQueryField(
name: "Project",
resolve: context =>
{
var dataContext = (context.UserContext as DataContextList).masterDbContext;
return dataContext.Project;
});
}
}
Following has been followed in startup.cs -> ConfigureServices 在startup.cs-> ConfigureServices中遵循以下内容
GraphTypeTypeRegistry.Register<Project, ProjectGraph>();
I am querying like below 我正在查询如下
{
project {
name,
{
datasource {
name
}
}
}
}
What am I missing here? 我在这里想念什么?
Please try this in your ConfigureServices method in Startup.cs : 请在Startup.cs的ConfigureServices方法中尝试此操作:
services.AddSingleton<IDependencyResolver>(s => new FuncDependencyResolver(s.GetRequiredService));
services.AddSingleton<ProjectGraph>();
You need to register a dependency resolver and your class in dotnet core IOC container 您需要在dotnet核心IOC容器中注册一个依赖关系解析器和您的类
Refer to: https://graphql-dotnet.github.io/docs/getting-started/dependency-injection 请参阅: https : //graphql-dotnet.github.io/docs/getting-started/dependency-injection
If you have followed their startup.cs file, and coded as below. 如果您遵循他们的startup.cs文件,并且编码如下。
foreach (var type in GetGraphQlTypes())
{
services.AddSingleton(type);
}
static IEnumerable<Type> GetGraphQlTypes()
{
return typeof(Startup).Assembly
.GetTypes()
.Where(x => !x.IsAbstract &&
(typeof(IObjectGraphType).IsAssignableFrom(x) ||
typeof(IInputObjectGraphType).IsAssignableFrom(x)));
}
Then make sure that classes are in same project, otherwise it will not work. 然后,请确保类在同一项目中,否则它将无法正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.