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

committed by
Ömer Faruk Aydın

parent
e7c537f217
commit
86d5cceec7
@@ -1,33 +1,38 @@
|
||||
import { GraphQLInt, GraphQLString, GraphQLNonNull } from 'graphql';
|
||||
import Flow from '../../models/flow';
|
||||
import flowType from '../types/flow';
|
||||
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||
|
||||
type Params = {
|
||||
id: number,
|
||||
name: string
|
||||
}
|
||||
const updateFlowResolver = async (params: Params, req: RequestWithCurrentUser) => {
|
||||
let flow = await Flow.query().findOne({
|
||||
user_id: req.currentUser.id,
|
||||
id: params.id
|
||||
}).throwIfNotFound();
|
||||
id: number;
|
||||
name: string;
|
||||
};
|
||||
const updateFlowResolver = async (
|
||||
params: Params,
|
||||
req: RequestWithCurrentUser
|
||||
) => {
|
||||
let flow = await req.currentUser
|
||||
.$relatedQuery('flows')
|
||||
.findOne({
|
||||
id: params.id,
|
||||
})
|
||||
.throwIfNotFound();
|
||||
|
||||
flow = await flow.$query().patchAndFetch({
|
||||
...flow,
|
||||
...params
|
||||
})
|
||||
...params,
|
||||
});
|
||||
|
||||
return flow;
|
||||
}
|
||||
};
|
||||
|
||||
const updateFlow = {
|
||||
type: flowType,
|
||||
args: {
|
||||
id: { type: GraphQLNonNull(GraphQLInt) },
|
||||
name: { type: GraphQLNonNull(GraphQLString) }
|
||||
name: { type: GraphQLNonNull(GraphQLString) },
|
||||
},
|
||||
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => updateFlowResolver(params, req)
|
||||
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
|
||||
updateFlowResolver(params, req),
|
||||
};
|
||||
|
||||
export default updateFlow;
|
||||
|
Reference in New Issue
Block a user