ZackR
ZackR

Reputation: 73

How can you switch includes on and off in Hotchocolate for EFCore

So I'm trying to figure out how to tell EfCore when to apply an include to an IQueryable object based on whether the GraphQl request from the client actually includes the related object.

Example:

What I would like to do would be something like:

    var baseQuery = db.People;
        if (graphqlRequestIncludePayments)
        {
            baseQuery.Include(p => p.Payments);
        }
    return baseQuery;

But I cannot figure out how to check the GraphQl query to see if payments is requested. I know GraphQl will remove the excess data before returning to the consumer but that can be a lot of wasted bandwidth and memory on the server side.

I feel like there is a way to do this in the Hotchocolate ObjectType.Configure function but I cant see how.

Any help would be much appreciated, thanks :)

Upvotes: 0

Views: 1655

Answers (1)

Tarang Damania
Tarang Damania

Reputation: 1

I was facing the same issue. there is a very small catch when using projections that I figured out after reading this page multiple times https://chillicream.com/docs/hotchocolate/v13/fetching-data/projections...

The catch is the note that we generally tend to skip. the note goes as "Note: Projections currently need a public setter on fields they operate on in order to function correctly. Otherwise the default constructed value will be returned upon query."

I checked my model and could see that my model was only having a public getter and not a setter and so I was getting the default value that was empty list. After adding a public setter, it worked perfectly.

Hope this helps.

Upvotes: 0

Related Questions