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

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


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 🙂

2

  • 1

    I think you are looking for projections: chillicream.com/docs/hotchocolate/fetching-data/projections

    – Tobias Tengler

    Dec 16, 2021 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, 2021 at 21:51

2 Answers
2


1

Tobias Tengler was correct this should be done through Projections

2

  • 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, 2021 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, 2021 at 19:18


0

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.

New contributor

Tarang Damania is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



Leave a Reply

Your email address will not be published. Required fields are marked *