refactor: Use graphql schema directly with graphql-tools library
This commit is contained in:

committed by
Ömer Faruk Aydın

parent
96cca96bff
commit
9926e5589e
@@ -1,22 +1,22 @@
|
||||
import { GraphQLList, GraphQLNonNull } from 'graphql';
|
||||
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';
|
||||
import Context from '../../types/express/context';
|
||||
|
||||
type Params = {
|
||||
key: string;
|
||||
};
|
||||
|
||||
const getAppConnectionsResolver = async (
|
||||
const getAppConnections = async (
|
||||
_parent: unknown,
|
||||
params: Params,
|
||||
req: RequestWithCurrentUser
|
||||
context: Context
|
||||
) => {
|
||||
const app = App.findOneByKey(params.key);
|
||||
|
||||
const connections = await req.currentUser.$relatedQuery('connections').where({
|
||||
key: params.key,
|
||||
});
|
||||
const connections = await context.currentUser
|
||||
.$relatedQuery('connections')
|
||||
.where({
|
||||
key: params.key,
|
||||
});
|
||||
|
||||
return connections.map((connection) => ({
|
||||
...connection,
|
||||
@@ -24,13 +24,4 @@ const getAppConnectionsResolver = async (
|
||||
}));
|
||||
};
|
||||
|
||||
const getAppConnections = {
|
||||
type: GraphQLList(connectionType),
|
||||
args: {
|
||||
key: { type: GraphQLNonNull(availableAppsEnumType) },
|
||||
},
|
||||
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
|
||||
getAppConnectionsResolver(params, req),
|
||||
};
|
||||
|
||||
export default getAppConnections;
|
||||
|
@@ -1,18 +1,15 @@
|
||||
import { GraphQLNonNull } from 'graphql';
|
||||
import App from '../../models/app';
|
||||
import appType from '../types/app';
|
||||
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||
import availableAppsEnumType from '../types/available-apps-enum-type';
|
||||
import Context from '../../types/express/context';
|
||||
|
||||
type Params = {
|
||||
key: string;
|
||||
};
|
||||
|
||||
const getAppResolver = async (params: Params, req: RequestWithCurrentUser) => {
|
||||
const getApp = async (_parent: unknown, params: Params, context: Context) => {
|
||||
const app = App.findOneByKey(params.key);
|
||||
|
||||
if (req.currentUser) {
|
||||
const connections = await req.currentUser
|
||||
if (context.currentUser) {
|
||||
const connections = await context.currentUser
|
||||
.$relatedQuery('connections')
|
||||
.where({
|
||||
key: params.key,
|
||||
@@ -27,13 +24,4 @@ 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),
|
||||
};
|
||||
|
||||
export default getApp;
|
||||
|
@@ -1,29 +1,19 @@
|
||||
import { GraphQLString, GraphQLList, GraphQLBoolean } from 'graphql';
|
||||
import appType from '../types/app';
|
||||
import App from '../../models/app';
|
||||
import { IApp } from '@automatisch/types';
|
||||
|
||||
type Params = {
|
||||
name: string;
|
||||
onlyWithTriggers: boolean;
|
||||
};
|
||||
|
||||
const getAppsResolver = (params: Params) => {
|
||||
const getApps = (_parent: unknown, params: Params) => {
|
||||
const apps = App.findAll(params.name);
|
||||
|
||||
if (params.onlyWithTriggers) {
|
||||
return apps.filter((app: any) => app.triggers?.length);
|
||||
return apps.filter((app: IApp) => app.triggers?.length);
|
||||
}
|
||||
|
||||
return apps;
|
||||
};
|
||||
|
||||
const getApps = {
|
||||
type: GraphQLList(appType),
|
||||
args: {
|
||||
name: { type: GraphQLString },
|
||||
onlyWithTriggers: { type: GraphQLBoolean },
|
||||
},
|
||||
resolve: (_: any, params: Params) => getAppsResolver(params),
|
||||
};
|
||||
|
||||
export default getApps;
|
||||
|
@@ -1,19 +1,19 @@
|
||||
import { GraphQLList, GraphQLString } from 'graphql';
|
||||
import App from '../../models/app';
|
||||
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||
import appType from '../types/app';
|
||||
import Context from '../../types/express/context';
|
||||
import { IApp, IConnection } from '@automatisch/types';
|
||||
|
||||
type Params = {
|
||||
name: string;
|
||||
};
|
||||
|
||||
const getConnectedAppsResolver = async (
|
||||
const getConnectedApps = async (
|
||||
_parent: unknown,
|
||||
params: Params,
|
||||
req: RequestWithCurrentUser
|
||||
context: Context
|
||||
) => {
|
||||
let apps = App.findAll(params.name);
|
||||
|
||||
const connections = await req.currentUser
|
||||
const connections = await context.currentUser
|
||||
.$relatedQuery('connections')
|
||||
.select('connections.key')
|
||||
.count('connections.id as count')
|
||||
@@ -23,10 +23,10 @@ const getConnectedAppsResolver = async (
|
||||
const connectionKeys = connections.map((connection) => connection.key);
|
||||
|
||||
apps = apps
|
||||
.filter((app: any) => connectionKeys.includes(app.key))
|
||||
.map((app: any) => {
|
||||
.filter((app: IApp) => connectionKeys.includes(app.key))
|
||||
.map((app: IApp) => {
|
||||
const connection = connections.find(
|
||||
(connection: any) => connection.key === app.key
|
||||
(connection: IConnection) => connection.key === app.key
|
||||
);
|
||||
|
||||
app.connectionCount = connection.count;
|
||||
@@ -36,13 +36,4 @@ const getConnectedAppsResolver = async (
|
||||
return apps;
|
||||
};
|
||||
|
||||
const getConnectedApps = {
|
||||
type: GraphQLList(appType),
|
||||
args: {
|
||||
name: { type: GraphQLString },
|
||||
},
|
||||
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
|
||||
getConnectedAppsResolver(params, req),
|
||||
};
|
||||
|
||||
export default getConnectedApps;
|
||||
|
@@ -1,13 +1,11 @@
|
||||
import { GraphQLNonNull, GraphQLString } from 'graphql';
|
||||
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||
import flowType from '../types/flow';
|
||||
import Context from '../../types/express/context';
|
||||
|
||||
type Params = {
|
||||
id: string;
|
||||
};
|
||||
|
||||
const getFlowResolver = async (params: Params, req: RequestWithCurrentUser) => {
|
||||
const flow = await req.currentUser
|
||||
const getFlow = async (_parent: unknown, params: Params, context: Context) => {
|
||||
const flow = await context.currentUser
|
||||
.$relatedQuery('flows')
|
||||
.withGraphJoined('[steps.[connection]]')
|
||||
.orderBy('steps.position', 'asc')
|
||||
@@ -17,13 +15,4 @@ const getFlowResolver = async (params: Params, req: RequestWithCurrentUser) => {
|
||||
return flow;
|
||||
};
|
||||
|
||||
const getFlow = {
|
||||
type: flowType,
|
||||
args: {
|
||||
id: { type: GraphQLNonNull(GraphQLString) },
|
||||
},
|
||||
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
|
||||
getFlowResolver(params, req),
|
||||
};
|
||||
|
||||
export default getFlow;
|
||||
|
@@ -1,21 +1,15 @@
|
||||
import { GraphQLList } from 'graphql';
|
||||
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||
import flowType from '../types/flow';
|
||||
import Context from '../../types/express/context';
|
||||
|
||||
const getFlowsResolver = async (
|
||||
req: RequestWithCurrentUser
|
||||
): Promise<any[]> => {
|
||||
const flows = await req.currentUser
|
||||
const getFlows = async (
|
||||
_parent: unknown,
|
||||
_params: unknown,
|
||||
context: Context
|
||||
) => {
|
||||
const flows = await context.currentUser
|
||||
.$relatedQuery('flows')
|
||||
.withGraphJoined('[steps.[connection]]');
|
||||
|
||||
return flows;
|
||||
};
|
||||
|
||||
const getFlows = {
|
||||
type: GraphQLList(flowType),
|
||||
resolve: (_: any, _params: any, req: RequestWithCurrentUser) =>
|
||||
getFlowsResolver(req),
|
||||
};
|
||||
|
||||
export default getFlows;
|
||||
|
@@ -1,21 +1,20 @@
|
||||
import { GraphQLNonNull, GraphQLString, GraphQLList } from 'graphql';
|
||||
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||
import stepType from '../types/step';
|
||||
import Context from '../../types/express/context';
|
||||
|
||||
type Params = {
|
||||
stepId: string;
|
||||
};
|
||||
|
||||
const getStepWithTestExecutionsResolver = async (
|
||||
const getStepWithTestExecutions = async (
|
||||
_parent: unknown,
|
||||
params: Params,
|
||||
req: RequestWithCurrentUser
|
||||
context: Context
|
||||
) => {
|
||||
const step = await req.currentUser
|
||||
const step = await context.currentUser
|
||||
.$relatedQuery('steps')
|
||||
.findOne({ 'steps.id': params.stepId })
|
||||
.throwIfNotFound();
|
||||
|
||||
const previousStepsWithCurrentStep = await req.currentUser
|
||||
const previousStepsWithCurrentStep = await context.currentUser
|
||||
.$relatedQuery('steps')
|
||||
.withGraphJoined('executionSteps')
|
||||
.where('flow_id', '=', step.flowId)
|
||||
@@ -29,13 +28,4 @@ const getStepWithTestExecutionsResolver = async (
|
||||
return previousStepsWithCurrentStep;
|
||||
};
|
||||
|
||||
const getStepWithTestExecutions = {
|
||||
type: GraphQLList(stepType),
|
||||
args: {
|
||||
stepId: { type: GraphQLNonNull(GraphQLString) },
|
||||
},
|
||||
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
|
||||
getStepWithTestExecutionsResolver(params, req),
|
||||
};
|
||||
|
||||
export default getStepWithTestExecutions;
|
||||
|
@@ -1,17 +1,17 @@
|
||||
import { GraphQLString, GraphQLNonNull } from 'graphql';
|
||||
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||
import connectionType from '../types/connection';
|
||||
import Context from '../../types/express/context';
|
||||
import App from '../../models/app';
|
||||
|
||||
type Params = {
|
||||
id: string;
|
||||
data: object;
|
||||
};
|
||||
const testConnectionResolver = async (
|
||||
|
||||
const testConnection = 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,
|
||||
@@ -33,13 +33,4 @@ const testConnectionResolver = async (
|
||||
return connection;
|
||||
};
|
||||
|
||||
const testConnection = {
|
||||
type: connectionType,
|
||||
args: {
|
||||
id: { type: GraphQLNonNull(GraphQLString) },
|
||||
},
|
||||
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
|
||||
testConnectionResolver(params, req),
|
||||
};
|
||||
|
||||
export default testConnection;
|
||||
|
Reference in New Issue
Block a user