I'm new to using GraphQL/Hot Chocolate and I've been struggling with something that I hope someone here can help me with. I'm going with the code-first approach using F#. I have a type called Employee that has a one-to-many relationship with another type called PersonalValue. I've got everything set up in EF Core to bring back the list of of PersonalValues. I'm also wanting to map a property to the Employee type that is the current value (the current active Personal Value is deteremined by EndDate = NULL in the database). How would I go about adding this? I couldnt find a way to make this mapping using EF Core so I tried a couple of different options.
I tried adding a read-only property called PersonalValue which just filtered out the PersonalValues to a single record. This did work however when I try to do any filtering on it, I get a LINQ error.
member this.PersonalValue with get() = this.PersonalValues.Where(fun x -> not(x.EndDate.HasValue)).FirstOrDefault()
Gives this nasty error
The LINQ expression 'DbSet<Employee>()\r\n .Where(e => e.PersonalValue.LastName != null && e.PersonalValue.LastName.Contains(__p_0) && e.EmployeeID > __p_1)' could not be translated. Additional information: Translation of member 'PersonalValue' on entity type 'Employee' failed. This commonly occurs when the specified member is unmapped.\r\nTranslation of member 'PersonalValue' on entity type 'Employee' failed. This commonly occurs when the specified member is unmapped. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync
I also tried adding a function to my Employee type to resolve a single PersonalValue
member this.GetPersonalValue(): PersonalValue =
this.PersonalValues
.Where(fun x -> not(x.EndDate.HasValue))
.FirstOrDefault()
This also works for a basic query, but now the schema doesnt show a filter for PersonalValue anymore.
I would really appreciate some help. The TLDR is I'm trying to figure out how can I add a property to a type that is resolved from another property and is filterable.
Thanks in advance!
Edit: I also wanted to share my query as an example
query {
employees(
first: 50
where: {
personalValue: {
lastName: {contains: "John"}
}
}
) {
nodes {
employeeID
personalValue {employeeID lastName firstName}
}
}
}