Skip to content

Commit 898a5c3

Browse files
committed
refactor: resolve methods can throw Errors. They cannot populate error fields because they don't know anything about it. So decision about errors (return error in mutation payload or return it in top-level) can be made only in resolver wrapper addErrorCatcherField.
1 parent 65c216b commit 898a5c3

File tree

4 files changed

+39
-95
lines changed

4 files changed

+39
-95
lines changed

src/resolvers/createMany.ts

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
validationsForDocument,
99
ValidationsWithMessage,
1010
ManyValidations,
11-
ManyValidationsWithMessage,
1211
} from '../errors/validationsForDocument';
1312

1413
export default function createMany<TSource = Document, TContext = any>(
@@ -51,6 +50,7 @@ export default function createMany<TSource = Document, TContext = any>(
5150
createCount: {
5251
type: 'Int!',
5352
description: 'Number of created documents',
53+
resolve: (s: any) => s.createCount || 0,
5454
},
5555
});
5656
});
@@ -106,36 +106,20 @@ export default function createMany<TSource = Document, TContext = any>(
106106
}
107107

108108
const hasValidationError = !manyValidations.every((error) => error === null);
109-
if (!hasValidationError) {
110-
await model.create(docs);
111-
}
112-
113109
if (hasValidationError) {
114-
const manyValidationsWithMessage: ManyValidationsWithMessage = {
115-
message: 'Cannot createMany some documents contain errors',
110+
throw new ManyValidationError({
111+
message: 'Cannot execute createMany, some documents contain errors',
116112
errors: manyValidations,
117-
};
118-
119-
if (!resolveParams?.projection?.error) {
120-
// if client does not request `errors` field we throw Exception on to level
121-
throw new ManyValidationError(manyValidationsWithMessage);
122-
}
123-
return {
124-
records: null,
125-
recordIds: null,
126-
error: {
127-
name: 'ManyValidationError',
128-
...manyValidationsWithMessage,
129-
},
130-
createCount: 0,
131-
};
132-
} else {
133-
return {
134-
records: docs,
135-
recordIds: docs.map((doc) => doc._id),
136-
createCount: docs.length,
137-
};
113+
});
138114
}
115+
116+
await model.create(docs);
117+
118+
return {
119+
records: docs,
120+
recordIds: docs.map((doc) => doc._id),
121+
createCount: docs.length,
122+
};
139123
},
140124
});
141125

src/resolvers/createOne.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,8 @@ export default function createOne<TSource = Document, TContext = any>(
7777
}
7878

7979
const validations: ValidationsWithMessage | null = await validationsForDocument(doc);
80-
8180
if (validations) {
82-
if (!resolveParams?.projection?.error) {
83-
// if client does not request `errors` field we throw Exception on to level
84-
throw new ValidationError(validations);
85-
}
86-
return {
87-
record: null,
88-
recordId: null,
89-
error: {
90-
name: 'ValidationError',
91-
...validations,
92-
},
93-
};
94-
} else {
95-
await doc.save();
96-
return {
97-
record: doc,
98-
recordId: tc.getRecordIdFn()(doc as any),
99-
};
81+
throw new ValidationError(validations);
10082
}
10183

10284
await doc.save();

src/resolvers/updateById.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,27 @@ export default function updateById<TSource = Document, TContext = any>(
8888
throw new Error('Document not found');
8989
}
9090

91-
if (recordData) {
92-
doc.set(recordData);
93-
94-
const validationErrors = await new Promise((resolve) => {
95-
doc.validate(null, null, resolve);
96-
});
97-
if (validationErrors) {
98-
throw new ValidationError(validationErrors as any);
99-
}
100-
101-
await doc.save();
102-
103-
return {
104-
record: doc,
105-
recordId: tc.getRecordIdFn()(doc),
106-
};
91+
if (!recordData) {
92+
throw new Error(
93+
`${tc.getTypeName()}.updateById resolver doesn't receive new data in args.record`
94+
);
10795
}
10896

109-
return null;
97+
doc.set(recordData);
98+
99+
const validationErrors = await new Promise((resolve) => {
100+
doc.validate(null, null, resolve);
101+
});
102+
if (validationErrors) {
103+
throw new ValidationError(validationErrors as any);
104+
}
105+
106+
await doc.save();
107+
108+
return {
109+
record: doc,
110+
recordId: tc.getRecordIdFn()(doc),
111+
};
110112
}) as any,
111113
});
112114

src/resolvers/updateOne.ts

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ export default function updateOne<TSource = Document, TContext = any>(
3434
type: tc,
3535
description: 'Updated document',
3636
},
37-
errors: {
38-
type: '[ErrorInterface]',
39-
description: 'Errors that may occur',
40-
},
4137
});
4238
});
4339

@@ -92,43 +88,23 @@ export default function updateOne<TSource = Document, TContext = any>(
9288
doc = await resolveParams.beforeRecordMutate(doc, resolveParams);
9389
}
9490

91+
if (!doc) return null;
92+
9593
if (recordData) {
9694
doc.set(recordData);
9795

9896
const validations: ValidationsWithMessage | null = await validationsForDocument(doc);
99-
10097
if (validations) {
101-
if (!resolveParams?.projection?.error) {
102-
// if client does not request `errors` field we throw Exception on to level
103-
throw new ValidationError(validations);
104-
}
105-
return {
106-
record: null,
107-
recordId: null,
108-
error: {
109-
name: 'ValidationError',
110-
...validations,
111-
},
112-
};
113-
} else {
114-
await doc.save();
115-
return {
116-
record: doc,
117-
recordId: tc.getRecordIdFn()(doc as any),
118-
};
98+
throw new ValidationError(validations);
11999
}
120100

121101
await doc.save();
122102
}
123103

124-
if (doc) {
125-
return {
126-
record: doc,
127-
recordId: tc.getRecordIdFn()(doc as any),
128-
};
129-
}
130-
131-
return null;
104+
return {
105+
record: doc,
106+
recordId: tc.getRecordIdFn()(doc as any),
107+
};
132108
}) as any,
133109
});
134110

0 commit comments

Comments
 (0)