簡體   English   中英

GraphQL HotChocolate 找不到輸入類型的兼容構造函數

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

這是引發的異常:

在此處輸入圖片說明

我認為這與無法解析我的基類有關。

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

        }

有什么我想念的嗎?

如果將類型用作輸入對象,則需要確保屬性可設置或具有允許反序列化器設置屬性的構造函數。

在您的特定情況下,您有一個計算屬性:

public bool IsValid => 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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM