I need return the user_roles in graphql query - graphql

i need return my user with your role, i'm using prisma.io and nestjs + graphql;
this is my return in graphl
{
"data": {
"login": {
"user": {
"secure_id": "1086dd35-d9ab-442a-8311-af4720ed3d9a",
"name": "Super Admin",
"email": "super#admin.com"
},
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InN1cGVyQGFkbWluLmNvbSIsInN1YiI6IjEwODZkZDM1LWQ5YWItNDQyYS04MzExLWFmNDcyMGVkM2Q5YSIsImlhdCI6MTY0OTY4NDg5NSwiZXhwIjoxNjQ5Njg4NDk1fQ.bwQRzcQ_gVR2wlVGPpKdSH1yZ6e6ipmENB3NqhKeN-8"
}
}
}
and this is the console.log of the user at moment of signin:
{
id: 5,
secure_id: '1086dd35-d9ab-442a-8311-af4720ed3d9a',
name: 'Super Admin',
email: 'super#admin.com',
password: '$2b$10$ioWYGeN.luOAzB9KdUjsp.qYPkgoqLtEqqcoN05dljgXQXPkX2N6W',
created_at: 2022-04-07T19:37:22.081Z,
updated_at: 2022-04-07T19:37:22.082Z,
UserRoles: [
{
user_id: 5,
roles_id: 2,
created_at: 2022-04-07T19:37:22.096Z,
updated_at: 2022-04-07T19:37:22.096Z
}
]
}
in the log i receive the UserRoles, but not in the signin.
this is the repository: https://github.com/paulozy/tgl-nest-and-graphql

Related

Combining two observable sources filtering by one first observable property

