首页 > 解决方案 > 类型:字节不能被有效地强制转换为 GraphQL 类型

问题描述

这是我用来获取 ByteArrayContent 类型的代码:

public class ImageDTO : APIResult
{
    public byte[] ImageData
    {
        get;
        set;
    }

    public string ImageFormat
    {
        get;
        set;
    }

    public ImageDTO()
    {
        ImageData = new byte[]{};
    }
}

public class ImageType : ObjectGraphType<ImageDTO>, IGraphQLType
{
    public ImageType()
    {
        Field(img => img.ImageData);
        Field(img => img.ImageFormat);
    }
}

public class ImageResolver : Resolver, IImageResolver
{
    private readonly ICommonService _commonService;
    public ImageResolver(ICommonService commonService)
    {
        _commonService = commonService;
    }

    public void Resolve(GraphQLQuery graphQLQuery)
    {
        graphQLQuery.Field<ResponseGraphType<ImageType>>("imageresponse", arguments: new QueryArguments(new QueryArgument<NonNullGraphType<StringGraphType>>{Name = "imageId", Description = "id of the project"}), resolve: context =>
        {
            var imageId = context.GetArgument<string>("imageId");
            var imageResult = _commonService.GetImage(imageId);
            if (imageResult == null)
            {
                return NotFoundError(imageId);
            }

            return Response(imageResult);
        }

        );
    }
}

在执行上面的代码时,我得到了下面提到的错误。类型: Byte 无法有效地强制转换为 GraphQL 类型\r\n参数名称: type\r\n at Type GraphQL。

从链接: https ://github.com/graphql-dotnet/graphql-dotnet/issues/458 ,提到支持ByteGraphType。我正在使用带有 version:2.3.0 的 GraphQL.net nuget 包。所以我相信它应该支持ByteGraphType。

你能帮我看看如何解决这个问题。

标签: c#asp.net-core-2.0graphql-dotnet

解决方案


而不是这一行=>Field(img => img.ImageData);

你可以用这个=>Field(img => img.ImageData, type: typeof(IdGraphType));


推荐阅读