I am trying to implement GraphQL in web API. The method is working fine when return object type. But it is throwing an error while I am trying to return Object List.
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");
}
The GetList() methods is returning the list with object data of type MyData. But in the Insomnia client showing an error. I think something wrong with the below code
descriptor.Field(g => g.GetList())
.Type<MyDataDataObjectType>().Name("GetList");
I am sending the following in the POST call
query{
a:GetList
{
Code
}
}
Error:
{ "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 } } }
Thanks in advance for your help.
You should be seeing an exception in the server that would've told you what is wrong.
What I can see is that the type of the field you are specifying is wrong. When returning a List you need to use the ListType
:
descriptor.Field(g => g.GetList())
.Type<ListType<MyDataDataObjectType>>().Name("GetList");