Having an observable emitting a list of users with the next content:
[
{
"id": 1,
"name": "John",
"status": "Active"
},
{
"id": 2,
"name": "Mary",
"status": "Inactive"
},
{
"id": 3,
"name": "Peter",
"status": "Inactive"
},
{
"id": 4,
"name": "Susan",
"status": "Active"
}
]
And I have another observable returning the extended user data:
{
"id": 1,
"authorizations: 20
}
I use the detail of each user in an specific details page, but I would like to combine part of the detail in the users list and obtain the next result and only filter by the status Active:
[
{
"id": 1,
"name": "John",
"status": "Active",
"authorizations": 20
},
{
"id": 4,
"name": "Susan",
"status": "Active",
"authorizations": 10
}
]
It is possible to use some filtering operator and combine those results without use two subscriptions?
Tried the following code but, would be a better or simplified way to do it?
import { of, Observable, combineLatest } from 'rxjs';
import { filter, map, mergeAll, mergeMap } from 'rxjs/operators';
type State = 'Active' | 'Inactive';
type User = { id: number; name: string; status: State };
type UserDetail = { id: number; authorizations: number };
type UserWithAuthorizations = User & UserDetail
const users: User[] = [
{
"id": 1,
"name": "John",
"status": "Active"
},
{
"id": 2,
"name": "Mary",
"status": "Inactive"
},
{
"id": 3,
"name": "Peter",
"status": "Inactive"
},
{
"id": 4,
"name": "Susan",
"status": "Active"
}
]
const authorizations: UserDetail[] = [
{ id: 1, authorizations: 20 },
{ id: 2, authorizations: 5 },
{ id: 3, authorizations: 30 },
{ id: 4, authorizations: 10 },
];
const getAuthorizationsByUser= (userId: number): Observable<Partial<UserWithAuthorizations>> => {
const users$ = of(users)
const authorizations$ = of(authorizations)
return combineLatest([users$, authorizations$]).pipe(
map(res => {
const user = res[0].find(u => u.id === userId)
const { authorizations } = res[1].find(a => a.id === userId)
return {
...user,
authorizations
}
}))
};
const fetchUsersWithAuthorizations = () => of(users);
fetchUsersWithAuthorizations()
.pipe(
mergeAll<User>(),
filter((user) => user.status === "Active"),
mergeMap((user) => getAuthorizationsByUser(user.id))
)
.subscribe(console.log);
Why not do it all in a single combine latest?
const { of, map, combineLatest } = rxjs;
const users = [
{
"id": 1,
"name": "John",
"status": "Active"
},
{
"id": 2,
"name": "Mary",
"status": "Inactive"
},
{
"id": 3,
"name": "Peter",
"status": "Inactive"
},
{
"id": 4,
"name": "Susan",
"status": "Active"
}
]
const authorizations = [
{ id: 1, authorizations: 20 },
{ id: 2, authorizations: 5 },
{ id: 3, authorizations: 30 },
{ id: 4, authorizations: 10 },
];
const users$ = of(users)
const authorizations$ = of(authorizations)
const activeUsersWithAuthorizations$ = combineLatest([users$, authorizations$]).pipe(
map(([users, authorizations]) =>
users
.filter((user) => user.status === 'Active')
.map((user) => ({
...user,
authorizations: authorizations.find((a) => a.id === user.id)?.authorizations,
}))
)
);
activeUsersWithAuthorizations$.subscribe(activeUsersWithAuthorizations => {
console.log(activeUsersWithAuthorizations);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/7.8.0/rxjs.umd.min.js" integrity="sha512-v0/YVjBcbjLN6scjmmJN+h86koeB7JhY4/2YeyA5l+rTdtKLv0VbDBNJ32rxJpsaW1QGMd1Z16lsLOSGI38Rbg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

how to get count of MongoDB documents in a given date range

The below mentioned is the Collection used:
[
{
_id: 1,
joinedAt:ISODate("2021-07-20T12:31:33.229+05:30"),
firstName: "John",
lastName: "King",
salary: 5000,
department: {
"name": "HR"
}
},
{
_id: 2,
joinedAt:ISODate("2021-08-20T12:31:33.229+05:30"),
firstName: "Sachin",
lastName: "T",
salary: 8000,
department: {
"name": "Marketing"
}
},
{
_id: 3,
joinedAt:ISODate("2021-06-20T12:31:33.229+05:30"),
firstName: "James",
lastName: "Bond",
salary: 7500,
department: {
"name": "Marketing"
}
},
{
_id: 4,
joinedAt:ISODate("2021-05-20T12:31:33.229+05:30"),
firstName: "Rosy",
lastName: "Brown",
salary: 5000,
department: {
"name": "HR"
}
},
{
_id: 5,
joinedAt:ISODate("2021-07-26T12:31:33.229+05:30"),
firstName: "Kapil",
lastName: "D",
salary: 4500,
department: {
"name": "HR"
}
},
{
_id: 6,
joinedAt:ISODate("2021-07-20T12:31:33.229+05:30"),
firstName: "Amitabh",
lastName: "B",
salary: 7000,
department: {
"name": "Marketing"
}
}
]
I used the following query:
db.collections.aggregate([
{
$match:
{ createdAt:
{
$gte: ISODate("2021-01-01T17:06:02.713+05:30"),
$lte: ISODate("2021-12-31T17:06:02.713+05:30")
}
}
},
{$sort: {createdAt: 1}},
{ $group:{ _id:{department:'$department.name'}, totalEmployees:
{$sum:1},firstEmployee: {$first: "$firstName"} }
}])
Using this query I can get the count of employees in each department and the first name of the employee who joined first in each department. I would also like to get the count of employees joined in each department in a particular month(eg: employees joined HR department in September). What are the changes to be made in this query?

GraphQL + Sequalize + existing database - "message": "parent.getPages is not a function" for one model not the other

GraphQL Query
Morning all,
I have a nice setup GraphQL -> Sequalize -> Existing DB (generated by a Laravel application). I've built this schema out:
type App {
id: ID!
userId: ID
user: User
pages: [Page]
experiments: [Experiment]
uri: String!
createdAt: String!
updatedAt: String!
}
type Page {
id: ID!
userId: ID
user: User
appId: ID!
app: App!
uri: String!
createdAt: String!
updatedAt: String!
}
type Experiment {
id: ID!
title: String!
appId: ID!
app: App!
createdAt: String!
updatedAt: String!
}
Based on the existing data. Querying an apps experiments works just great:
query {
app(id: 6) {
id
title
experiments {
id
}
}
}
{
"data": {
"app": {
"id": "6",
"title": "C-Map Embark: Boating",
"experiments": [
{
"id": "1"
}
]
}
}
}
But querying pages I get this:
query {
app(id: 6) {
id
title
pages {
id
}
}
}
{
"errors": [
{
"message": "parent.getPages is not a function",
"locations": [
{
"line": 5,
"column": 5
}
],
"path": [
"app",
"pages"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"TypeError: parent.getPages is not a function",
...
The db columns are the same, as are the resolvers:
/* jshint indent: 2 */
module.exports = function(sequelize, DataTypes) {
const Page = sequelize.define(
"page",
{
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
...
createdAt: {
type: DataTypes.DATE,
allowNull: true
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true
}
},
{
tableName: "pages",
underscored: true
}
);
Page.associate = models => {
Page.belongsTo(models.app);
};
return Page;
};
/* jshint indent: 2 */
module.exports = function(sequelize, DataTypes) {
const Experiment = sequelize.define(
"experiment",
{
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
...
createdAt: {
type: DataTypes.DATE,
allowNull: true
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true
}
},
{
tableName: "experiments",
underscored: true
}
);
Experiment.associate = models => {
Experiment.belongsTo(models.app);
};
return Experiment;
};
Have you come across this before?

Subobject is not parsed graphql with golang

The object itself inside the object is parsed with an empty field.
input:
{
Products(search: {limit: 1, filters: {product_id: {gte: 5}}}) {
data {
product_id
product_name
sales_history{
total_check
}
}
}
}
output:
{
"data": {
"Products": {
"data": [
{
"product_id": 35,
"product_name": "testpr",
"sales_history": {}
}
]
}
}
}
Product type:
gql.ProductType = graphql.NewObject(graphql.ObjectConfig{
Name: "Product",
Fields: graphql.Fields{
"product_id": &graphql.Field{
Type: graphql.Int,
},
"product_name": &graphql.Field{
Type: graphql.String,
},
"sales_history": &graphql.Field{
Type: gql.SalesHistoryType,
},
},
})
SalesHistory type:
gql.SalesHistoryType = graphql.NewObject(graphql.ObjectConfig{
Name: "sales_history",
Fields: graphql.Fields{
"total_check": &graphql.Field{
Type: graphql.Float,
},
},
})
In Resolve returning map in interface:
map[data:[map[product_id:35 product_name:testpr sales_history:map[total_check:671.20]]]]
I create the map"sales_history" myself, otherwise opposite the field sales_history - null
The problem was in the packaging of the final map.
It was wrong:
tmp := make(map[string]interface{}, 0)
tmp["total_check"] = v["total_check"]
v["sales_history"] = tmp
*Some fields are hidden
That`s right:
v["sales_history"] = make(map[string]interface{}, 0)
v["sales_history"].(map[string]interface{})["total_check"] = v["total_check"]

Filtering and find unique value in Kibana(Elasticsearch)

I use kibana.
I want to filter some value and find unique count.
ex)
{ user_id: 1, type: "customer", action: "xxx" }
{ user_id: 1, type: "customer", action: "xxx" }
{ user_id: 1, type: "seller", action: "xxx" } # user change type!!!
{ user_id: 2, type: "customer", action: "xxx" }
{ user_id: 2, type: "customer", action: "xxx" }
{ user_id: 3, type: "customer", action: "xxx" }
How can I count just customers.
I want to get
customer_count: 2
for example, in SQL:
SELECT COUNT(DISTINCT(user_id)) FROM users where user_id NOT IN (SELECT user_id FROM users WHERE type = "seller")
Is there any query for this situation?

Resources