GraphQl查询为null
我正在使用GraphQl热巧克力,需要获取数据。 我需要从具有规格的模板中获取数据,并且在规格中包含属性。 当我尝试查询数据时,模板和规格具有值,但属性是不应该的null。 这是我的实体:
模板
public long Id { get; set; }
public bool IsDeleted { get; set; }
public string Name { get; set; }
public long SpecId { get; set; }
public virtual List<Spec> Specs { get; set; }
规格
public long Id { get; set; }
public int Position { get; set; }
public string Label { get; set; }
public bool IsDeleted { get; set; }
public virtual AttributeType AttributeType { get; set; }
public long AttributeTypeId { get; set; }
public Template Template { get; set; }
public long? TemplateId { get; set; }
和最后属性,
public long Id { get; set; }
public string Description { get; set; }
public string Format { get; set; }
当我查询getTemplateById时,我使用了dataLoadeer和resolver获取数据,
[Authorize]
[GraphQLType( typeof(TemplateObjectType) )]
public Task<Template> GetTemplateById( long id, TemplateDataLoader dataLoader, CancellationToken cancellationToken )
=> dataLoader.LoadAsync( id, cancellationToken );
public class TemplateObjectType: ObjectType<Template>
{
protected override void Configure( IObjectTypeDescriptor<Template> descriptor )
{
descriptor.Field( x => x.Specs)
.ResolveWith<TemplateResolver>( r => r.GetSpecsAsync( default, default, default ) );
}
}
public async Task<IReadOnlyList<Spec>> GetSpecAsync(
[Parent] Template template,
SpecDataLoader dataLoader,
CancellationToken cancellationToken)
=> await dataLoader.LoadAsync(template.Id, cancellationToken);
protected override async Task<ILookup<long, Spec>> LoadGroupedBatchAsync( IReadOnlyList<long> keys, CancellationToken cancellationToken )
{
var result = await _dbContext.Templates
.Where( template => keys.Contains( template.Id ) )
.Select( x => new {
TemplateId= x.Id,
x.Specs
} )
.ToListAsync( cancellationToken: cancellationToken );
var final = result
.Select(x => x.Specs.Select(c => new
{
x.TemplateId,
Spec= c
}))
.SelectMany(x => x)
.ToLookup(x => x.TemplateId, x => x.Spec);
return final;
}
但是 attributetype null n ull n ull n ull n uln not n n ull note note n n n overate询问:
"data": {
"templatebyId": {
"name": "test",
"specs": [
{
"id": 4,
"position": 0,
"label": "price",
"templateId": 1,
"attributeType": null
}
]
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,EF试图优化数据库获取的数据量。由于您的代码现在,因此属性未加载。
您应该在此呼叫中包括
属性
,或者省略tolistasync
以使EF确定您需要在以后的阶段需要数据。使用将告诉框架包括数据:
By default EF tries to optimize the amount of data fetched from the database. As your code is now, the attributes are not loaded.
You should either include the
attributes
in this call, or omit theToListAsync
to have EF determine you'll need the data in a later stage.Using Include/ThenInclude will tell the framework to include the data: