refactor: Refactor queries by using related query from objectionjs
This commit is contained in:
@@ -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;
|
||||
|
Reference in New Issue
Block a user