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

committed by
Ömer Faruk Aydın

parent
e7c537f217
commit
86d5cceec7
@@ -1,5 +1,4 @@
|
||||
import { GraphQLNonNull } from 'graphql';
|
||||
import Connection from '../../models/connection';
|
||||
import App from '../../models/app';
|
||||
import connectionType from '../types/connection';
|
||||
import availableAppsEnumType from '../types/available-apps-enum-type';
|
||||
@@ -7,31 +6,34 @@ import RequestWithCurrentUser from '../../types/express/request-with-current-use
|
||||
import { GraphQLJSONObject } from 'graphql-type-json';
|
||||
|
||||
type Params = {
|
||||
key: string,
|
||||
data: object
|
||||
}
|
||||
const createConnectionResolver = async (params: Params, req: RequestWithCurrentUser) => {
|
||||
key: string;
|
||||
data: object;
|
||||
};
|
||||
const createConnectionResolver = async (
|
||||
params: Params,
|
||||
req: RequestWithCurrentUser
|
||||
) => {
|
||||
const app = App.findOneByKey(params.key);
|
||||
|
||||
const connection = await Connection.query().insert({
|
||||
const connection = await req.currentUser.$relatedQuery('connections').insert({
|
||||
key: params.key,
|
||||
data: params.data,
|
||||
userId: req.currentUser.id
|
||||
});
|
||||
|
||||
return {
|
||||
...connection,
|
||||
app,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const createConnection = {
|
||||
type: connectionType,
|
||||
args: {
|
||||
key: { type: GraphQLNonNull(availableAppsEnumType) },
|
||||
data: { type: GraphQLNonNull(GraphQLJSONObject) }
|
||||
data: { type: GraphQLNonNull(GraphQLJSONObject) },
|
||||
},
|
||||
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => createConnectionResolver(params, req)
|
||||
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
|
||||
createConnectionResolver(params, req),
|
||||
};
|
||||
|
||||
export default createConnection;
|
||||
|
Reference in New Issue
Block a user