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 Connection from '../../models/connection';
import App from '../../models/app';
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import connectionType from '../types/connection';
import availableAppsEnumType from '../types/available-apps-enum-type';
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 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,
app,
}));
}
};
const getAppConnections = {
type: GraphQLList(connectionType),
args: {
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;

View File

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

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;

View File

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

View File

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

View File

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