refactor: Refactor queries by using related query from objectionjs
This commit is contained in:
@@ -1,37 +1,43 @@
|
||||
import { GraphQLString, GraphQLNonNull } from 'graphql';
|
||||
import Connection from '../../models/connection';
|
||||
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||
import connectionType from '../types/connection'
|
||||
import connectionType from '../types/connection';
|
||||
|
||||
type Params = {
|
||||
id: string,
|
||||
data: object
|
||||
}
|
||||
const testConnectionResolver = async (params: Params, req: RequestWithCurrentUser) => {
|
||||
let connection = await Connection.query().findOne({
|
||||
user_id: req.currentUser.id,
|
||||
id: params.id
|
||||
}).throwIfNotFound();
|
||||
id: string;
|
||||
data: object;
|
||||
};
|
||||
const testConnectionResolver = async (
|
||||
params: Params,
|
||||
req: RequestWithCurrentUser
|
||||
) => {
|
||||
let connection = await req.currentUser
|
||||
.$relatedQuery('connections')
|
||||
.findOne({
|
||||
id: params.id,
|
||||
})
|
||||
.throwIfNotFound();
|
||||
|
||||
const appClass = (await import(`../../apps/${connection.key}`)).default;
|
||||
|
||||
const appInstance = new appClass(connection.data);
|
||||
const isStillVerified = await appInstance.authenticationClient.isStillVerified();
|
||||
const isStillVerified =
|
||||
await appInstance.authenticationClient.isStillVerified();
|
||||
|
||||
connection = await connection.$query().patchAndFetch({
|
||||
data: connection.data,
|
||||
verified: isStillVerified
|
||||
})
|
||||
verified: isStillVerified,
|
||||
});
|
||||
|
||||
return connection;
|
||||
}
|
||||
};
|
||||
|
||||
const testConnection = {
|
||||
type: connectionType,
|
||||
args: {
|
||||
id: { type: GraphQLNonNull(GraphQLString) }
|
||||
id: { type: GraphQLNonNull(GraphQLString) },
|
||||
},
|
||||
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => testConnectionResolver(params, req)
|
||||
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
|
||||
testConnectionResolver(params, req),
|
||||
};
|
||||
|
||||
export default testConnection;
|
||||
|
Reference in New Issue
Block a user