简体   繁体   中英

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.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.

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

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 :

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

Refer to: https://graphql-dotnet.github.io/docs/getting-started/dependency-injection

If you have followed their startup.cs file, and coded as below.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM