简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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