@@ -4,6 +4,7 @@ import { recordHelperArgs } from './helpers/record';
44import findById from './findById' ;
55import { addErrorCatcherField } from './helpers/addErrorCatcherField' ;
66import type { ExtendedResolveParams , GenResolverOpts } from './index' ;
7+ import { validationsForDocument , ValidationsWithMessage } from '../errors/validationsForDocument' ;
78import { ValidationError } from '../errors' ;
89
910export 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 } ) ;
0 commit comments