为什么我在 GraphQl 分页中出现“解析器不是像 IEnumerable、IQueryable、IList 这样的可迭代类型”错误?

问题描述 投票:0回答:0

我正在使用包装器返回 API 响应,其中

Error
ICollection
的一种。我从
Hot Chocolate
收到以下错误并试图理解它。这是一个简单的操作,我根据条件返回一个列表。这将是只读的。我正在从我的回购中返回
AsAsyncEnumerable()

只有在我使用 pagination 时才会发生这种情况。所以不确定如何解决这个问题。

错误:

“消息”:“有关更多详细信息,请查看

Errors
属性。 1. 有关更多详细信息,请查看
Errors
属性。 1. 无法从当前解析器推断出元素类型。如果解析器不是可迭代类型(如 IEnumerable、IQueryable、IList 等),通常会发生这种情况。确保您显式指定元素类型或解析器的返回类型是可迭代类型。 (HotChocolate.Types.ObjectType) “

代码:

MyWrapper.cs

 public class ServiceResponse<T> : ServiceResponse
  {
    public T Result { get; set; }

    public ServiceResponse()
      : this(default (T), ResultCode.Success, (ICollection<Error>) new List<Error>())
    {
    }

    public ServiceResponse(T result, ResultCode statusCode)
      : this(result, statusCode, (ICollection<Error>) new List<Error>())
    {
    }

    public ServiceResponse(T result, ResultCode statusCode, ICollection<Error> errors)
      : base(statusCode, errors)
    {
      this.Result = result;
    }
  }

在 service.cs 中的使用

public class ExceptionService: ServiceBase, IExceptionService
{
   private readonly IDbContext _DbContext;
    public ExceptionService(ILogger<ExceptionService> logger, IDbContext DbContext, IMapper mapper)
    {
        _DbContext = DbContext;
    }

    public async Task<ServiceResponse<List<MyDto>>> GetLocation(
        string location, 
        CancellationToken cancellationToken,
        string invoiceType = "",
        long stockNumber = 0,
        string supplierName = "")
    {
        if (string.IsNullOrWhiteSpace(location))
            return new ServiceResponse<List<MyDto>>
            {
                StatusCode = ResultCode.ValidationFailed,
                Errors =
                {
                    new Error
                    {
                        Message = "Something went wrong"
                    }
                }
            };

        try
        {
            var myDtoList = new List<MyDto>();

            await foreach (var entity in _DbContext.MyRepository
                                .GetExceptionByLocation(location, cancellationToken, invoiceType, stockNumber,
                                    supplierName)
                                .WithCancellation(cancellationToken))
            {
                myDtoList.Add(_mapper.Map<MyDto>(entity));
            }

            return new ServiceResponse<List<MyDto>>
            {
                Result = myDtoList,
                StatusCode = myDtoList.Any() ? ResultCode.Success : ResultCode.NotFound,
            };
        }
        catch (Exception exception)
        {
            _logger.Error($"[{nameof(GetLocation)}]: Error occurred - {{@exception}}", args: new object[] { exception.GetBaseException().Message});
            return GetExceptionServiceResponse<List<MyDto>>(exception.GetBaseException().GetType(), ResultCode.Exception, exception.GetBaseException().Message);
        }
    }
}

ServiceBase.cs

  public class ServiceBase
    {
        protected ServiceResponse<T> GetExceptionServiceResponse<T>(Type type, ResultCode statusCode = ResultCode.Exception, string message = "", string reason = "")
        {
            return new ServiceResponse<T>
            {
                StatusCode = statusCode,
                Errors =
                {
                    new Error
                    {
                        Message = string.IsNullOrWhiteSpace(message) ? "Error Processing Request" : message.Trim(),
                        Reason = string.IsNullOrWhiteSpace(reason) ? ResponseMessages.ExceptionOccurred : reason.Trim(),
                        Type = type.ToString()
                    }
                }
            };
        }
    }

Query.cs

public class ExceptionsQuery
{
    [Authorize(policy: "Policy")]
    [UsePaging] // If I remove this, not seeing this error.
    public async Task<ServiceResponse<List<MyDto>>> GetExceptionsAsync(
        string location,
        [Service(ServiceKind.Synchronized)] IMyService myService,
        CancellationToken cancellationToken,
        string invoiceType = "",
        long stockNumber = 0,
        string supplierName = "")
        => await myService.GetExceptionsByLocation(location, cancellationToken, invoiceType, stockNumber,
            supplierName);
}
c# hotchocolate
© www.soinside.com 2019 - 2024. All rights reserved.