refactor: Use related query from objectionjs for mutations
This commit is contained in:

committed by
Ömer Faruk Aydın

parent
e7c537f217
commit
86d5cceec7
@@ -1,31 +1,36 @@
|
||||
import { GraphQLInt, GraphQLNonNull } from 'graphql';
|
||||
import Connection from '../../models/connection';
|
||||
import connectionType from '../types/connection';
|
||||
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||
|
||||
type Params = {
|
||||
id: number
|
||||
}
|
||||
id: number;
|
||||
};
|
||||
|
||||
const resetConnectionResolver = async (params: Params, req: RequestWithCurrentUser) => {
|
||||
let connection = await Connection.query().findOne({
|
||||
user_id: req.currentUser.id,
|
||||
id: params.id
|
||||
}).throwIfNotFound();
|
||||
const resetConnectionResolver = async (
|
||||
params: Params,
|
||||
req: RequestWithCurrentUser
|
||||
) => {
|
||||
let connection = await req.currentUser
|
||||
.$relatedQuery('connections')
|
||||
.findOne({
|
||||
id: params.id,
|
||||
})
|
||||
.throwIfNotFound();
|
||||
|
||||
connection = await connection.$query().patchAndFetch({
|
||||
data: { screenName: connection.data.screenName }
|
||||
})
|
||||
data: { screenName: connection.data.screenName },
|
||||
});
|
||||
|
||||
return connection;
|
||||
}
|
||||
};
|
||||
|
||||
const resetConnection = {
|
||||
type: connectionType,
|
||||
args: {
|
||||
id: { type: GraphQLNonNull(GraphQLInt) },
|
||||
},
|
||||
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => resetConnectionResolver(params, req)
|
||||
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
|
||||
resetConnectionResolver(params, req),
|
||||
};
|
||||
|
||||
export default resetConnection;
|
||||
|
Reference in New Issue
Block a user