Skip to content

Commit 05d1ec8

Browse files
committed
Refactor
1 parent 6379676 commit 05d1ec8

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

generateQuery.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@ const printName = (name) => {
22
return ' ' + name;
33
}
44

5+
const handleNested = (fieldsPath, fieldType) => {
6+
const fields = recursiveQuery(fieldsPath);
7+
return fields
8+
? ` ${fieldType} { ${fields} }`
9+
: '';
10+
}
11+
512
const _query = property => {
613
// Special cases (nested values)
714
if (property.name === 'allOf') {
815
if (property.type.ofType) {
9-
const fields = recursiveQuery(property.type.ofType.fields);
10-
return fields
11-
?' allOf { ' + fields + ' }'
12-
: '';
16+
return handleNested(property.type.ofType.fields, 'allOf');
1317
}
14-
const fields = recursiveQuery(property.type.fields);
15-
return fields
16-
? ' allOf { ' + fields + ' }'
17-
: ''
18+
19+
return handleNested(property.type.fields, 'allOf');
1820
}
1921

2022
if (property.name === 'oneOf') {
2123
if (property.type.ofType) {
22-
const fields = recursiveQuery(property.type.ofType.fields);
23-
return fields ? ' oneOf { ' + fields + ' }' : '';
24+
return handleNested(property.type.ofType.fields, 'oneOf');
2425
}
25-
const fields = recursiveQuery(property.type.fields);
26-
return fields ? ' oneOf { ' + fields + ' }' : '';
26+
return handleNested(property.type.fields, 'oneOf');
2727
}
2828

2929
// Base cases
@@ -46,7 +46,7 @@ const _query = property => {
4646
if (property.type && property.type.typeOf && property.type.typeOf.fields) {
4747
return `${printName(property.name)} { ${recursiveQuery(property.type.typeOf.fields)} }`;
4848
}
49-
49+
5050
// If we reach this point, there's a problem.
5151
throw new Error('No cases matched.');
5252
}

0 commit comments

Comments
 (0)