refactor: Refactor queries by using related query from objectionjs

This commit is contained in:
Faruk AYDIN
2022-01-28 21:08:49 +03:00
committed by Ali BARIN
parent 5b392f3f87
commit 2416ce13a7
9 changed files with 106 additions and 83 deletions

View File

@@ -1,43 +1,48 @@
import { GraphQLList, GraphQLString } from 'graphql';
import Connection from '../../models/connection';
import App from '../../models/app';
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import appType from '../types/app';
type Params = {
name: string
}
name: string;
};
const getConnectedAppsResolver = async (params: Params, req: RequestWithCurrentUser) => {
let apps = App.findAll(params.name)
const getConnectedAppsResolver = async (
params: Params,
req: RequestWithCurrentUser
) => {
let apps = App.findAll(params.name);
const connections = await Connection.query()
const connections = await req.currentUser
.$relatedQuery('connections')
.select('connections.key')
.count('connections.id as count')
.where({ user_id: req.currentUser.id, verified: true })
.groupBy('connections.key')
.where({ verified: true })
.groupBy('connections.key');
const connectionKeys = connections.map(connection => connection.key)
const connectionKeys = connections.map((connection) => connection.key);
apps = apps
.filter((app: any) => connectionKeys.includes(app.key))
.map((app: any) => {
const connection = connections
.find((connection: any) => connection.key === app.key)
const connection = connections.find(
(connection: any) => connection.key === app.key
);
app.connectionCount = connection.count;
return app;
})
});
return apps;
}
};
const getConnectedApps = {
type: GraphQLList(appType),
args: {
name: { type: GraphQLString }
name: { type: GraphQLString },
},
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => getConnectedAppsResolver(params, req)
}
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
getConnectedAppsResolver(params, req),
};
export default getConnectedApps;