【发布时间】:2023-01-11 16:43:08
【问题描述】:
// 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"
}
}
}
结果 currencyName 不为空。当我调用它时没有货币对象。
query{
parents(id: 1){
id
currencyName
}
}
// Result
{
"data": {
"parents": {
"Id": 1,
"currencyName": null
}
}
}
结果 currencyName 为空。不可能从代码中包含像货币这样的嵌套对象? 我想在不调用 graphql 查询中的货币对象的情况下获取货币名称。
【问题讨论】:
标签: c# graphql hotchocolate