GraphQL - 执行错误(无法将父项转换为...)
GraphQL - Execution Error (The parent cannot be cast to..)
我正在尝试在网络中实现 GraphQL API。当 return 对象类型时,该方法工作正常。但是当我尝试 return 对象列表时它抛出错误。
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; }
}
Query.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() 方法return使用 MyData 类型的对象数据对列表进行编辑。但是在 Insomnia 客户端显示错误。我认为下面的代码有问题
descriptor.Field(g => g.GetList())
.Type<MyDataDataObjectType>().Name("GetList");
我在 POST 通话中发送以下内容
query{
a:GetList
{
Code
}
}
错误:
{
"errors": [
{
"message": "Unexpected Execution Error",
"locations": [
{
"line": 4,
"column": 4
}
],
"path": [
"a",
"Code"
],
"extensions": {
"message": "The parent cannot be cast to MyData.",
"stackTrace": " at HotChocolate.Execution.Processing.MiddlewareContext.SourceT\r\n at HotChocolate.Execution.Processing.MiddlewareContext.ParentT\r\n at lambda_method65(Closure , IResolverContext )\r\n at HotChocolate.Types.FieldMiddlewareCompiler.<>c__DisplayClass3_0.<b__0>d.MoveNext()\r\n--- End of stack trace from previous location ---\r\n at HotChocolate.Execution.Processing.ResolverTask.ExecuteResolverPipelineAsync(CancellationToken cancellationToken)\r\n at HotChocolate.Execution.Processing.ResolverTask.TryExecuteAsync(CancellationToken cancellationToken)"
}
}
],
"data": {
"a": {
"Code": null
}
}
}
在此先感谢您的帮助。
您应该会在服务器中看到异常,它会告诉您出了什么问题。
我能看到的是您指定的字段类型是错误的。返回列表时,您需要使用 ListType
:
descriptor.Field(g => g.GetList())
.Type<ListType<MyDataDataObjectType>>().Name("GetList");
我正在尝试在网络中实现 GraphQL API。当 return 对象类型时,该方法工作正常。但是当我尝试 return 对象列表时它抛出错误。
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; }
}
Query.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() 方法return使用 MyData 类型的对象数据对列表进行编辑。但是在 Insomnia 客户端显示错误。我认为下面的代码有问题
descriptor.Field(g => g.GetList())
.Type<MyDataDataObjectType>().Name("GetList");
我在 POST 通话中发送以下内容
query{
a:GetList
{
Code
}
}
错误:
{ "errors": [ { "message": "Unexpected Execution Error", "locations": [ { "line": 4, "column": 4 } ], "path": [ "a", "Code" ], "extensions": { "message": "The parent cannot be cast to MyData.", "stackTrace": " at HotChocolate.Execution.Processing.MiddlewareContext.SourceT\r\n at HotChocolate.Execution.Processing.MiddlewareContext.ParentT\r\n at lambda_method65(Closure , IResolverContext )\r\n at HotChocolate.Types.FieldMiddlewareCompiler.<>c__DisplayClass3_0.<b__0>d.MoveNext()\r\n--- End of stack trace from previous location ---\r\n at HotChocolate.Execution.Processing.ResolverTask.ExecuteResolverPipelineAsync(CancellationToken cancellationToken)\r\n at HotChocolate.Execution.Processing.ResolverTask.TryExecuteAsync(CancellationToken cancellationToken)" } } ], "data": { "a": { "Code": null } } }
在此先感谢您的帮助。
您应该会在服务器中看到异常,它会告诉您出了什么问题。
我能看到的是您指定的字段类型是错误的。返回列表时,您需要使用 ListType
:
descriptor.Field(g => g.GetList())
.Type<ListType<MyDataDataObjectType>>().Name("GetList");