So transformieren Sie das Ergebnis, das nach der Paginierung festgelegt wirdC#

Ein Treffpunkt für C#-Programmierer
Guest
 So transformieren Sie das Ergebnis, das nach der Paginierung festgelegt wird

Post by Guest »

Ich möchte ein benutzerdefiniertes Modell zurückgeben, nicht einen Sammelsegmenttyp. Dies ist mein Resolver, um Sortieren, Filtern, Pagination und Projektion anzuwenden. < /P>

Code: Select all

public class CompanyResolver
{

[UseOffsetPaging(IncludeTotalCount = true)]
[UseProjection]
[UseFiltering]
[UseSorting]
public IQueryable 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);
}
}
< /code>
Ich möchte das Ergebnis der Middleware mit diesem Ansatz < /p>
transformieren.public class CompanyObjectType : ObjectType
{
protected override void Configure(IObjectTypeDescriptor descriptor)
{
descriptor.Field("childCompanies")
.Type()
.Use(next => async context =>
{
await next(context);
if (context.Result is CollectionSegment allApplied)
{
var service = context.Services.GetRequiredService();
var sourceData = allApplied.Items.ToList();
var mappedData = sourceData.Any() ? service.mapper.Map(sourceData) : null;
context.Result = mappedData;
}
})
.ResolveWith(x => x.GetChildCompanies(default!, default!));
}
}
< /code>
Wie im Code -Block zu sehen, möchte ich eine Sammlung von ChildCompany zurückgeben. Ausführung der folgenden Anfrage in Bananenkuchen -Pop -Konsole < /p>
jedochchildCompanies(skip: 0, take: 5) {
totalCount
items {
organizationTypeId
relationshipTypeId
statusComment
statusDate
statusTypeId
}
}
< /code>
Gibt die folgende Fehlermeldung zurück, obwohl sie Filterung, Sortierung usw. anwendet. < /p>
{
"message": "The resolver parent type of field `ChildCompaniesCollectionSegment.items` is `System.Collections.Generic.List`1[Eastdil.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"
}
}
< /code>
Ich habe es versucht, ohne eine Middleware wie diese zu verwenden, aber die Abfrage filtert noch nicht.  Bananenkuchen Pop zeigte keine Argumente. < /P>
descriptor.Field("childCompanies")
.Type()
.Resolve(async context =>
{
var allApplied = await query.Sort(context)
.Filter(context)
.Project(context)
.ApplyOffsetPaginationAsync(context);
...
...
});
Bitte lassen Sie mich wissen, was mir hier fehlt

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post