简体   繁体   English

GraphQL HotChocolate 找不到输入类型的兼容构造函数

[英]GraphQL HotChocolate no compatible constructor find for input type

This is the exception being raised:这是引发的异常:

在此处输入图片说明

I goes it has something to do with not being able to resolve my base class.我认为这与无法解析我的基类有关。

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();
        }
...
}

Here is my Base:这是我的基地:

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;
        }

And this is how I registered the services.这就是我注册服务的方式。

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

And the object type和对象类型

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.");

        }

Is there something I'm missing?有什么我想念的吗?

If a type is used as an input object you need to make sure that either properties are settable or that you have a constructor that allows the deserializer to set the properties.如果将类型用作输入对象,则需要确保属性可设置或具有允许反序列化器设置属性的构造函数。

In your specific case you have a computed property:在您的特定情况下,您有一个计算属性:

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

You can either ignore that property with an attribute or you can provide a fluent type definition that ignores that property for inputs.您可以使用属性忽略该属性,也可以提供一个流畅的类型定义,忽略该属性的输入。

Attribute:属性:

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

Fluent:流利:

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);
    }
}

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

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