Reputation: 49
I want to return a custom model, not a CollectionSegment type. This is my resolver to apply sorting, filtering, pagination and projection.
public class CompanyResolver
{
[UseOffsetPaging(IncludeTotalCount = true)]
[UseProjection]
[UseFiltering]
[UseSorting]
public IQueryable<CommandCompany> GetChildCompanies(CompanyDbContext dbContext, [Parent] Company company)
{
return dbContext.Companies
.Include(x => x.Sol)
.Include(x => x.Comp)
.Include(x => x.Ris)
.Where(x => x.ParentId == company.CompanyId);
}
}
I want to transform the result set in the middleware by using this approach
public class CompanyObjectType : ObjectType<Company>
{
protected override void Configure(IObjectTypeDescriptor<Company> descriptor)
{
descriptor.Field("childCompanies")
.Type<ListType<ObjectType<ChildCompany>>>()
.Use(next => async context =>
{
await next(context);
if (context.Result is CollectionSegment<CommandCompany> allApplied)
{
var service = context.Services.GetRequiredService<CompanyQueryService>();
var sourceData = allApplied.Items.ToList();
var mappedData = sourceData.Any() ? service.mapper.Map<List<ChildCompany>>(sourceData) : null;
context.Result = mappedData;
}
})
.ResolveWith<CompanyResolver>(x => x.GetChildCompanies(default!, default!));
}
}
As seen in the code block, I want to return a collection of ChildCompany; however, executing the request below in Banana Cake Pop console
childCompanies(skip: 0, take: 5) {
totalCount
items {
organizationTypeId
relationshipTypeId
statusComment
statusDate
statusTypeId
}
}
returns the following error message although it applies filtering, sorting etc..
{
"message": "The resolver parent type of field `ChildCompaniesCollectionSegment.items` is `System.Collections.Generic.List`1[Companies.GraphQL.Types.ChildCompany]` but the resolver requested the type `HotChocolate.Types.Pagination.CollectionSegment`. The resolver was unable to cast the parent type to the requested type.",
"path": [
"company",
"childCompanies",
"items"
],
"extensions": {
"field": "ChildCompaniesCollectionSegment.items",
"code": "HC0053"
}
}
I tried without using a middleware like this, but it doesn't filter or paginate the query. Banana Cake Pop didn't show any arguments.
descriptor.Field("childCompanies")
.Type<ListType<ObjectType<ChildCompany>>>()
.Resolve(async context =>
{
var allApplied = await query.Sort(context)
.Filter(context)
.Project(context)
.ApplyOffsetPaginationAsync(context);
...
...
});
Please let me know what I'm missing here
Upvotes: 0
Views: 32