-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetErrorObjectType.js
More file actions
53 lines (40 loc) · 1.11 KB
/
Copy pathgetErrorObjectType.js
File metadata and controls
53 lines (40 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import {
GraphQLObjectType,
GraphQLList,
GraphQLString
} from 'graphql'
const $type = Symbol('type')
export function getErrorObjectType(Type, moreFields = {}, deep = false) {
if (!deep && Type[$type])
return Type[$type]
switch (Type.constructor.name) {
case 'GraphQLObjectType':
let _fields = Type.getFields()
if (deep && _fields.id && !Type.entityLess)
return GraphQLString
return Type[$type] = Type[$type] || new GraphQLObjectType({
name: `${Type.name}Error`,
fields() {
let fields = {}
Object.keys(_fields).forEach(
key =>
fields[key] = {
type: getErrorObjectType(_fields[key].type, moreFields[key] || {}, true)
}
)
return Object.assign(
fields,
moreFields
)
}
})
case 'GraphQLScalarType':
case 'GraphQLEnumType':
case 'GraphQLUnionType':
return GraphQLString
case 'GraphQLList':
return Type[$type] = new GraphQLList(
getErrorObjectType(Type.ofType, moreFields, true)
)
}
}