Skip to content

Commit 1916b3d

Browse files
committed
refactor: remove duplicated validation on save becase we call it manually right before save method
1 parent 898a5c3 commit 1916b3d

File tree

4 files changed

+11
-12
lines changed

4 files changed

+11
-12
lines changed

src/resolvers/createMany.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export default function createMany<TSource = Document, TContext = any>(
9494

9595
for (const record of recordData) {
9696
// eslint-disable-next-line new-cap
97-
let doc = new model(record);
97+
let doc: Document = new model(record);
9898
if (resolveParams.beforeRecordMutate) {
9999
doc = await resolveParams.beforeRecordMutate(doc, resolveParams);
100100
}
@@ -113,7 +113,7 @@ export default function createMany<TSource = Document, TContext = any>(
113113
});
114114
}
115115

116-
await model.create(docs);
116+
await model.create(docs, { validateBeforeSave: false });
117117

118118
return {
119119
records: docs,

src/resolvers/createOne.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export default function createOne<TSource = Document, TContext = any>(
8181
throw new ValidationError(validations);
8282
}
8383

84-
await doc.save();
84+
await doc.save({ validateBeforeSave: false });
8585
return {
8686
record: doc,
8787
recordId: tc.getRecordIdFn()(doc as any),

src/resolvers/updateById.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { recordHelperArgs } from './helpers/record';
44
import findById from './findById';
55
import { addErrorCatcherField } from './helpers/addErrorCatcherField';
66
import type { ExtendedResolveParams, GenResolverOpts } from './index';
7+
import { validationsForDocument, ValidationsWithMessage } from '../errors/validationsForDocument';
78
import { ValidationError } from '../errors';
89

910
export default function updateById<TSource = Document, TContext = any>(
@@ -78,7 +79,7 @@ export default function updateById<TSource = Document, TContext = any>(
7879
// We should get all data for document, cause Mongoose model may have hooks/middlewares
7980
// which required some fields which not in graphql projection
8081
// So empty projection returns all fields.
81-
let doc = await findByIdResolver.resolve({ ...resolveParams, projection: {} });
82+
let doc: Document = await findByIdResolver.resolve({ ...resolveParams, projection: {} });
8283

8384
if (resolveParams.beforeRecordMutate) {
8485
doc = await resolveParams.beforeRecordMutate(doc, resolveParams);
@@ -96,18 +97,16 @@ export default function updateById<TSource = Document, TContext = any>(
9697

9798
doc.set(recordData);
9899

99-
const validationErrors = await new Promise((resolve) => {
100-
doc.validate(null, null, resolve);
101-
});
102-
if (validationErrors) {
103-
throw new ValidationError(validationErrors as any);
100+
const validations: ValidationsWithMessage | null = await validationsForDocument(doc);
101+
if (validations) {
102+
throw new ValidationError(validations);
104103
}
105104

106-
await doc.save();
105+
await doc.save({ validateBeforeSave: false });
107106

108107
return {
109108
record: doc,
110-
recordId: tc.getRecordIdFn()(doc),
109+
recordId: tc.getRecordIdFn()(doc as any),
111110
};
112111
}) as any,
113112
});

src/resolvers/updateOne.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export default function updateOne<TSource = Document, TContext = any>(
9898
throw new ValidationError(validations);
9999
}
100100

101-
await doc.save();
101+
await doc.save({ validateBeforeSave: false });
102102
}
103103

104104
return {

0 commit comments

Comments
 (0)