refactor: Use graphql schema directly with graphql-tools library

This commit is contained in:
Faruk AYDIN
2022-03-06 18:16:28 +03:00
committed by Ömer Faruk Aydın
parent 96cca96bff
commit 9926e5589e
48 changed files with 551 additions and 839 deletions

View File

@@ -1,17 +1,16 @@
import { GraphQLNonNull, GraphQLString } from 'graphql';
import authLinkType from '../types/auth-link';
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import Context from '../../types/express/context';
import App from '../../models/app';
type Params = {
id: string;
};
const createAuthDataResolver = async (
const createAuthData = async (
_parent: unknown,
params: Params,
req: RequestWithCurrentUser
context: Context
) => {
const connection = await req.currentUser
const connection = await context.currentUser
.$relatedQuery('connections')
.findOne({
id: params.id,
@@ -38,13 +37,4 @@ const createAuthDataResolver = async (
return authLink;
};
const createAuthData = {
type: authLinkType,
args: {
id: { type: GraphQLNonNull(GraphQLString) },
},
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
createAuthDataResolver(params, req),
};
export default createAuthData;

View File

@@ -1,25 +1,24 @@
import { GraphQLNonNull } from 'graphql';
import App from '../../models/app';
import connectionType from '../types/connection';
import availableAppsEnumType from '../types/available-apps-enum-type';
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import { GraphQLJSONObject } from 'graphql-type-json';
import Context from '../../types/express/context';
import { IJSONObject } from '@automatisch/types';
type Params = {
key: string;
formattedData: IJSONObject;
};
const createConnectionResolver = async (
const createConnection = async (
_parent: unknown,
params: Params,
req: RequestWithCurrentUser
context: Context
) => {
const app = App.findOneByKey(params.key);
const connection = await req.currentUser.$relatedQuery('connections').insert({
key: params.key,
formattedData: params.formattedData,
});
const connection = await context.currentUser
.$relatedQuery('connections')
.insert({
key: params.key,
formattedData: params.formattedData,
});
return {
...connection,
@@ -27,14 +26,4 @@ const createConnectionResolver = async (
};
};
const createConnection = {
type: connectionType,
args: {
key: { type: GraphQLNonNull(availableAppsEnumType) },
formattedData: { type: GraphQLNonNull(GraphQLJSONObject) },
},
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
createConnectionResolver(params, req),
};
export default createConnection;

View File

@@ -1,6 +1,5 @@
import Step from '../../models/step';
import flowType, { flowInputType } from '../types/flow';
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import Context from '../../types/express/context';
type Params = {
input: {
@@ -8,13 +7,14 @@ type Params = {
};
};
const createFlowResolver = async (
const createFlow = async (
_parent: unknown,
params: Params,
req: RequestWithCurrentUser
context: Context
) => {
const appKey = params?.input?.triggerAppKey;
const flow = await req.currentUser.$relatedQuery('flows').insert({
const flow = await context.currentUser.$relatedQuery('flows').insert({
name: 'Name your flow',
});
@@ -28,13 +28,4 @@ const createFlowResolver = async (
return flow;
};
const createFlow = {
type: flowType,
args: {
input: { type: flowInputType },
},
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
createFlowResolver(params, req),
};
export default createFlow;

View File

@@ -1,6 +1,4 @@
import { GraphQLNonNull } from 'graphql';
import stepType, { stepInputType } from '../types/step';
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import Context from '../../types/express/context';
type Params = {
input: {
@@ -18,13 +16,14 @@ type Params = {
};
};
const createStepResolver = async (
const createStep = async (
_parent: unknown,
params: Params,
req: RequestWithCurrentUser
context: Context
) => {
const { input } = params;
const flow = await req.currentUser
const flow = await context.currentUser
.$relatedQuery('flows')
.findOne({
id: input.flow.id,
@@ -61,13 +60,4 @@ const createStepResolver = async (
return step;
};
const createStep = {
type: stepType,
args: {
input: { type: new GraphQLNonNull(stepInputType) },
},
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
createStepResolver(params, req),
};
export default createStep;

View File

@@ -1,15 +1,15 @@
import { GraphQLString, GraphQLNonNull, GraphQLBoolean } from 'graphql';
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import Context from '../../types/express/context';
type Params = {
id: string;
};
const deleteConnectionResolver = async (
const deleteConnection = async (
_parent: unknown,
params: Params,
req: RequestWithCurrentUser
context: Context
) => {
await req.currentUser
await context.currentUser
.$relatedQuery('connections')
.delete()
.findOne({
@@ -20,13 +20,4 @@ const deleteConnectionResolver = async (
return;
};
const deleteConnection = {
type: GraphQLBoolean,
args: {
id: { type: GraphQLNonNull(GraphQLString) },
},
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
deleteConnectionResolver(params, req),
};
export default deleteConnection;

View File

@@ -1,15 +1,15 @@
import { GraphQLString, GraphQLNonNull, GraphQLBoolean } from 'graphql';
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import Context from '../../types/express/context';
type Params = {
id: string;
};
const deleteFlowResolver = async (
const deleteFlow = async (
_parent: unknown,
params: Params,
req: RequestWithCurrentUser
context: Context
) => {
await req.currentUser
await context.currentUser
.$relatedQuery('flows')
.delete()
.findOne({
@@ -20,13 +20,4 @@ const deleteFlowResolver = async (
return;
};
const deleteFlow = {
type: GraphQLBoolean,
args: {
id: { type: GraphQLNonNull(GraphQLString) },
},
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
deleteFlowResolver(params, req),
};
export default deleteFlow;

View File

@@ -1,16 +1,15 @@
import { GraphQLString, GraphQLNonNull } from 'graphql';
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import stepType from '../types/step';
import Context from '../../types/express/context';
type Params = {
id: string;
};
const deleteStepResolver = async (
const deleteStep = async (
_parent: unknown,
params: Params,
req: RequestWithCurrentUser
context: Context
) => {
const step = await req.currentUser
const step = await context.currentUser
.$relatedQuery('steps')
.withGraphFetched('flow')
.findOne({
@@ -40,13 +39,4 @@ const deleteStepResolver = async (
return step;
};
const deleteStep = {
type: stepType,
args: {
id: { type: GraphQLNonNull(GraphQLString) },
},
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
deleteStepResolver(params, req),
};
export default deleteStep;

View File

@@ -1,17 +1,17 @@
import { GraphQLString, GraphQLNonNull } from 'graphql';
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import executeFlowType from '../types/execute-flow';
import Context from '../../types/express/context';
import Processor from '../../services/processor';
import processorQueue from '../../queues/processor';
type Params = {
stepId: string;
};
const executeFlowResolver = async (
const executeFlow = async (
_parent: unknown,
params: Params,
req: RequestWithCurrentUser
): Promise<any> => {
const step = await req.currentUser
context: Context
) => {
const step = await context.currentUser
.$relatedQuery('steps')
.withGraphFetched('connection')
.findOne({
@@ -35,13 +35,4 @@ const executeFlowResolver = async (
return { data, step };
};
const executeFlow = {
type: executeFlowType,
args: {
stepId: { type: GraphQLNonNull(GraphQLString) },
},
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
executeFlowResolver(params, req),
};
export default executeFlow;

View File

@@ -1,6 +1,4 @@
import { GraphQLString, GraphQLNonNull } from 'graphql';
import User from '../../models/user';
import authType from '../types/auth';
import jwt from 'jsonwebtoken';
import appConfig from '../../config/app';
@@ -9,7 +7,7 @@ type Params = {
password: string;
};
const loginResolver = async (params: Params) => {
const login = async (_parent: unknown, params: Params) => {
const user = await User.query().findOne({
email: params.email,
});
@@ -23,13 +21,4 @@ const loginResolver = async (params: Params) => {
throw new Error('User could not be found.');
};
const login = {
type: authType,
args: {
email: { type: GraphQLNonNull(GraphQLString) },
password: { type: GraphQLNonNull(GraphQLString) },
},
resolve: (_: any, params: any) => loginResolver(params),
};
export default login;

View File

@@ -1,16 +1,15 @@
import { GraphQLString, GraphQLNonNull } from 'graphql';
import connectionType from '../types/connection';
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import Context from '../../types/express/context';
type Params = {
id: string;
};
const resetConnectionResolver = async (
const resetConnection = async (
_parent: unknown,
params: Params,
req: RequestWithCurrentUser
context: Context
) => {
let connection = await req.currentUser
let connection = await context.currentUser
.$relatedQuery('connections')
.findOne({
id: params.id,
@@ -24,13 +23,4 @@ const resetConnectionResolver = async (
return connection;
};
const resetConnection = {
type: connectionType,
args: {
id: { type: GraphQLNonNull(GraphQLString) },
},
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
resetConnectionResolver(params, req),
};
export default resetConnection;

View File

@@ -1,7 +1,4 @@
import { GraphQLString, GraphQLNonNull } from 'graphql';
import { GraphQLJSONObject } from 'graphql-type-json';
import connectionType from '../types/connection';
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import Context from '../../types/express/context';
import { IJSONObject } from '@automatisch/types';
type Params = {
@@ -9,11 +6,12 @@ type Params = {
formattedData: IJSONObject;
};
const updateConnectionResolver = async (
const updateConnection = async (
_parent: unknown,
params: Params,
req: RequestWithCurrentUser
context: Context
) => {
let connection = await req.currentUser
let connection = await context.currentUser
.$relatedQuery('connections')
.findOne({
id: params.id,
@@ -30,14 +28,4 @@ const updateConnectionResolver = async (
return connection;
};
const updateConnection = {
type: connectionType,
args: {
id: { type: GraphQLNonNull(GraphQLString) },
formattedData: { type: GraphQLNonNull(GraphQLJSONObject) },
},
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
updateConnectionResolver(params, req),
};
export default updateConnection;

View File

@@ -1,6 +1,4 @@
import { GraphQLString, GraphQLNonNull, GraphQLBoolean } from 'graphql';
import flowType from '../types/flow';
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import Context from '../../types/express/context';
type Params = {
id: string;
@@ -8,11 +6,12 @@ type Params = {
active: boolean;
};
const updateFlowResolver = async (
const updateFlow = async (
_parent: unknown,
params: Params,
req: RequestWithCurrentUser
context: Context
) => {
let flow = await req.currentUser
let flow = await context.currentUser
.$relatedQuery('flows')
.findOne({
id: params.id,
@@ -24,15 +23,4 @@ const updateFlowResolver = async (
return flow;
};
const updateFlow = {
type: flowType,
args: {
id: { type: GraphQLNonNull(GraphQLString) },
name: { type: GraphQLNonNull(GraphQLString) },
active: { type: GraphQLBoolean },
},
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
updateFlowResolver(params, req),
};
export default updateFlow;

View File

@@ -1,7 +1,5 @@
import { GraphQLNonNull } from 'graphql';
import Step from '../../models/step';
import stepType, { stepInputType } from '../types/step';
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import Context from '../../types/express/context';
type Params = {
input: {
@@ -17,13 +15,15 @@ type Params = {
};
};
};
const updateStepResolver = async (
const updateStep = async (
_parent: unknown,
params: Params,
req: RequestWithCurrentUser
context: Context
) => {
const { input } = params;
let step = await req.currentUser
let step = await context.currentUser
.$relatedQuery('steps')
.findOne({
'steps.id': input.id,
@@ -43,13 +43,4 @@ const updateStepResolver = async (
return step;
};
const updateStep = {
type: stepType,
args: {
input: { type: new GraphQLNonNull(stepInputType) },
},
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
updateStepResolver(params, req),
};
export default updateStep;

View File

@@ -1,17 +1,16 @@
import { GraphQLString, GraphQLNonNull } from 'graphql';
import connectionType from '../types/connection';
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import Context from '../../types/express/context';
import App from '../../models/app';
type Params = {
id: string;
};
const verifyConnectionResolver = async (
const verifyConnection = async (
_parent: unknown,
params: Params,
req: RequestWithCurrentUser
context: Context
) => {
let connection = await req.currentUser
let connection = await context.currentUser
.$relatedQuery('connections')
.findOne({
id: params.id,
@@ -36,13 +35,4 @@ const verifyConnectionResolver = async (
return connection;
};
const verifyConnection = {
type: connectionType,
args: {
id: { type: GraphQLNonNull(GraphQLString) },
},
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
verifyConnectionResolver(params, req),
};
export default verifyConnection;