Skip to content

Commit 38628dc

Browse files
committed
Flatten errors and use an error interface
1 parent f5fa838 commit 38628dc

File tree

5 files changed

+95
-34
lines changed

5 files changed

+95
-34
lines changed

src/resolvers/createOne.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Resolver, ObjectTypeComposer } from 'graphql-compose';
22
import type { Model, Document } from 'mongoose';
3-
import { getOrCreateErrorPayload } from '../utils/getOrCreateErrorPayload';
3+
import { getOrCreateErrorInterface } from '../utils/getOrCreateErrorInterface';
44
import { recordHelperArgs } from './helpers';
55
import type { ExtendedResolveParams, GenResolverOpts } from './index';
66

@@ -30,7 +30,7 @@ export default function createOne<TSource = Document, TContext = any>(
3030
}
3131
}
3232

33-
getOrCreateErrorPayload(tc);
33+
getOrCreateErrorInterface(tc);
3434

3535
const outputTypeName = `CreateOne${tc.getTypeName()}Payload`;
3636
const outputType = tc.schemaComposer.getOrCreateOTC(outputTypeName, (t) => {
@@ -44,8 +44,8 @@ export default function createOne<TSource = Document, TContext = any>(
4444
description: 'Created document',
4545
},
4646
errors: {
47-
type: '[ErrorPayload]',
48-
description: 'Errors that may occur, typically validations',
47+
type: '[ErrorInterface]',
48+
description: 'Errors that may occur',
4949
},
5050
});
5151
});
@@ -81,16 +81,15 @@ export default function createOne<TSource = Document, TContext = any>(
8181
}
8282

8383
const validationErrors = doc.validateSync();
84-
let errors: {
84+
const errors: {
8585
path: string;
86-
messages: string[];
87-
}[];
86+
message: string;
87+
}[] = [];
8888
if (validationErrors && validationErrors.errors) {
89-
errors = [];
9089
Object.keys(validationErrors.errors).forEach((key) => {
9190
errors.push({
9291
path: key,
93-
messages: [validationErrors.errors[key].message],
92+
message: validationErrors.errors[key].message,
9493
});
9594
});
9695
return {

src/resolvers/updateById.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Resolver, ObjectTypeComposer } from 'graphql-compose';
22
import type { Model, Document } from 'mongoose';
3-
import { getOrCreateErrorPayload } from '../utils/getOrCreateErrorPayload';
3+
import { getOrCreateErrorInterface } from '../utils/getOrCreateErrorInterface';
44
import { recordHelperArgs } from './helpers/record';
55
import findById from './findById';
66

@@ -21,9 +21,9 @@ export default function updateById<TSource = Document, TContext = any>(
2121
);
2222
}
2323

24-
const findByIdResolver = findById(model, tc);
24+
getOrCreateErrorInterface(tc);
2525

26-
getOrCreateErrorPayload(tc);
26+
const findByIdResolver = findById(model, tc);
2727

2828
const outputTypeName = `UpdateById${tc.getTypeName()}Payload`;
2929
const outputType = tc.schemaComposer.getOrCreateOTC(outputTypeName, (t) => {
@@ -37,8 +37,8 @@ export default function updateById<TSource = Document, TContext = any>(
3737
description: 'Updated document',
3838
},
3939
errors: {
40-
type: '[ErrorPayload]',
41-
description: 'Errors that may occur, typically validations',
40+
type: '[ErrorInterface]',
41+
description: 'Errors that may occur',
4242
},
4343
});
4444
});

src/resolvers/updateOne.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Resolver, ObjectTypeComposer } from 'graphql-compose';
22
import type { Model, Document } from 'mongoose';
33
import type { ExtendedResolveParams, GenResolverOpts } from './index';
4-
import { getOrCreateErrorPayload } from '../utils/getOrCreateErrorPayload';
4+
import { getOrCreateErrorInterface } from '../utils/getOrCreateErrorInterface';
55
import { skipHelperArgs, recordHelperArgs, filterHelperArgs, sortHelperArgs } from './helpers';
66
import findOne from './findOne';
77

@@ -21,7 +21,7 @@ export default function updateOne<TSource = Document, TContext = any>(
2121

2222
const findOneResolver = findOne(model, tc, opts);
2323

24-
getOrCreateErrorPayload(tc);
24+
getOrCreateErrorInterface(tc);
2525

2626
const outputTypeName = `UpdateOne${tc.getTypeName()}Payload`;
2727
const outputType = tc.schemaComposer.getOrCreateOTC(outputTypeName, (t) => {
@@ -35,8 +35,8 @@ export default function updateOne<TSource = Document, TContext = any>(
3535
description: 'Updated document',
3636
},
3737
errors: {
38-
type: '[ErrorPayload]',
39-
description: 'Errors that may occur, typically validations',
38+
type: '[ErrorInterface]',
39+
description: 'Errors that may occur',
4040
},
4141
});
4242
});
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import type { ObjectTypeComposer, InterfaceTypeComposer } from 'graphql-compose';
2+
3+
export function getOrCreateErrorInterface(tc: ObjectTypeComposer): InterfaceTypeComposer {
4+
const errorInterface: InterfaceTypeComposer = tc.schemaComposer.getOrCreateIFTC(
5+
'ErrorInterface',
6+
(iftc: InterfaceTypeComposer) => {
7+
iftc.addFields({
8+
message: {
9+
description: 'Generic error message',
10+
type: 'String',
11+
},
12+
});
13+
}
14+
);
15+
16+
const validationErrorOTC: ObjectTypeComposer = tc.schemaComposer.getOrCreateOTC(
17+
'ValidationError',
18+
(otc: ObjectTypeComposer) => {
19+
otc.addFields({
20+
message: {
21+
description: 'Validation error message',
22+
type: 'String',
23+
},
24+
path: {
25+
description: 'Source of the validation error from the model path',
26+
type: 'String',
27+
},
28+
});
29+
otc.addInterface(errorInterface);
30+
}
31+
);
32+
tc.schemaComposer.addSchemaMustHaveType(validationErrorOTC);
33+
34+
const runtimeErrorOTC: ObjectTypeComposer = tc.schemaComposer.getOrCreateOTC(
35+
'RuntimeError',
36+
(otc: ObjectTypeComposer) => {
37+
otc.addFields({
38+
message: {
39+
description: 'Runtime error message',
40+
type: 'String',
41+
},
42+
});
43+
otc.addInterface(errorInterface);
44+
}
45+
);
46+
tc.schemaComposer.addSchemaMustHaveType(runtimeErrorOTC);
47+
48+
const mongoErrorOTC: ObjectTypeComposer = tc.schemaComposer.getOrCreateOTC(
49+
'MongoError',
50+
(otc: ObjectTypeComposer) => {
51+
otc.addFields({
52+
message: {
53+
description: 'MongoDB error message',
54+
type: 'String',
55+
},
56+
code: {
57+
description: 'MongoDB error code',
58+
type: 'String',
59+
},
60+
});
61+
otc.addInterface(errorInterface);
62+
}
63+
);
64+
tc.schemaComposer.addSchemaMustHaveType(mongoErrorOTC);
65+
66+
const resolveType = (value: any) => {
67+
if (value) {
68+
if (value.path) return 'ValidationError';
69+
else if (value.code) return 'MongoError';
70+
else return 'RuntimeError';
71+
}
72+
return null;
73+
};
74+
75+
errorInterface.setResolveType(resolveType);
76+
77+
return errorInterface;
78+
}

src/utils/getOrCreateErrorPayload.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)