【问题标题】:Hot Chocolate - Is it possible to implement my own object type with generics?Hot Chocolate - 是否可以使用泛型实现我自己的对象类型?
【发布时间】:2020-01-21 23:34:08
【问题描述】:

我写了以下对象类型类。

public class ResponseType<T> : ObjectType<ResponseEntry<T>>
{
    protected override void Configure(IObjectTypeDescriptor<ResponseEntry<T>> descriptor)
    {
        descriptor.Name("Response");
    }
}

我想像这样使用它作为解析器定义中的最外层类型。

descriptor.Field<SharedResolvers>(r => r.GetObject1(default, default, default, default))
    .Type<ResponseType<ListType<Object1>>>()
    .Name("object1");

descriptor.Field<SharedResolvers>(r => r.GetObject2(default, default, default, default))
    .Type<ResponseType<ListType<Object2>>>()
    .Name("object2");

如果我只实现object1,则此代码有效,但是一旦我添加object2,我就会收到以下错误。

System.Collections.Generic.KeyNotFoundException: 'The given key 'HotChocolate.Configuration.RegisteredType' was not present in the dictionary.'

似乎声明两个相同类类型的解析器可能存在一些问题。是这样吗?如果是这样,我有什么选择?

【问题讨论】:

    标签: graphql hotchocolate


    【解决方案1】:

    我能够通过将descriptor.Name 设置为基于T 的唯一值来解决此问题。

    descriptor.Name($"Response_{typeof(T).GetHashCode()}");
    

    然后我意识到我真正的问题是我在定义这个名字。如果您不覆盖名称,它会根据类型定义自动提供一个唯一的名称/键。

    【讨论】:

      猜你喜欢
      • 2010-09-13
      • 1970-01-01
      • 2014-08-19
      • 1970-01-01
      • 2020-11-27
      • 2011-08-23
      • 2012-06-09
      • 1970-01-01
      • 2012-06-11
      相关资源
      最近更新 更多