【问题标题】:GraphQL HotChocolate no compatible constructor find for input typeGraphQL HotChocolate 找不到输入类型的兼容构造函数
【发布时间】:2021-12-23 05:56:26
【问题描述】:

这是引发的异常:

我认为这与无法解析我的基类有关。

public class Project : BaseEntity<ProjectId>
    {
        public string Name { get; private set; }
        public string Description { get; private set; }

        private List<Asset> _assets = new();
        public IReadOnlyList<Asset> Assets => _assets;

        public Project(ProjectId id, string name, string description)
        {
            Id = id;
            Name = name;
            Description = description;

            Validate();
        }
...
}

这是我的基地:

public abstract class BaseEntity<TId>
    {
        public TId Id { get; set; }

        public ValidationResult ValidationResult { get; set; }

        public bool IsValid => ValidationResult?.IsValid ?? Validate();

        protected abstract bool Validate();

        protected bool RunValidation<TValidator, TEntity>(TEntity entity, TValidator validator)
          where TValidator : AbstractValidator<TEntity>
          where TEntity : BaseEntity<TId>
        {
            ValidationResult = validator.Validate(entity);
            return IsValid;
        }

这就是我注册服务的方式。

builder.Services
            .AddGraphQLServer()
            .AddQueryType<ProjectQueries>()
            .AddType<ProjectType>();

以及对象类型

public class ProjectType : ObjectType<Project>, IViewModel
    {
        protected override void Configure(IObjectTypeDescriptor<Project> descriptor)
        {
            descriptor
                .Field(p => p.Id)
                .Description("Unique ID for the project.");

            descriptor
                .Field(p => p.Name)
                .Description("Represents the name for the project.");

        }

我有什么遗漏吗?

【问题讨论】:

  • 何时引发此异常?
  • 启动期间。
  • 发布完整的异常文本(即 Exception.ToString() 的结果),而不是异常框的图像

标签: c# graphql hotchocolate .net-6.0


【解决方案1】:

如果将类型用作输入对象,您需要确保属性是可设置的,或者您有一个允许反序列化器设置属性的构造函数。

在您的特定情况下,您有一个计算属性:

public bool IsValid =&gt; ValidationResult?.IsValid ?? Validate();

您可以使用属性忽略该属性,也可以提供一个流利的类型定义,忽略该输入属性。

属性:

[GraphQLIgnore]
public bool IsValid => ValidationResult?.IsValid ?? Validate();

流利:

public class ProjectInputType : InputObjectType<Project>
{
    protected override void Configure(IInputObjectTypeDescriptor<Project> descriptor)
    {
        // make sure to ignore all unusable props here that are public.
        descriptor.Ignore(t => t.IsValid);
        descriptor.Ignore(t => Assets);
    }
}

【讨论】:

  • 我试过了,无论是属性还是内部配置,仍然是我上面发布的错误消息。
  • 它与包含这些属性的类是抽象类有关吗?
  • 应该不是问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-12
  • 2013-04-22
  • 1970-01-01
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
  • 2020-11-11
相关资源
最近更新 更多