问题描述
Hotchocolate 似乎没有正确映射我的模型之一。 我有这个产品搜索模型:
public class ProductSearchResponseModel : ISearchResult<SimpleProductviewmodel>
{
public string Identifier { get; set; }
public Dictionary<string,List<FacetModel>> Facets { get; set; }
public bool HasMoreResults { get; set; }
public long Total { get; set; }
public List<SimpleProductviewmodel> Items { get; set; }
}
如您所见,它有一个 FacetModel,如下所示:
public class FacetModel
{
public object Value { get; set; }
public object From { get; set; }
public object To { get; set; }
public long? Count { get; set; }
}
出于某种原因,hotchololate 没有正确映射:
public class FacetType: ObjectType<FacetModel>
{
}
public static void AddGraphQLServices(this IServiceCollection services)
{
services
.AddGraphQLServer()
.AddType<FacetType>()
.AddQueryType<Query>();
}
但是如果我查看文档,它看起来像这样:
有人知道这是怎么回事吗?
解决方法
这是因为您的属性类型是 object
。 Hot Chocolate 不知道如何处理 object
,因为 GraphQL 中没有 object
的概念。
有对象类型,是的,但它有一组已知的字段,object
没有 - 它可以是任何东西。
话虽如此,热巧克力中有一个 AnyType
。您可以将对象注释为 AnyType
,然后在解析器中检索实际值。
public class FacetModel
{
[GraphQLType(typeof(AnyType))]
public object Value { get; set; }
}
AnyType
仅适用于标量(字符串、整数、...)和 IDictionary
。如果这些对象需要支持不同的对象类型,我建议使用联合。