Skip to content

Parent Require with Collections #8234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pm-hdi opened this issue Apr 8, 2025 · 3 comments
Closed

Parent Require with Collections #8234

pm-hdi opened this issue Apr 8, 2025 · 3 comments

Comments

@pm-hdi
Copy link

pm-hdi commented Apr 8, 2025

Product

Hot Chocolate

Is your feature request related to a problem?

It appears to me that parent requirements do not work for collections, when requiring a sub field that happens to be part of an item within a collection:

public class Order
{
   public int OrderId { get; set; }
   public ICollection<Item> Items { get; set; }
}

public class Item
{
   public int ItemId { get; set; }
   public decimal Price { get; set; }
}

[BindMember("totalPrice")]
[UseProjection]
public static async Task<decimal?> GetTotalPrice([Parent("Items { Price }")] Order order)
{
   return order.Items.Sum(i => i.Price);
}

This results in an error message that the field requirement does not exist - raised in the PropertyTreeBuilder CollectProperties method:
The field requirement /Items/Price does not exist on Order.TotalPrice.

When the method traverses the defined require query of the Parent attribute, it uses the GetProperty method of .NET reflection to collect each class property for each selected field (CollectProperties method). This works fine as long as there is no collection involved. In my sample the first property node for Items get resolved successfully, but when resolving the "Price" field the GetProperty method is called on the type of ICollection.

Is this not yet supported or am I missing GraphQL specific issues why this is not implemented?

The solution you'd like

I can require Collections too and use that for projections.

@michaelstaib
Copy link
Member

Correct ... precisely because it would be very inefficient to do things like

[BindMember("totalPrice")]
[UseProjection]
public static async Task<decimal?> GetTotalPrice([Parent("Items { Price }")] Order order)
{
   return order.Items.Sum(i => i.Price);
}

Aggregations like that are better done differently in your business model.

We have done a couple of tutorials that explain design decisions behind the new data APIs.

https://www.youtube.com/@chillicreamtv

@glen-84
Copy link
Collaborator

glen-84 commented Apr 8, 2025

See also #8179 (comment).

@pm-hdi
Copy link
Author

pm-hdi commented Apr 9, 2025

Resolved the issue above by using projection via QueryContext as shown in the latest tutorial video:
https://www.youtube.com/watch?v=dYSqssul4jY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants