0

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:

  • 2 Classes: Person, Payment

  • Theses are kept in 2 different tables and related Person -> Payment in a one to many relationship

  • I have 1 GraphQl Query set up to retrieve all people in the Database:

     [UseFiltering]
     [UseDbContext(typeof(DbAccess))]
     public IQueryable<Person> GetPeople([ScopedService] DbAccess db)
     {
         return db.People;
     }
    
  • This returns just the fields in the People table ex: query: {people{sso,payments{amount, recipient{compayName}}}} response: { "data": { "people": [ { "sso": "000-00-0003", "payments": null } ] } }

  • In order to get the payments my GetPeople func needs to be changed to

     [UseFiltering]
     [UseDbContext(typeof(DbAccess))]
     public IQueryable<Person> GetPeople([ScopedService] DbAccess db)
     {
         return db.People.Include(p => p.Payments);
     }
    
  • This works but places an unneeded strain when the payments aren't part of the request (as the Include will run every time).

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 :)

ZackR
  • 61
  • 1
  • 7
  • 1
    I think you are looking for projections: https://chillicream.com/docs/hotchocolate/fetching-data/projections – Tobias Tengler Dec 16 '21 at 18:34
  • I think you are completely correct Tobias although it does not seem to be working for me. I tried adding projections and extra fields are still getting pulled and the related fields aren't getting pulled. Based on the documentation through it does look like Projections should solve the problem :/ – ZackR Dec 17 '21 at 21:51

1 Answers1

1

Tobias Tengler was correct this should be done through Projections

ZackR
  • 61
  • 1
  • 7
  • That's no answer. What was the actual solution? What did you do? If it took you a week to solve the problem clearly it's not something that can be solved simply by looking at the docs – Panagiotis Kanavos Dec 23 '21 at 15:21
  • It does answer the problem. It took me a week to share the final answer because I had 1 small issue (ordering of attributes) in projections that was causing my code to fail for a while. But that is unrelated to this question. I also wasn't just working on this problem so it took a bit to finish it to be sure that projections were the correct answer to what I was trying to do. – ZackR Dec 30 '21 at 19:18