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,31 +1,36 @@
import { GraphQLList, GraphQLNonNull } from 'graphql'; import { GraphQLList, GraphQLNonNull } from 'graphql';
import Connection from '../../models/connection';
import App from '../../models/app'; import App from '../../models/app';
import RequestWithCurrentUser from '../../types/express/request-with-current-user'; import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import connectionType from '../types/connection'; import connectionType from '../types/connection';
import availableAppsEnumType from '../types/available-apps-enum-type'; import availableAppsEnumType from '../types/available-apps-enum-type';
type Params = { type Params = {
key: string key: string;
} };
const getAppConnectionsResolver = async (params: Params, req: RequestWithCurrentUser) => { const getAppConnectionsResolver = async (
params: Params,
req: RequestWithCurrentUser
) => {
const app = App.findOneByKey(params.key); const app = App.findOneByKey(params.key);
const connections = await Connection.query()
.where({ user_id: req.currentUser.id, key: params.key })
return connections.map((connection: any) => ({ const connections = await req.currentUser.$relatedQuery('connections').where({
key: params.key,
});
return connections.map((connection) => ({
...connection, ...connection,
app, app,
})); }));
} };
const getAppConnections = { const getAppConnections = {
type: GraphQLList(connectionType), type: GraphQLList(connectionType),
args: { args: {
key: { type: GraphQLNonNull(availableAppsEnumType) }, key: { type: GraphQLNonNull(availableAppsEnumType) },
}, },
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => getAppConnectionsResolver(params, req) resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
} getAppConnectionsResolver(params, req),
};
export default getAppConnections; export default getAppConnections;

View File

