1

Background:

Given the following two entities joint through one to one relationship:

public partial class Parent
{
    public long Id { get; set; }
    public string Email { get; set; }
    public virtual Details Details{ get; set; }
}

public partial class Details
{
    public long Id { get; set; }
    public long ParentId{ get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Dob { get; set; }
    public virtual Parent Parent { get; set;}
}

And having the following Query model:

public class Query
{
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime? Dob { get; set; }
}

Question

How can I apply the Query as IQueryable on the Parent (or Details) entity?

Notes based on the use case I have:

  • Query class can't have two sub-classes for Parent and Details (it should be flattened)
  • DB SQL query should fetch results that matches both conditions in Parent and Details (if condition fail for details, then parent shouldn't be in the results).
  • There might be long list of optional fields in Query model. It means that the DB query should be dynamic and smart enough to know how to build the query and to know each field in Query belongs to which entity Parent or Details (i.e. I don't want a solution where I add conditions to check whether Dob exist in the Query or not)

Use case:

I'm using HotChocolate framework to integrate GraphQL which uses expression trees to build the queries. The issue I'm trying to solve is mentioned here

Your support and suggestions would be highly appreciated!

Moamen Naanou
  • 1,683
  • 1
  • 21
  • 45

1 Answers1

0

I managed to handle this issue by creating a View on the DB which joins both tables to act as single entity where I can filter, paginate and sort regardless whether it is one to one linked tables or it is single table. Then did reverse engineering of that view using this link to be integrated with EntityFramework and finally managed to handle IQueryable on both entities.

In case of any other option available and suitable for HotChocolate, please add another answer to enhance my existing solution.

Moamen Naanou
  • 1,683
  • 1
  • 21
  • 45