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

@@ -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;