1
1
import { ArgumentsHost } from '@nestjs/common' ;
2
- import {
3
- GqlContextType as ContextKey ,
4
- GqlExecutionContext ,
5
- } from '@nestjs/graphql' ;
6
2
import { lowerCase , upperFirst } from 'lodash' ;
7
3
import type { ExclusivityViolationError } from '~/core/edgedb' ;
8
4
import { InputException } from './input.exception' ;
@@ -27,38 +23,14 @@ export class DuplicateException extends InputException {
27
23
28
24
// Attempt to add path prefix automatically to the property name, based
29
25
// on given GQL input.
30
- if ( context && context . getType < ContextKey > ( ) === 'graphql' ) {
31
- let gqlArgs = GqlExecutionContext . create ( context as any ) . getArgs ( ) ;
32
-
33
- // unwind single `input` argument, based on our own conventions
34
- if ( Object . keys ( gqlArgs ) . length === 1 && 'input' in gqlArgs ) {
35
- gqlArgs = gqlArgs . input ;
36
- }
37
-
38
- const flattened = flattenObject ( gqlArgs ) ;
39
- // Guess the correct path based on property name.
40
- // This kinda assumes the property name will be unique amongst all the input.
41
- const guessedPath = Object . keys ( flattened ) . find (
42
- ( path ) => property === path || path . endsWith ( '.' + property ) ,
43
- ) ;
44
- property = guessedPath ?? property ;
45
- }
26
+ // This kinda assumes the property name will be unique amongst all the input.
27
+ const guessedPath = Object . keys (
28
+ InputException . getFlattenInput ( context ) ,
29
+ ) . find ( ( path ) => property === path || path . endsWith ( '.' + property ) ) ;
30
+ property = guessedPath ?? property ;
46
31
47
32
const ex = new DuplicateException ( property , message , exception ) ;
48
33
ex . stack = exception . stack ;
49
34
return ex ;
50
35
}
51
36
}
52
-
53
- const flattenObject = ( obj : object , prefix = '' ) => {
54
- const result : Record < string , any > = { } ;
55
- for ( const [ key , value ] of Object . entries ( obj ) ) {
56
- if ( value && typeof value === 'object' && ! Array . isArray ( value ) ) {
57
- const nestedObj = flattenObject ( value , prefix + key + '.' ) ;
58
- Object . assign ( result , nestedObj ) ;
59
- } else {
60
- result [ prefix + key ] = value ;
61
- }
62
- }
63
- return result ;
64
- } ;
0 commit comments