@@ -3,10 +3,31 @@ const printName = (name) => {
3
3
}
4
4
5
5
const _query = property => {
6
+ // Special cases (nested values)
6
7
if ( property . name === 'allOf' ) {
7
- return 'allOf { ' + recursiveQuery ( property . type . ofType . fields ) + ' }'
8
+ if ( property . type . ofType ) {
9
+ const fields = recursiveQuery ( property . type . ofType . fields ) ;
10
+ return fields
11
+ ?' allOf { ' + fields + ' }'
12
+ : '' ;
13
+ }
14
+ const fields = recursiveQuery ( property . type . fields ) ;
15
+ return fields
16
+ ? ' allOf { ' + fields + ' }'
17
+ : ''
8
18
}
9
19
20
+ if ( property . name === 'oneOf' ) {
21
+ if ( property . type . ofType ) {
22
+ const fields = recursiveQuery ( property . type . ofType . fields ) ;
23
+ return fields ? ' oneOf { ' + fields + ' }' : '' ;
24
+ }
25
+ const fields = recursiveQuery ( property . type . fields ) ;
26
+ return fields ? ' oneOf { ' + fields + ' }' : '' ;
27
+ }
28
+
29
+ // Base cases
30
+
10
31
if ( ! property . type ) {
11
32
return printName ( property . name ) ;
12
33
}
@@ -16,18 +37,22 @@ const _query = property => {
16
37
return printName ( property . name ) ;
17
38
}
18
39
40
+ // Normal recursion
41
+
19
42
if ( property . type && property . type . fields ) {
20
43
return `${ printName ( property . name ) } { ${ recursiveQuery ( property . type . fields ) } }` ;
21
44
}
22
45
23
46
if ( property . type && property . type . typeOf && property . type . typeOf . fields ) {
24
47
return `${ printName ( property . name ) } { ${ recursiveQuery ( property . type . typeOf . fields ) } }` ;
25
48
}
26
- console . log ( 'no cases matched' , property ) ;
27
- return ;
49
+
50
+ // If we reach this point, there's a problem.
51
+ throw new Error ( 'No cases matched.' ) ;
28
52
}
29
53
30
54
const recursiveQuery = properties => {
55
+ if ( properties === null ) { return ; }
31
56
return properties . map ( property => _query ( property ) ) ;
32
57
}
33
58
0 commit comments