graphql .NET核心解决方案-无法包含相关对象

时间:2019-02-26 09:42:48

标签: graphql

我是graphql的新手,并在.Net核心解决方案中使用它。我的解决方案包括一个SQlite数据库,可以通过管理器进行访问。我在GraphTypes的解析器函数中使用管理器。在模型中,我有一个“ Ticket”类和一个“ TicketResponse”类-一个票证包含ticketResponses列表。

问题是我无法包含ObjectGraphType的嵌套数据。 这是我的代码:

namespace SupportCenter.Schema.GraphQLTypes
{
    public class TicketType : ObjectGraphType<Ticket>
    {
        public TicketType(ITicketManager manager)
        {
            Name = "Ticket";
            Field(x => x.Id, type: typeof(IdGraphType)).Description("The id of the ticket");
            Field(x => x.AccountId).Description("THe account the ticket was issued from");
            Field(x => x.Text).Description("The description of the problem");
            Field(x => x.DateOpened).Description("The date the ticket was opened");
            Field<TicketStateEnum>("state", resolve: context => context.Source.State);
            Field<ListGraphType<TicketResponseType>>("ticketresponses",
              resolve: context => manager.GetTicketResponses(context.Source.Id));
        }
    }
}

我得到的错误如下:

  

“ GraphQL.ExecutionError:类型无服务   'GraphQL.Types.ObjectGraphType 1[SupportCenter.Schema.GraphQLTypes.TicketType]' has been registered. ---> System.InvalidOperationException: No service for type 'GraphQL.Types.ObjectGraphType 1 [SupportCenter.Schema.GraphQLTypes.TicketType]'   已注册。\ n   Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider   提供者,输入serviceType)\ n   GraphQL.Types.Schema.b__56_2(Type type)\ n在   GraphQL.Types.GraphTypesLookup.AddTypeIfNotRegistered(Type type,   TypeCollectionContext context)\ n在   GraphQL.Types.GraphTypesLookup.HandleField(Type parentType,FieldType   字段,TypeCollectionContext上下文)\ n位于   GraphQL.Types.GraphTypesLookup.AddType(IGraphType类型,   TypeCollectionContext context)\ n在   GraphQL.Types.GraphTypesLookup.HandleField(Type parentType,FieldType   字段,TypeCollectionContext上下文)\ n位于   GraphQL.Types.GraphTypesLookup.AddType(IGraphType类型,   TypeCollectionContext context)\ n在   GraphQL.Types.GraphTypesLookup.HandleField(Type parentType,FieldType   字段,TypeCollectionContext上下文)\ n位于   GraphQL.Types.GraphTypesLookup.AddType(IGraphType类型,   TypeCollectionContext context)\ n在   GraphQL.Types.GraphTypesLookup.Create(IEnumerable 1 types, IEnumerable 1指令,Func 2 resolveType, IFieldNameConverter fieldNameConverter)\n at System.Lazy 1.ViaFactory(LazyThreadSafetyMode模式)\ n--堆栈结束   从先前引发异常的位置开始跟踪--- \ n   System.Lazy`1.CreateValue()\ n在   GraphQL.Types.Schema.get_AllTypes()\ n在   GraphQL.Instrumentation.FieldMiddlewareBuilder.ApplyTo(ISchema   模式)\ n在GraphQL.DocumentExecuter.ExecuteAsync(ExecutionOptions   选项)\ n ---内部异常堆栈跟踪的结尾---“,

我已经在启动文件的服务中注册了TicketType和TicketResponseType。有什么想法我做错了吗?

这是我的启动文件:

public class Startup
    {

        public void ConfigureServices(IServiceCollection services)
        {   
            services.AddMvc();

            services.AddSingleton<SupportCenterDbContext>();

            services.AddSingleton<TicketType>();
            services.AddSingleton<TicketInputCreateType>();
            services.AddSingleton<TicketResponseType>();
            services.AddSingleton<TicketStateEnum>();
            services.AddSingleton<ResponseQuery>();
            services.AddSingleton<ITicketManager, TicketManager>();

            services.AddSingleton<SupportCentreQuery>();
            services.AddSingleton<TicketsQuery>();
            services.AddSingleton<TicketsMutation>();

            services.AddSingleton<SupportCentreSchema>();

            services.AddSingleton<IDependencyResolver>(
                c => new FuncDependencyResolver(c.GetRequiredService));

            services.AddSingleton<IDocumentExecuter, DocumentExecuter>();
            services.AddSingleton<IDocumentWriter, DocumentWriter>();

            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            services.AddSingleton<IGraphQLExecuter<SupportCentreSchema>, DefaultGraphQLExecuter<SupportCentreSchema>>();

            services.AddGraphQL(options =>
                {
                    options.EnableMetrics = true;
                    options.ExposeExceptions = true;
                })
                .AddWebSockets()
                .AddDataLoader();
        }

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

                 app.UseDefaultFiles();
                 app.UseStaticFiles();

                 app.UseGraphQL<SupportCentreSchema>("/graphql");

                 app.UseGraphiQLServer(new GraphiQLOptions
                 {
                    GraphiQLPath = "/ui/graphiql",
                    GraphQLEndPoint = "/graphql"
                 });

                 app.UseMvc();
        }
    }

0 个答案:

没有答案