我试图让自动注册功能在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>();发布于 2022-04-20 09:00:07
好的,所以设法用大量的尝试和错误来解决这个问题。
通常,正确设置了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();
}
}问题是查询类没有正确设置,下面的内容使它正常工作:
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)
);https://stackoverflow.com/questions/71913776
复制相似问题