【问题标题】:GraphQL - Execution Error (The parent cannot be cast to..)GraphQL - 执行错误(无法将父级强制转换为..)
【发布时间】:2021-07-27 03:23:21
【问题描述】:

我正在尝试在 Web API 中实现 GraphQL。返回对象类型时该方法工作正常。但是当我试图返回对象列表时它会抛出一个错误。

    public class MyData
    {
       public int Code { get; set; }
       public string Name { get; set; }
       public string LastName { get; set; }
       public string City { get; set; }
       public string State { get; set; }
       public string Country { get; set; }
   }

查询.cs

     public List<MyData> GetList()
     {
         List<MyData> myList = null;

         string query = "select Code, Name, LastName,  City, State, Country from MyTable";

         using (var connection = new MySqlConnection(connectionString))
         {
              myList = connection.Query<MyData>(query).ToList();
          }
          return myList;
      }

    public class QueryObjectType : ObjectType<Query>
    {
         descriptor.Field(g => g.GetList())
        .Type<MyDataDataObjectType>().Name("GetList");
    }
    
    public class MyDataDataObjectType: ObjectType<MyData>
    {
        protected override void Configure(IObjectTypeDescriptor<MyData> descriptor)
        {
            descriptor.Field(g => g.Code).Type<IntType>().Name("Code");
            descriptor.Field(g => g.Name).Type<StringType>().Name("Name");
            descriptor.Field(g => g.LastName).Type<StringType>().Name("LastName");
            descriptor.Field(g => g.City).Type<StringType>().Name("City");
            descriptor.Field(g => g.State).Type<IntType>().Name("State");
            descriptor.Field(g => g.Country).Type<IntType>().Name("Country");
         }
    }

startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddGraphQLServer()
        .AddQueryType<QueryObjectType>();
    }
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
            app.UseGraphQLVoyager(new VoyagerOptions()
            {
                GraphQLEndPoint = "/graphql"
        }, "/graphql-ui");
    }

GetList() 方法返回带有 MyData 类型对象数据的列表。但在 Insomnia 客户端显示错误。我认为下面的代码有问题

         descriptor.Field(g => g.GetList())
        .Type<MyDataDataObjectType>().Name("GetList");

我在 POST 调用中发送以下内容

query{
      a:GetList
      {
         Code
       }
     }

错误:

{ “错误”:[ { "message": "意外执行错误", “地点”:[ { “行”:4, “列”:4 } ], “小路”: [ “一种”, “代码” ], “扩展”:{ "message": "父级不能转换为 MyData。", "stackTrace": " 在 HotChocolate.Execution.Processing.MiddlewareContext.SourceT\r\n 在 HotChocolate.Execution.Processing.MiddlewareContext.ParentT\r\n 在 lambda_method65(Closure , IResolverContext)\r\n 在 HotChocolate.Types.FieldMiddlewareCompiler .c__DisplayClass3_0.d.MoveNext()\r\n--- 从先前位置结束堆栈跟踪 ---\r\n 在 HotChocolate.Execution.Processing.ResolverTask.ExecuteResolverPipelineAsync(CancellationToken cancelToken)\r \n 在 HotChocolate.Execution.Processing.ResolverTask.TryExecuteAsync(CancellationToken cancelToken)" } } ], “数据”: { “一种”: { “代码”:空 } } }

提前感谢您的帮助。

【问题讨论】:

    标签: graphql dapper asp.net-core-5.0 hotchocolate


    【解决方案1】:

    您应该会在服务器中看到一个异常,该异常会告诉您出了什么问题。 我可以看到您指定的字段类型是错误的。返回列表时需要使用ListType:

    descriptor.Field(g => g.GetList())
            .Type<ListType<MyDataDataObjectType>>().Name("GetList");
    

    Learn more about this in the lists documentation

    【讨论】:

    • 谢谢,再次遇到同样的错误,我的实现有问题。让我了解文档
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    • 2013-04-08
    • 2021-10-25
    • 2012-09-04
    • 1970-01-01
    • 2018-09-05
    相关资源
    最近更新 更多