获取适用于 GraphQL.NET v5 的 AutoRegisteringInputObjectGraphType 和 AutoRegisteringObjectGraphType

Getting AutoRegisteringInputObjectGraphType and AutoRegisteringObjectGraphType working for GraphQL.NET v5

我正在尝试让自动注册功能在 GraphQL.NET 中运行。但是,列表类型出现一些问题。请注意,我是 GraphQL 世界的新手,之前从未使用过这个框架,如果您发现代码有任何改进,请告诉我。感谢对此的任何帮助。

收到以下错误消息。

"Message": "The GraphQL type for field 'BasicInformationResponse.status' could not be derived implicitly. Could not find type mapping from CLR type 'ScroW.Application.Features.Information.Queries.BasicInformation.Status' to GraphType. Did you forget to register the type mapping with the 'ISchema.RegisterTypeMapping'?",

我已经注册了以下架构和类型。请注意,BasicInformationResponse 包含如下所示的状态列表。

    public class BasicInformationResponseType : AutoRegisteringObjectGraphType<BasicInformationResponse>
    {
        public BasicInformationResponseType() : base()
        {
            Name = "BasicInformationResponseType";
        }
    }
    
    public class StatusType : AutoRegisteringObjectGraphType<Status>
    {
        public StatusType() : base()
        {
            Name = "StatusType";
        }
    }

    public class BasicInformationResponse
    {
        public string OrganizationNumber { get; set; }
        public string Name { get; set; }
        public List<Status> Status { get; set; }
    }

不确定我在这里注册的是什么类型的字段? (rn 作为 StringGraphType,但我很确定它应该是其他东西)。

    public class Query : ObjectGraphType
    {
        public Query(IMediator mediator)
        {
            FieldAsync<StringGraphType>(
                Name = "basicInformation",
                arguments: new QueryArguments(new QueryArgument<AutoRegisteringInputObjectGraphType<BasicInformationResponse>> { Name = "organizationNumber" }),
                resolve: async x =>
                {
                    return await mediator.Send(new BasicInformationQuery
                    {
                        OrganizationNumber = x.GetArgument<String>("organizationNumber")
                    });
                });
        }
    }

我的架构定义

    public class ScroWSchema : Schema
    {
        public ScroWSchema(IServiceProvider provider)
            : base(provider)
        {
            Query = (Query)provider.GetRequiredService(typeof(Query)) ?? throw new InvalidOperationException();
            // Mutation = (StarWarsMutation)provider.GetService(typeof(StarWarsMutation)) ?? throw new InvalidOperationException();

            // FieldMiddleware.Use(new InstrumentFieldsMiddleware());
        }
    }

最后启动。我不太确定什么需要注册为单身人士,而不是,并且被那里的不同指南弄糊涂了。如果可以删除,请告诉我。

            services.AddGraphQL(builder => builder
                // .AddSchema<ScroWSchema>()
                .AddSelfActivatingSchema<ScroWSchema>()
                .AddClrTypeMappings()
                .AddNewtonsoftJson()
                .AddGraphTypes(typeof(ScroWSchema).Assembly)
            );
            services.AddSingleton<IDocumentExecuter, DocumentExecuter>();
            services.AddSingleton<IGraphQLSerializer, GraphQLSerializer>();
            services.AddSingleton<ISchema, ScroWSchema>(services => new ScroWSchema(new SelfActivatingServiceProvider(services)));
            services.AddSingleton(typeof(AutoRegisteringObjectGraphType<>));
            services.AddSingleton(typeof(AutoRegisteringInputObjectGraphType<>));
            services.AddSingleton<BasicInformationResponseType>();
            services.AddSingleton<StatusType>();

好吧,通过大量的反复试验等设法解决了这个问题

总的来说,AutoRegisteringObjectGraphType 设置正确:

    public class BasicInformationResponseType : AutoRegisteringObjectGraphType<BasicInformationResponse>
    {
        public BasicInformationResponseType()
        {
        }
    }

Scema 也正确:

    public class ScroWSchema : Schema
    {
        public ScroWSchema(IServiceProvider provider)
            : base(provider)
        {
            Query = (Query)provider.GetRequiredService(typeof(Query)) ?? throw new InvalidOperationException();
        }
    }

问题是查询 class,它没有正确设置,下面让它工作:

    public class Query : ObjectGraphType
    {
        public Query(IMediator mediator)
        {
            FieldAsync<BasicInformationResponseType>(
                Name = "basicInformation",
                arguments: new QueryArguments(new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "organizationNumber" }),
                resolve: async x =>
                {
                    return await mediator.Send(new BasicInformationQuery
                    {
                        OrganizationNumber = x.GetArgument<string>("organizationNumber")
                    });
                });
        }
    }

关于启动服务是通过GraphQL注册自动添加的,不需要添加具体的服务。

            services.AddGraphQL(builder => builder
                .AddSelfActivatingSchema<ScroWSchema>()
                .AddClrTypeMappings()
                .AddSystemTextJson()
                .AddGraphTypes(typeof(ScroWSchema).Assembly)
            );