Skip to content

Commit 35ff42e

Browse files
authored
Merge pull request #51 from postmanlabs/feature/fix-obj-typeerror
Fixed issue where certain GQL definitions were failing with TypeError.
2 parents f50b7e9 + 0b4b305 commit 35ff42e

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

lib/assets/gql-generator.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ var graphql = require('graphql'),
1818
* @param dict dictionary of arguments
1919
*/
2020
getArgsToVarsStr = (dict) => {
21+
if (typeof dict !== 'object') {
22+
return '';
23+
}
24+
2125
return Object.entries(dict)
2226
.map(([varName, arg]) => { return `${arg.name}: $${varName}`; })
2327
.join(', ');
@@ -102,6 +106,10 @@ var graphql = require('graphql'),
102106
* @param dict dictionary of arguments
103107
*/
104108
getVarsToTypesStr = (dict) => {
109+
if (typeof dict !== 'object') {
110+
return '';
111+
}
112+
105113
return Object.entries(dict)
106114
.map(([varName, arg]) => {
107115
return `$${varName}: ${arg.type}`;
@@ -151,7 +159,7 @@ function resolveVariableType (type, gqlSchema, stack = 0, stackLimit = 4) {
151159

152160
if (graphql.isInputObjectType(argType)) {
153161
fields = argType.getFields();
154-
Object.keys(fields).forEach((field) => {
162+
typeof fields === 'object' && Object.keys(fields).forEach((field) => {
155163
if (fields[field].type === type) {
156164
fieldObj[field] = '<Same as ' + type + '>';
157165
}
@@ -241,7 +249,7 @@ module.exports = {
241249
}
242250
crossReferenceKeyList[crossReferenceKey] = true;
243251

244-
let childKeys = Object.keys(curType.getFields());
252+
let childKeys = Object.keys(curType.getFields() || {});
245253
childQuery = childKeys
246254
.filter((fieldName) => {
247255
/* Exclude deprecated fields */
@@ -282,7 +290,7 @@ module.exports = {
282290
for (let i = 0, len = types.length; i < len; i++) {
283291
const valueTypeName = types[i],
284292
valueType = gqlSchema.getType(valueTypeName),
285-
unionChildQuery = Object.keys(valueType.getFields())
293+
unionChildQuery = Object.keys(valueType.getFields() || {})
286294
.map((cur) => {
287295
// Don't genrate query fields that have self referencing
288296
if (cur === curName) {
@@ -330,7 +338,7 @@ module.exports = {
330338
console.log('[gqlg warning]:', 'description is required');
331339
}
332340

333-
Object.keys(obj).forEach((type) => {
341+
typeof obj === 'object' && Object.keys(obj).forEach((type) => {
334342

335343
let field,
336344
newDescription;
@@ -358,9 +366,12 @@ module.exports = {
358366

359367
/* Generate variables Object from argumentDict */
360368
var variables = {};
361-
Object.entries(queryResult.argumentsDict).map(([varName, arg]) => {
362-
variables[varName] = resolveVariableType(arg.type, gqlSchema, 0, stackLimit);
363-
});
369+
370+
if (typeof queryResult.argumentsDict === 'object') {
371+
Object.entries(queryResult.argumentsDict).map(([varName, arg]) => {
372+
variables[varName] = resolveVariableType(arg.type, gqlSchema, 0, stackLimit);
373+
});
374+
}
364375

365376
let query = queryResult.queryStr;
366377
// here the `description` is used to construct the actual queries

test/unit/gql-generator.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,21 @@ describe('gql-generator tests', function () {
2222

2323
done();
2424
});
25+
26+
it('should not throw type error for some of elements that are defined non-object in GQL schema', function (done) {
27+
const data = validSchemaSDL,
28+
gqlSchemaObj = graphql.buildSchema(data);
29+
30+
// Set specific property as null to test behaviour for non defined nodes.
31+
gqlSchemaObj._mutationType._fields = undefined;
32+
33+
try {
34+
schemaToQuery(gqlSchemaObj, { depth: 5 });
35+
}
36+
catch (e) {
37+
expect(e).to.be.undefined;
38+
}
39+
40+
done();
41+
});
2542
});

0 commit comments

Comments
 (0)