How to avoid a nested query? - scala

In Sangria I'm getting a list of users by IDs
lazy val UsersType: ObjectType[GraphQLContext, Seq[User]] = ObjectType("users", () =>
fields[GraphQLContext, Seq[User]](
Field("users", ListType(UserDetailType), resolve =
_.value),
))
The Query works, but I have to write the name of the Query and the name of the Field "users" twice, they are the same.
The Query now is:
users(id: 123) {
users {
id
name
}
}
How to avoid the need to type the query name and the field name? How to turn the query to:
users(id: 123) {
id
name
}

Just use ListType(UsersType) in schema

Related

Foreign Key Object Missing in LINQ GroupBy?

I am using GroupBy in my LINQ queries. My query is working fine, except my foreign key objects are missing. Then, I tried to add Include in my query. Following is my code:
public ActionResult GetEmployees()
{
var Emloyees = db.Employees.AsQueryable();
Emloyees.GroupBy(e=> new {e.JobTitleId, e.GenderId})
.Select(tr => new MyObject
SalaryTotal = tr.Sum(r=>r.Salary)
}).Include(tr=>tr.JobTitle).Include(tr=>tr.Gender).ToList();
}
I am getting this exception:
The result type of the query is neither an EntityType nor a CollectionType with an entity element type. An Include path can only be specified for a query with one of these result types.
I tried to add it before GroupBy and directly in db.Employees.AsQueryable(), but nothing worked. What am I doing wrong?
The problem is that you are doing a projection with the .Select(...), after which your includes cannot be resolved. The result of that query will be a list MyObject, which does not work for includes that are actually on Employee.
So Try it like this:
Emloyees
.GroupBy(e=> new {e.JobTitleId, e.GenderId})
.Select(tr => new MyObject {
SalaryTotal = tr.Sum(r=>r.Salary),
JobTitle = tr.FirstOrDefault().JobTitle,
Gender = tr.FirstOrDefault().Gender
}).ToList();
You will have to extend MyObject with 2 additional properties, but the result of your query will be what you want (I assume).

Linq lambda query to select child and specify parent data

I have two tables, User and Group. I need to select user table and name of the group through lambda expression, and the return type of the method is User table
Below is my schema structure for user
Below is my schema structure for Group table
Try something like;
var query = yourContext.Users.Select(user => new
{
User = user,
GroupName = user.Group.Name
});

f# Insert on MongoDB using Records

I've been trying for a while to insert on MongoDB using only records with no success.
My problem is that I want to create a simple insert function which I send a generic type and it is inserted into the database.
Like so.
let insert (value: 'a) =
let collection = MongoClient().GetDatabase("db").GetCollection<'a> "col"
collection.InsertOne value
From this function, I tried inserting the following records.
// Error that it can't set the Id
type t1 = {
Id: ObjectId
Text: string
}
// Creates the record perfectly but doesn't generate a new Id
type t2 = {
Id: string
Text: string
}
// Creates the record and autogenerates the Id but doesn't insert the Text, and there are two Ids (_id, Id#)
type t3 = {
mutable Id: ObjectId
Text: string
}
// Creates the record and autogenerates the Id but for every property it generates two on MongoDB (_id, Id#, Text, Text#)
type t4 = {
mutable Id: ObjectId
mutable Text: string
}
So does anyone can think of a solution for this or am I stuck having to use a class.
// Works!!!
type t5() =
member val Id = ObjectId.Empty with get, set
member val Name = "" with get, set
Also, does anyone has any Idea of why when the C# MongoDB library translates the mutable he gets the property with the # at the end?
I would be fine with having all my properties set as mutable, although this wouldn't be my first choice, having he create multiple properties on the DB is quite bad.
You could try annotating your records with CLIMutable (and no mutable fields).
The #s end up in the DB because MongoDB using reflection and F# implementing mutable with backing fields fieldName#

Not able to use IN query in LINQ with Entity Framework

I am using EF Framework to retrieve the data from SQL DB.
Sub Request Table looks like below:
In this table "org_assigneddept" is foreign key to another Department Table.
I have list of Departments as Input and I want to retrieve only those rows from DB whose org_assigneddept is matching the list.
Please find my whole code:-
private List<EventRequestDetailsViewModel> GetSummaryAssignedDeptEventRequests(List<EmpRoleDeptViewModel> vmDept)
{
List<EventRequestDetailsViewModel> vmEventRequestDeptSummary = new List<EventRequestDetailsViewModel>();
RequestBLL getRequestBLL = new RequestBLL();
Guid subRequestStatusId = getRequestBLL.GetRequestStatusId("Open");
using (var ctxGetEventRequestSumm = new STREAM_EMPLOYEEDBEntities())
{
vmEventRequestDeptSummary = (from ers in ctxGetEventRequestSumm.SubRequests
where vmDept.Any(dep=>dep.DeptId == ers.org_assigneddept)
select new EventRequestDetailsViewModel
{
SubRequestId = ers.org_subreqid
}).ToList();
}
}
It is giving the following error at the LINQ Query level:-
System.NotSupportedException: 'Unable to create a constant value of
type 'Application.Business.DLL.EmpRoleDeptViewModel'. Only primitive
types or enumeration types are supported in this context.'
Please let me know as how can I achieve the result
You cannot pass the department VMs to SQL, it doesn't know what those are.
// Extract the IDs from the view models.. Now a list of primitive types..
var departmentIds = vmDept.Select(x => x.DeptId).ToList();
then in your select statement...
..
where departmentIds.Contains(id=> id == ers.org_assigneddept)
..

How to query cloudant db when there is an array in the json document

I have some json docs in cloudant DB like the following records.
{"#dataType":"GroupItem", "name":"aGroupName1", "GroupType":0},
{"#dataType":"GroupItem", "name":"aGroupName2", "GroupType":1, "Users":[{"deviceUUID":"id1", "userName":"user1"},{"deviceUUID":"id2", "userName":"user2"}] }
They are for groups and the users that belong to the group.
I want to do a query to return the groups the login user belongs to.
So when "user1" login, I'd like to see both "aGroupName1" and "aGroupName2" being returned.
The condition is like:
Select group when GroupType == 0 or (GroupType == 1 and userName == loginUser)
Because the userName is in an array in the json, I don't know how to do the query in Cloudant. Appreciate your help!
Jen
You'll need a map function like the following that emits a compound key:
function (doc) {
if (doc["#dataType"] == "GroupItem") {
doc.Users.map(function(user) {
emit([user.userName, doc.name]);
});
}
}
This will let you query by userName and find all of the matching group name values. Add GroupType to that compound key if you also need to query by group type.