refactor: Use related query from objectionjs for mutations

This commit is contained in:
Faruk AYDIN
2022-01-29 12:07:19 +03:00
committed by Ömer Faruk Aydın
parent e7c537f217
commit 86d5cceec7
11 changed files with 170 additions and 136 deletions

View File

@@ -4,20 +4,25 @@ import authLinkType from '../types/auth-link';
import RequestWithCurrentUser from '../../types/express/request-with-current-user'; import RequestWithCurrentUser from '../../types/express/request-with-current-user';
type Params = { type Params = {
id: number, id: number;
} };
const createAuthDataResolver = async (params: Params, req: RequestWithCurrentUser) => { const createAuthDataResolver = async (
const connection = await Connection.query().findOne({ params: Params,
user_id: req.currentUser.id, req: RequestWithCurrentUser
id: params.id ) => {
}).throwIfNotFound(); const 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({ const appInstance = new appClass({
consumerKey: connection.data.consumerKey, consumerKey: connection.data.consumerKey,
consumerSecret: connection.data.consumerSecret consumerSecret: connection.data.consumerSecret,
}); });
const authLink = await appInstance.authenticationClient.createAuthData(); const authLink = await appInstance.authenticationClient.createAuthData();
@@ -25,19 +30,20 @@ const createAuthDataResolver = async (params: Params, req: RequestWithCurrentUse
await connection.$query().patch({ await connection.$query().patch({
data: { data: {
...connection.data, ...connection.data,
...authLink ...authLink,
} },
}) });
return authLink; return authLink;
} };
const createAuthData = { const createAuthData = {
type: authLinkType, type: authLinkType,
args: { args: {
id: { type: GraphQLNonNull(GraphQLInt) }, id: { type: GraphQLNonNull(GraphQLInt) },
}, },
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => createAuthDataResolver(params, req) resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
createAuthDataResolver(params, req),
}; };
export default createAuthData; export default createAuthData;

View File

@@ -1,5 +1,4 @@
import { GraphQLNonNull } from 'graphql'; import { GraphQLNonNull } from 'graphql';
import Connection from '../../models/connection';
import App from '../../models/app'; import App from '../../models/app';
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';
@@ -7,31 +6,34 @@ import RequestWithCurrentUser from '../../types/express/request-with-current-use
import { GraphQLJSONObject } from 'graphql-type-json'; import { GraphQLJSONObject } from 'graphql-type-json';
type Params = { type Params = {
key: string, key: string;
data: object data: object;
} };
const createConnectionResolver = async (params: Params, req: RequestWithCurrentUser) => { const createConnectionResolver = async (
params: Params,
req: RequestWithCurrentUser
) => {
const app = App.findOneByKey(params.key); const app = App.findOneByKey(params.key);
const connection = await Connection.query().insert({ const connection = await req.currentUser.$relatedQuery('connections').insert({
key: params.key, key: params.key,
data: params.data, data: params.data,
userId: req.currentUser.id
}); });
return { return {
...connection, ...connection,
app, app,
}; };
} };
const createConnection = { const createConnection = {
type: connectionType, type: connectionType,
args: { args: {
key: { type: GraphQLNonNull(availableAppsEnumType) }, key: { type: GraphQLNonNull(availableAppsEnumType) },
data: { type: GraphQLNonNull(GraphQLJSONObject) } data: { type: GraphQLNonNull(GraphQLJSONObject) },
}, },
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => createConnectionResolver(params, req) resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
createConnectionResolver(params, req),
}; };
export default createConnection; export default createConnection;

View File

@@ -1,12 +1,9 @@
import Flow from '../../models/flow';
import Step from '../../models/step'; import Step from '../../models/step';
import flowType from '../types/flow'; import flowType from '../types/flow';
import RequestWithCurrentUser from '../../types/express/request-with-current-user'; import RequestWithCurrentUser from '../../types/express/request-with-current-user';
const createFlowResolver = async (req: RequestWithCurrentUser) => { const createFlowResolver = async (req: RequestWithCurrentUser) => {
const flow = await Flow.query().insert({ const flow = await req.currentUser.$relatedQuery('flows').insert();
userId: req.currentUser.id,
});
await Step.query().insert({ await Step.query().insert({
flowId: flow.id, flowId: flow.id,

View File

@@ -1,6 +1,4 @@
import { GraphQLNonNull } from 'graphql'; import { GraphQLNonNull } from 'graphql';
import Step from '../../models/step';
import Flow from '../../models/flow';
import stepType, { stepInputType } from '../types/step'; import stepType, { stepInputType } from '../types/step';
import RequestWithCurrentUser from '../../types/express/request-with-current-user'; import RequestWithCurrentUser from '../../types/express/request-with-current-user';
@@ -26,33 +24,30 @@ const createStepResolver = async (
) => { ) => {
const { input } = params; const { input } = params;
const flow = await Flow.query() const flow = await req.currentUser
.$relatedQuery('flows')
.findOne({ .findOne({
id: input.flow.id, id: input.flow.id,
user_id: req.currentUser.id,
}) })
.throwIfNotFound(); .throwIfNotFound();
const previousStep = await Step.query() const previousStep = await flow
.$relatedQuery('steps')
.findOne({ .findOne({
id: input.previousStep.id, id: input.previousStep.id,
flow_id: flow.id,
}) })
.throwIfNotFound(); .throwIfNotFound();
const step = await Step.query().insertAndFetch({ const step = await flow.$relatedQuery('steps').insertAndFetch({
flowId: flow.id,
key: input.key, key: input.key,
appKey: input.appKey, appKey: input.appKey,
type: 'action', type: 'action',
position: previousStep.position + 1, position: previousStep.position + 1,
}); });
const nextSteps = await Step.query() const nextSteps = await flow
.where({ .$relatedQuery('steps')
flow_id: flow.id, .where('position', '>=', step.position)
})
.andWhere('position', '>=', step.position)
.whereNot('id', step.id); .whereNot('id', step.id);
const nextStepQueries = nextSteps.map(async (nextStep, index) => { const nextStepQueries = nextSteps.map(async (nextStep, index) => {

View File

@@ -1,26 +1,32 @@
import { GraphQLInt, GraphQLNonNull, GraphQLBoolean } from 'graphql'; import { GraphQLInt, GraphQLNonNull, GraphQLBoolean } 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';
type Params = { type Params = {
id: number, id: number;
data: object data: object;
} };
const deleteConnectionResolver = async (params: Params, req: RequestWithCurrentUser) => { const deleteConnectionResolver = async (
await Connection.query().delete().findOne({ params: Params,
user_id: req.currentUser.id, req: RequestWithCurrentUser
id: params.id ) => {
}).throwIfNotFound(); await req.currentUser
.$relatedQuery('connections')
.delete()
.findOne({
id: params.id,
})
.throwIfNotFound();
return; return;
} };
const deleteConnection = { const deleteConnection = {
type: GraphQLBoolean, type: GraphQLBoolean,
args: { args: {
id: { type: GraphQLNonNull(GraphQLInt) } id: { type: GraphQLNonNull(GraphQLInt) },
}, },
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => deleteConnectionResolver(params, req) resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
deleteConnectionResolver(params, req),
}; };
export default deleteConnection; export default deleteConnection;

View File

@@ -1,31 +1,36 @@
import { GraphQLInt, GraphQLNonNull } from 'graphql'; import { GraphQLInt, GraphQLNonNull } from 'graphql';
import Connection from '../../models/connection';
import connectionType from '../types/connection'; import connectionType from '../types/connection';
import RequestWithCurrentUser from '../../types/express/request-with-current-user'; import RequestWithCurrentUser from '../../types/express/request-with-current-user';
type Params = { type Params = {
id: number id: number;
} };
const resetConnectionResolver = async (params: Params, req: RequestWithCurrentUser) => { const resetConnectionResolver = 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();
connection = await connection.$query().patchAndFetch({ connection = await connection.$query().patchAndFetch({
data: { screenName: connection.data.screenName } data: { screenName: connection.data.screenName },
}) });
return connection; return connection;
} };
const resetConnection = { const resetConnection = {
type: connectionType, type: connectionType,
args: { args: {
id: { type: GraphQLNonNull(GraphQLInt) }, id: { type: GraphQLNonNull(GraphQLInt) },
}, },
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => resetConnectionResolver(params, req) resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
resetConnectionResolver(params, req),
}; };
export default resetConnection; export default resetConnection;

View File

@@ -1,36 +1,41 @@
import { GraphQLInt, GraphQLNonNull } from 'graphql'; import { GraphQLInt, GraphQLNonNull } from 'graphql';
import { GraphQLJSONObject } from 'graphql-type-json'; import { GraphQLJSONObject } from 'graphql-type-json';
import Connection from '../../models/connection';
import connectionType from '../types/connection'; import connectionType from '../types/connection';
import RequestWithCurrentUser from '../../types/express/request-with-current-user'; import RequestWithCurrentUser from '../../types/express/request-with-current-user';
type Params = { type Params = {
id: number, id: number;
data: object data: object;
} };
const updateConnectionResolver = async (params: Params, req: RequestWithCurrentUser) => { const updateConnectionResolver = 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();
connection = await connection.$query().patchAndFetch({ connection = await connection.$query().patchAndFetch({
data: { data: {
...connection.data, ...connection.data,
...params.data ...params.data,
} },
}) });
return connection; return connection;
} };
const updateConnection = { const updateConnection = {
type: connectionType, type: connectionType,
args: { args: {
id: { type: GraphQLNonNull(GraphQLInt) }, id: { type: GraphQLNonNull(GraphQLInt) },
data: { type: GraphQLNonNull(GraphQLJSONObject) } data: { type: GraphQLNonNull(GraphQLJSONObject) },
}, },
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => updateConnectionResolver(params, req) resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
updateConnectionResolver(params, req),
}; };
export default updateConnection; export default updateConnection;

View File

@@ -1,33 +1,38 @@
import { GraphQLInt, GraphQLString, GraphQLNonNull } from 'graphql'; import { GraphQLInt, GraphQLString, GraphQLNonNull } from 'graphql';
import Flow from '../../models/flow';
import flowType from '../types/flow'; import flowType from '../types/flow';
import RequestWithCurrentUser from '../../types/express/request-with-current-user'; import RequestWithCurrentUser from '../../types/express/request-with-current-user';
type Params = { type Params = {
id: number, id: number;
name: string name: string;
} };
const updateFlowResolver = async (params: Params, req: RequestWithCurrentUser) => { const updateFlowResolver = async (
let flow = await Flow.query().findOne({ params: Params,
user_id: req.currentUser.id, req: RequestWithCurrentUser
id: params.id ) => {
}).throwIfNotFound(); let flow = await req.currentUser
.$relatedQuery('flows')
.findOne({
id: params.id,
})
.throwIfNotFound();
flow = await flow.$query().patchAndFetch({ flow = await flow.$query().patchAndFetch({
...flow, ...flow,
...params ...params,
}) });
return flow; return flow;
} };
const updateFlow = { const updateFlow = {
type: flowType, type: flowType,
args: { args: {
id: { type: GraphQLNonNull(GraphQLInt) }, id: { type: GraphQLNonNull(GraphQLInt) },
name: { type: GraphQLNonNull(GraphQLString) } name: { type: GraphQLNonNull(GraphQLString) },
}, },
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => updateFlowResolver(params, req) resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
updateFlowResolver(params, req),
}; };
export default updateFlow; export default updateFlow;

View File

@@ -1,8 +1,6 @@
import { GraphQLInt, GraphQLString, GraphQLNonNull } from 'graphql'; import { GraphQLNonNull } from 'graphql';
import Flow from '../../models/flow';
import Step from '../../models/step'; import Step from '../../models/step';
import stepType, { stepInputType } from '../types/step'; import stepType, { stepInputType } from '../types/step';
import availableAppsEnumType from '../types/available-apps-enum-type';
import RequestWithCurrentUser from '../../types/express/request-with-current-user'; import RequestWithCurrentUser from '../../types/express/request-with-current-user';
type Params = { type Params = {
@@ -17,20 +15,27 @@ type Params = {
connection: { connection: {
id: number; id: number;
}; };
} };
} };
const updateStepResolver = async (params: Params, req: RequestWithCurrentUser) => { const updateStepResolver = async (
params: Params,
req: RequestWithCurrentUser
) => {
const { input } = params; const { input } = params;
const flow = await Flow.query().findOne({ const flow = await req.currentUser
user_id: req.currentUser.id, .$relatedQuery('flows')
id: input.flow.id .findOne({
}).throwIfNotFound(); id: input.flow.id,
})
.throwIfNotFound();
let step = await Step.query().findOne({ let step = await flow
flow_id: flow.id, .$relatedQuery('steps')
id: input.id .findOne({
}).throwIfNotFound(); id: input.id,
})
.throwIfNotFound();
step = await Step.query().patchAndFetchById(input.id, { step = await Step.query().patchAndFetchById(input.id, {
key: input.key, key: input.key,
@@ -40,14 +45,15 @@ const updateStepResolver = async (params: Params, req: RequestWithCurrentUser) =
}); });
return step; return step;
} };
const updateStep = { const updateStep = {
type: stepType, type: stepType,
args: { args: {
input: { type: new GraphQLNonNull(stepInputType) } input: { type: new GraphQLNonNull(stepInputType) },
}, },
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => updateStepResolver(params, req) resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
updateStepResolver(params, req),
}; };
export default updateStep; export default updateStep;

View File

@@ -1,39 +1,45 @@
import { GraphQLInt, GraphQLNonNull } from 'graphql'; import { GraphQLInt, GraphQLNonNull } from 'graphql';
import Connection from '../../models/connection';
import connectionType from '../types/connection'; import connectionType from '../types/connection';
import RequestWithCurrentUser from '../../types/express/request-with-current-user'; import RequestWithCurrentUser from '../../types/express/request-with-current-user';
type Params = { type Params = {
id: number id: number;
} };
const verifyConnectionResolver = async (params: Params, req: RequestWithCurrentUser) => { const verifyConnectionResolver = 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 verifiedCredentials = await appInstance.authenticationClient.verifyCredentials(); const verifiedCredentials =
await appInstance.authenticationClient.verifyCredentials();
connection = await connection.$query().patchAndFetch({ connection = await connection.$query().patchAndFetch({
data: { data: {
...connection.data, ...connection.data,
...verifiedCredentials ...verifiedCredentials,
}, },
verified: true verified: true,
}) });
return connection; return connection;
} };
const verifyConnection = { const verifyConnection = {
type: connectionType, type: connectionType,
args: { args: {
id: { type: GraphQLNonNull(GraphQLInt) } id: { type: GraphQLNonNull(GraphQLInt) },
}, },
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => verifyConnectionResolver(params, req) resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
verifyConnectionResolver(params, req),
}; };
export default verifyConnection; export default verifyConnection;

View File

@@ -1,10 +1,11 @@
import Base from './base' import Base from './base';
import Step from './step' import Step from './step';
class Flow extends Base { class Flow extends Base {
id!: number id!: number;
userId!: number userId!: number;
active: boolean active: boolean;
steps?: [Step];
static tableName = 'flows'; static tableName = 'flows';
@@ -14,9 +15,9 @@ class Flow extends Base {
properties: { properties: {
id: { type: 'integer' }, id: { type: 'integer' },
userId: { type: 'integer' }, userId: { type: 'integer' },
active: { type: 'boolean' } active: { type: 'boolean' },
} },
} };
static relationMappings = () => ({ static relationMappings = () => ({
steps: { steps: {
@@ -26,8 +27,8 @@ class Flow extends Base {
from: 'flows.id', from: 'flows.id',
to: 'steps.flow_id', to: 'steps.flow_id',
}, },
} },
}) });
} }
export default Flow; export default Flow;