-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetInputObjectType.js
More file actions
63 lines (49 loc) · 1.65 KB
/
getInputObjectType.js
File metadata and controls
63 lines (49 loc) · 1.65 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
54
55
56
57
58
59
60
61
62
63
import {
GraphQLInputObjectType,
GraphQLObjectType,
GraphQLList,
GraphQLScalarType,
GraphQLEnumType,
GraphQLUnionType,
GraphQLID
} from './'
import getConfig from './getConfig'
import Scalar from './Scalar'
/**
* get an GraphQLInputObjectType from GraphQLObjectType
* @param {(GraphQLObjectType|GraphQLList|GraphQLScalarType|GraphQLEnumType|GraphQLUnionType)} ObjectType Type to convert
* @param {Boolean} deep
* @returns Input type
*/
export default function getInputObjectType(ObjectType, deep = false) {
if(!deep && ObjectType._typeInput)
return ObjectType._typeInput
switch (ObjectType.constructor.name) {
case 'GraphQLObjectType':
let _fields = getConfig(ObjectType)
if(deep && _fields.id && !ObjectType.entityLess)
return GraphQLID
return ObjectType._typeInput || (ObjectType._typeInput = new GraphQLInputObjectType({
name: `${ObjectType.name}Input`,
fields() {
let fields = {}
Object.keys(_fields).forEach(
key =>
fields[key] = {
type: getInputObjectType(_fields[key].type, true)
}
)
return fields
}
}))
case 'GraphQLScalarType':
case 'GraphQLEnumType':
return ObjectType
case 'GraphQLUnionType':
return Scalar
case 'GraphQLList':
return new GraphQLList(
getInputObjectType(ObjectType.ofType, true)
)
}
}