HotChocolate (GraphQL) 在没有查询调用的情况下不可能包含嵌套对象
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;
}
}
}
当我使用带货币对象的 graphql 查询调用它时。
query{
parents(id: 1){
id
currencyName
currency{
name
}
}
}
//Result
{
"data": {
"parents": {
"id": 1,
"currency": {
"name": "USD"
},
"currencyName": "USD"
}
}
}
结果货币名称不为空。当我调用它时没有货币对象。
query{
parents(id: 1){
id
currencyName
}
}
// Result
{
"data": {
"parents": {
"Id": 1,
"currencyName": null
}
}
}
结果 currencyName 为空。不可能从代码中包含像货币这样的嵌套对象?
我想在 graphql 查询中不调用货币对象来获取货币名称。
不,这目前是不可能的,因为您访问的 属性 只有在选择嵌套 class 时才会解析
// 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;
}
}
}
当我使用带货币对象的 graphql 查询调用它时。
query{
parents(id: 1){
id
currencyName
currency{
name
}
}
}
//Result
{
"data": {
"parents": {
"id": 1,
"currency": {
"name": "USD"
},
"currencyName": "USD"
}
}
}
结果货币名称不为空。当我调用它时没有货币对象。
query{
parents(id: 1){
id
currencyName
}
}
// Result
{
"data": {
"parents": {
"Id": 1,
"currencyName": null
}
}
}
结果 currencyName 为空。不可能从代码中包含像货币这样的嵌套对象? 我想在 graphql 查询中不调用货币对象来获取货币名称。
不,这目前是不可能的,因为您访问的 属性 只有在选择嵌套 class 时才会解析