简体   繁体   English

HotChocolate (GraphQL) 在没有查询调用的情况下不可能包含嵌套的 object

[英]HotChocolate (GraphQL) it is impossible include nested object without query calling

// Parent entity
public class Parent
{
   public int Id {get; set;}
   public int CurrencyId {get; set;}
   public virtual Currency Currency {get; set;}
}
// Currency Entity
public class Currency
{
  public int Id {get; set;}
  public string Name {get; set;}
  public virtual ICollection<Parent> Parents {get; set;}
}

// Query
[UseDbContext(typeof(AppDbContext))]
[UseFirstOrDefault]
[UseProjection]
[UseFiltering]
[UseSorting]
public IQueryable<Parent> GetParents([ScopedService] AppDbContext context, int id)
{
  return context.Set<Parent>().Where(x => x.id == id);
}

public class ParentType : ObjectType<Parent>
{
   protected override void Configure(IObjectTypeDescriptor<Parent> descriptor)
    {
        descriptor.Field("currencyName")
            .ResolveWith<Resolvers>(t => t.GetCurrencyName(default!));
    }

    private class Resolvers
    {
        public string GetCurrencyName([Parent] Parent parent)
        {
            return parent?.Currency?.Name;
        }
    }
}

And when I call it's with graphql query with currency object.当我用 graphql 查询和货币 object 调用它时。

    query{
     parents(id: 1){
      id
      currencyName
      currency{
       name
      }
     }
    }

//Result
{
  "data": {
    "parents": {
      "id": 1,
      "currency": {
        "name": "USD"
      },
      "currencyName": "USD"
    }
  }
}

Result currencyName is not null. And when I call it's without currency object.结果 currencyName 不是 null。当我调用它时没有货币 object。

       query{
         parents(id: 1){
          id
          currencyName
         }
        }
// Result
{
  "data": {
    "parents": {
      "Id": 1,
      "currencyName": null
    }
  }
}

And result currencyName is null. It is impossible include nested object like currency from code?结果 currencyName 是 null。不可能像代码中的货币一样包含嵌套的 object? I want get currency Name without calling currency object in graphql query.我想在 graphql 查询中不调用货币 object 来获取货币名称。

No this is currently not possible as the property you access is only resolved if the nested class is selected不,这目前是不可能的,因为您访问的属性只有在选择嵌套 class 时才能解决

It seems that you have projections enabled.您似乎启用了投影。 In this case, you are not selecting the referenceid, and this is not being selected.在这种情况下,您没有选择 referenceid,它也没有被选中。 as a consequence, your resolver is receiving 0 and cant resolve it correctly.因此,您的解析器收到 0 并且无法正确解析它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM