© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
3 replies
Anton

❔ HotChocolate with IQueryable, apply a required filter on the ef entity

Basically, I'm trying to find a way to configure an
IObjectFieldDescriptor
IObjectFieldDescriptor
by adding a required argument to it, which should in turn apply a filter on the database entity. In this case, I'm adding a
companyId
companyId
argument, and then applying the respective filter manually, before mapping. What should I do to configure this in a simpler way? Let me show you what I have and then what I mean by a better way.

This is what I have so far. Don't mind the
FieldCollectionQuery
FieldCollectionQuery
and
UseBuiltinMiddleware
UseBuiltinMiddleware
extension methods, they are there to set up the ef core stuff. The argument is the important bit right now.
public sealed class QueryType : ObjectType
{
    protected override void Configure(IObjectTypeDescriptor descriptor)
    {
        descriptor
            .FieldCollectionQuery<SupplierGraphDtoType>("suppliers")
            .Argument("companyId", a => a.Type<NonNullType<IntType>>())
            .UseBuiltinMiddleware<SupplierGraphDtoType>(MiddlewareFlags.All)
            .Resolve(ctx =>
            {
                var q = ctx.DbContext<DataContext>()
                    .Set<Supplier>()
                    .AsQueryable();

                int companyId = ctx.ArgumentValue<int>("companyId");
                q = q.Where(s => s.CompanyId == companyId);

                return q.ProjectTo<Supplier, SupplierGraphDto>(ctx);
            });
    }
}
public sealed class QueryType : ObjectType
{
    protected override void Configure(IObjectTypeDescriptor descriptor)
    {
        descriptor
            .FieldCollectionQuery<SupplierGraphDtoType>("suppliers")
            .Argument("companyId", a => a.Type<NonNullType<IntType>>())
            .UseBuiltinMiddleware<SupplierGraphDtoType>(MiddlewareFlags.All)
            .Resolve(ctx =>
            {
                var q = ctx.DbContext<DataContext>()
                    .Set<Supplier>()
                    .AsQueryable();

                int companyId = ctx.ArgumentValue<int>("companyId");
                q = q.Where(s => s.CompanyId == companyId);

                return q.ProjectTo<Supplier, SupplierGraphDto>(ctx);
            });
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

EF Entity 'required' property
C#CC# / help
2y ago
Entity Framework IQueryable question
C#CC# / help
2y ago
✅ LINQ help: IQueryable<Entity> -> Dictionary<string, Entity[]>
C#CC# / help
12mo ago
EF 6 - Creating entity with n-n
C#CC# / help
4y ago