简体   繁体   English

HotChocolate 未将模型正确映射到类型

[英]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.如果这些对象需要支持不同的对象类型,我建议使用联合。

Learn more about the AnyType 了解有关 AnyType 的更多信息

Learn more about unions 了解有关工会的更多信息

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

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