[英]HotChocolate not mapping models properly to type
Hotchocolate does not appear to have mapped one of my models properly. Hotchocolate 似乎没有正确映射我的模型之一。 I have this product search model:
我有这个产品搜索模型:
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; }
}
As you can see there, it has a FacetModel which looks like this:正如你在那里看到的,它有一个如下所示的 FacetModel :
public class FacetModel
{
public object Value { get; set; }
public object From { get; set; }
public object To { get; set; }
public long? Count { get; set; }
}
For some reason, this is not being mapped properly by hotchololate:出于某种原因,hotchololate 没有正确映射:
I have created a type and registered it like this:我创建了一个类型并像这样注册它:
public class FacetType: ObjectType<FacetModel>
{
}
public static void AddGraphQLServices(this IServiceCollection services)
{
services
.AddGraphQLServer()
.AddType<FacetType>()
.AddQueryType<Query>();
}
But if I look at the docs, it looks like this:但是如果我查看文档,它看起来像这样:
Does anyone know what is happening with this?有谁知道这是怎么回事?
This is because the type of your properties are object
.这是因为您的属性类型是
object
。 Hot Chocolate doesn't know how to handle object
, since there's no concept of an object
in GraphQL. Hot Chocolate 不知道如何处理
object
,因为在 GraphQL 中没有object
的概念。 There's the object type, yes, but it has a known set of fields, which the object
does not have - it could be anything.有对象类型,是的,但它有一组已知的字段,
object
没有 - 它可以是任何东西。
That being said, there's an AnyType
in Hot Chocolate.话虽
AnyType
,热巧克力中有一个AnyType
。 You could annotate your object as an AnyType
and then retrieve the actual value in your resolvers.您可以将对象注释为
AnyType
,然后在解析器中检索实际值。
public class FacetModel
{
[GraphQLType(typeof(AnyType))]
public object Value { get; set; }
}
The AnyType
only works for scalars (string, int, ...) and IDictionary
though. AnyType
仅适用于标量(字符串、整数、...)和IDictionary
。 If these objects need to support different object types, I'd suggest using unions.如果这些对象需要支持不同的对象类型,我建议使用联合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.