@@ -2,19 +2,21 @@ import { GraphQLNonNull } from 'graphql';
import App from '../../models/app'; import App from '../../models/app';
import appType from '../types/app'; import appType from '../types/app';
import RequestWithCurrentUser from '../../types/express/request-with-current-user'; import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import Connection from '../../models/connection';
import availableAppsEnumType from '../types/available-apps-enum-type'; import availableAppsEnumType from '../types/available-apps-enum-type';
type Params = { type Params = {
key: string key: string;
} };
const getAppResolver = async (params: Params, req: RequestWithCurrentUser) => { const getAppResolver = async (params: Params, req: RequestWithCurrentUser) => {
const app = App.findOneByKey(params.key); const app = App.findOneByKey(params.key);
if (req.currentUser?.id) { if (req.currentUser) {
const connections = await Connection.query() const connections = await req.currentUser
.where({ user_id: req.currentUser.id, key: params.key }); .$relatedQuery('connections')
.where({
key: params.key,
});
return { return {
...app, ...app,
@@ -23,14 +25,15 @@ const getAppResolver = async (params: Params, req: RequestWithCurrentUser) => {
} }
return app; return app;
} };
const getApp = { const getApp = {
type: appType, type: appType,
args: { args: {
key: { type: GraphQLNonNull(availableAppsEnumType) }, key: { type: GraphQLNonNull(availableAppsEnumType) },
}, },
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => getAppResolver(params, req) resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
} getAppResolver(params, req),
};
export default getApp; export default getApp;

View File

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

View File

@@ -1,5 +1,4 @@
import { GraphQLNonNull, GraphQLInt } from 'graphql'; import { GraphQLNonNull, GraphQLInt } from 'graphql';
import Flow from '../../models/flow';
import RequestWithCurrentUser from '../../types/express/request-with-current-user'; import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import flowType from '../types/flow'; import flowType from '../types/flow';
@@ -8,10 +7,11 @@ type Params = {
}; };
const getFlowResolver = async (params: Params, req: RequestWithCurrentUser) => { const getFlowResolver = async (params: Params, req: RequestWithCurrentUser) => {
const flow = await Flow.query() const flow = await req.currentUser
.$relatedQuery('flows')
.withGraphJoined('[steps.[connection]]') .withGraphJoined('[steps.[connection]]')
.orderBy('steps.position', 'asc') .orderBy('steps.position', 'asc')
.findOne({ 'flows.user_id': req.currentUser.id, 'flows.id': params.id }) .findOne({ 'flows.id': params.id })
.throwIfNotFound(); .throwIfNotFound();
return flow; return flow;

View File

@@ -1,19 +1,21 @@
import { GraphQLList } from 'graphql'; import { GraphQLList } from 'graphql';
import Flow from '../../models/flow';
import RequestWithCurrentUser from '../../types/express/request-with-current-user'; import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import flowType from '../types/flow'; import flowType from '../types/flow';
const getFlowsResolver = async (req: RequestWithCurrentUser): Promise<any[]> => { const getFlowsResolver = async (
const flows = await Flow.query() req: RequestWithCurrentUser
.withGraphJoined('[steps.[connection]]') ): Promise<any[]> => {
.where({'flows.user_id': req.currentUser.id}); const flows = await req.currentUser
.$relatedQuery('flows')
.withGraphJoined('[steps.[connection]]');
return flows; return flows;
} };
const getFlows = { const getFlows = {
type: GraphQLList(flowType), type: GraphQLList(flowType),
resolve: (_: any, _params: any, req: RequestWithCurrentUser) => getFlowsResolver(req) resolve: (_: any, _params: any, req: RequestWithCurrentUser) =>
} getFlowsResolver(req),
};
export default getFlows; export default getFlows;

View File

@@ -1,37 +1,43 @@
import { GraphQLString, GraphQLNonNull } from 'graphql'; import { GraphQLString, GraphQLNonNull } from 'graphql';
import Connection from '../../models/connection';
import RequestWithCurrentUser from '../../types/express/request-with-current-user'; import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import connectionType from '../types/connection' import connectionType from '../types/connection';
type Params = { type Params = {
id: string, id: string;
data: object data: object;
} };
const testConnectionResolver = async (params: Params, req: RequestWithCurrentUser) => { const testConnectionResolver = async (
let connection = await Connection.query().findOne({ params: Params,
user_id: req.currentUser.id, req: RequestWithCurrentUser
id: params.id ) => {
}).throwIfNotFound(); let connection = await req.currentUser
.$relatedQuery('connections')
.findOne({
id: params.id,
})
.throwIfNotFound();
const appClass = (await import(`../../apps/${connection.key}`)).default; const appClass = (await import(`../../apps/${connection.key}`)).default;
const appInstance = new appClass(connection.data); const appInstance = new appClass(connection.data);
const isStillVerified = await appInstance.authenticationClient.isStillVerified(); const isStillVerified =
await appInstance.authenticationClient.isStillVerified();
connection = await connection.$query().patchAndFetch({ connection = await connection.$query().patchAndFetch({
data: connection.data, data: connection.data,
verified: isStillVerified verified: isStillVerified,
}) });
return connection; return connection;
} };
const testConnection = { const testConnection = {
type: connectionType, type: connectionType,
args: { args: {
id: { type: GraphQLNonNull(GraphQLString) } id: { type: GraphQLNonNull(GraphQLString) },
}, },
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => testConnectionResolver(params, req) resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
testConnectionResolver(params, req),
}; };
export default testConnection; export default testConnection;

View File

@@ -1,12 +1,15 @@
import { QueryContext, ModelOptions } from 'objection'; import { QueryContext, ModelOptions } from 'objection';
import Base from './base'; import Base from './base';
import Connection from './connection'; import Connection from './connection';
import Flow from './flow';
import bcrypt from 'bcrypt'; import bcrypt from 'bcrypt';
class User extends Base { class User extends Base {
id!: number id!: number;
email!: string email!: string;
password!: string password!: string;
connections?: [Connection];
flows?: [Flow];
static tableName = 'users'; static tableName = 'users';
@@ -18,8 +21,8 @@ class User extends Base {
id: { type: 'integer' }, id: { type: 'integer' },
email: { type: 'string', format: 'email', minLength: 1, maxLength: 255 }, email: { type: 'string', format: 'email', minLength: 1, maxLength: 255 },
password: { type: 'string', minLength: 1, maxLength: 255 }, password: { type: 'string', minLength: 1, maxLength: 255 },
} },
} };
static relationMappings = () => ({ static relationMappings = () => ({
connections: { connections: {
@@ -29,8 +32,16 @@ class User extends Base {
from: 'users.id', from: 'users.id',
to: 'connections.user_id', to: 'connections.user_id',
}, },
} },
}) flows: {
relation: Base.HasManyRelation,
modelClass: Flow,
join: {
from: 'users.id',
to: 'flows.user_id',
},
},
});
login(password: string) { login(password: string) {
return bcrypt.compare(password, this.password); return bcrypt.compare(password, this.password);
@@ -42,14 +53,14 @@ class User extends Base {
async $beforeInsert(queryContext: QueryContext) { async $beforeInsert(queryContext: QueryContext) {
await super.$beforeInsert(queryContext); await super.$beforeInsert(queryContext);
await this.generateHash() await this.generateHash();
} }
async $beforeUpdate(opt: ModelOptions, queryContext: QueryContext) { async $beforeUpdate(opt: ModelOptions, queryContext: QueryContext) {
await super.$beforeUpdate(opt, queryContext); await super.$beforeUpdate(opt, queryContext);
if (this.password) { if (this.password) {
await this.generateHash() await this.generateHash();
} }
} }
} }

View File

@@ -1,8 +1,8 @@
import { Request } from 'express'; import { Request } from 'express';
import User from '../user'; import User from '../../models/user';
interface RequestWithCurrentUser extends Request { interface RequestWithCurrentUser extends Request {
currentUser: User currentUser: User;
} }
export default RequestWithCurrentUser; export default RequestWithCurrentUser;

View File

@@ -1,9 +0,0 @@
type User = {
id: number,
email: string,
password: string,
createdAt: string,
updatedAt: string
}
export default User;