Skip to content

Commit 6379676

Browse files
committed
Fix oneOf logic case
1 parent 55a2c22 commit 6379676

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

gatsby-node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const specs = require("./static/api/docs/v4/spec.json");
77
const crypto = require("crypto");
88
const parser = new JsonSchemaRefParser();
99

10-
const { rawQuery } = require("./generateQuery.js");
10+
const { rawQuery, recursiveQuery } = require("./generateQuery.js");
1111

1212
exports.sourceNodes = async ({ actions }) => {
1313
const { createNode, createTypes } = actions;
@@ -516,7 +516,7 @@ exports.createPages = async ({ actions, graphql }) => {
516516
const props = result.data.__type.fields;
517517
const file = fs.createWriteStream(fileName);
518518

519-
const query = rawQuery(props)
519+
const query = recursiveQuery(props)
520520
.toString()
521521
.replace(/\,/g, "");
522522

generateQuery.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,31 @@ const printName = (name) => {
33
}
44

55
const _query = property => {
6+
// Special cases (nested values)
67
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+
: ''
818
}
919

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+
1031
if (!property.type) {
1132
return printName(property.name);
1233
}
@@ -16,18 +37,22 @@ const _query = property => {
1637
return printName(property.name);
1738
}
1839

40+
// Normal recursion
41+
1942
if (property.type && property.type.fields) {
2043
return `${printName(property.name)} { ${recursiveQuery(property.type.fields)} }`;
2144
}
2245

2346
if (property.type && property.type.typeOf && property.type.typeOf.fields) {
2447
return `${printName(property.name)} { ${recursiveQuery(property.type.typeOf.fields)} }`;
2548
}
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.');
2852
}
2953

3054
const recursiveQuery = properties => {
55+
if (properties === null) { return; }
3156
return properties.map(property => _query(property));
3257
}
3358

new.txt

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)