0

How do you load nested types? For example, I have fairly simple model that looks like:

public class Customer
{
    public string CustomerId { get; set; }
    
    public string Forename { get; set; }
    
    public string LastName { get; set; }
    
    public ICollection<Order> Orders { get; set; }
}

public class Order
{
    public string OrderId { get; set; }
    
    public string CustomerId { get; set; }
}

My query does the following (returns hardcoded data)

    [UseProjection]
    [UseFiltering]
    [UseSorting]
    public IQueryable<Customer> Customers()
    {
        var customers = new List<Customer>
        {
            new()
            {
                CustomerId = "1", Forename = "Foo", LastName = "Blah",
                Orders = _orderService.GetOrdersByCustomerId("")
            },
            new()
            {
                CustomerId = "2", Forename = "Bar", LastName = "BlahBlah",
                Orders = _orderService.GetOrdersByCustomerId("")
            }
        };
        
        return customers.AsQueryable();
    }

The _orderService its calling is returning hardcoded data:

    public ICollection<Order> GetOrdersByCustomerId(string customerId)
    {
        return new List<Order>
        {
            new() { OrderId = $"{customerId}-99", CustomerId = "1" },
            new() { OrderId = $"{customerId}-98", CustomerId = "2" }
        };
    }

My query works fine for the top level but doesn't seem to filter the nested types.

query {
  customers(where: { customerId: { eq: "1" } }) {
    forename
    lastName
    orders {
      orderId
    }
  }
}

I am clearly missing something or perhaps I misusing.

For my desired output I would need to change the query to be:

query {
  customers(where: { customerId: { eq: "1" } }) {
    forename
    lastName
    orders(where: { customerId: { eq: "1" } }) {
      orderId
      customerId
    }
  }
}
Dr Schizo
  • 4,045
  • 7
  • 38
  • 77

2 Answers2

1

In your case you do not need UseProjection since there is no database involved. Remove it and it should just work. UseProjection would rewrite the GraphQL query on top of the queryable to translate it to a native database query. If you put EF Core in for instance.

0

I think you looking nested object filtering. This can be done like this:

    public class CustomerType : ObjectType<Customer>
    {
        protected override void Configure(IObjectTypeDescriptor<Customer> descriptor)
        {
            descriptor.Field(f => f.Orders).UseFiltering();
        }
    }
group
  • 9
  • 2