GraphQl查询为null

发布于 2025-01-22 18:32:14 字数 3494 浏览 0 评论 0 原文

我正在使用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
            }
        ]
    }
}

I'm using graphQL hot chocolate and need to fetch data.
I need to fetch data from Template that has Specs and in spec it contains AttributeType.
when i try to query the data, template and spec has values but AttributeType is null that should not be.
here are my entities:

Template

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; }

Spec

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; }

and finally AttributeType

public         long            Id          { get; set; }
public         string          Description { get; set; }
public         string          Format      { get; set; }

I used Dataloader and resolver to fetch the data

[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;
}

When I query GetTemplateById I get the result but AttributeType is null which shouldn't be, here is a sample of the query:

"data": {
    "templatebyId": {
        "name": "test",
        "specs": [
            {
                "id": 4,
                "position": 0,
                "label": "price",
                "templateId": 1,
                "attributeType": null
            }
        ]
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤君无依 2025-01-29 18:32:14

默认情况下,EF试图优化数据库获取的数据量。由于您的代码现在,因此属性未加载。

您应该在此呼叫中包括属性,或者省略 tolistasync 以使EF确定您需要在以后的阶段需要数据。

var result = await _dbContext.Templates
                               .Where( template => keys.Contains( template.Id ) )
                               .Select( x => new {
                                                   TemplateId= x.Id,
                                                   x.Specs
                                                 } )
                               .ToListAsync( cancellationToken: cancellationToken );

使用将告诉框架包括数据:

var result = await _dbContext.Templates
                           .Where( template => keys.Contains( template.Id ) )
                           .Include(prop => prop.Specs)
                           .ThenInclude(spec => spec.AttributeType) 
                           .Select( x => new {
                                               TemplateId= x.Id,
                                               x.Specs
                                             } )
                           .ToListAsync( cancellationToken: cancellationToken );

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 the ToListAsync to have EF determine you'll need the data in a later stage.

var result = await _dbContext.Templates
                               .Where( template => keys.Contains( template.Id ) )
                               .Select( x => new {
                                                   TemplateId= x.Id,
                                                   x.Specs
                                                 } )
                               .ToListAsync( cancellationToken: cancellationToken );

Using Include/ThenInclude will tell the framework to include the data:

var result = await _dbContext.Templates
                           .Where( template => keys.Contains( template.Id ) )
                           .Include(prop => prop.Specs)
                           .ThenInclude(spec => spec.AttributeType) 
                           .Select( x => new {
                                               TemplateId= x.Id,
                                               x.Specs
                                             } )
                           .ToListAsync( cancellationToken: cancellationToken );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文