Skip to content

Commit d240426

Browse files
author
Bret Hubbard
committed
add array handling
1 parent 5b4ade3 commit d240426

File tree

2 files changed

+138
-19
lines changed

2 files changed

+138
-19
lines changed

src/__tests__/jsonToGraphQLQuery.tests.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,116 @@ describe('jsonToGraphQL()', () => {
426426
);
427427
});
428428

429+
it('gets keys from an array instead of adding the index as a key', () => {
430+
const query = {
431+
query: {
432+
Posts: [{
433+
id: true,
434+
name: true,
435+
}],
436+
Lorem: {
437+
id: true
438+
},
439+
Ipsum: false,
440+
}
441+
};
442+
expect(jsonToGraphQLQuery(query)).to.equal(
443+
'query { Posts { id name } Lorem { id } }'
444+
);
445+
});
446+
447+
it('gets keys from an array instead of adding the index as a key and print pretty', () => {
448+
const query = {
449+
query: {
450+
Posts: [{
451+
id: true,
452+
name: true,
453+
}],
454+
Lorem: {
455+
id: true
456+
},
457+
Ipsum: false,
458+
}
459+
};
460+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
461+
`query {
462+
Posts {
463+
id
464+
name
465+
}
466+
Lorem {
467+
id
468+
}
469+
}`);
470+
});
471+
472+
it('handles empty arrays by adding the key but no values to the query', () => {
473+
const Posts: any[] = []
474+
const query = {
475+
query: {
476+
Posts,
477+
Lorem: {
478+
id: true
479+
},
480+
Ipsum: false,
481+
}
482+
};
483+
expect(jsonToGraphQLQuery(query)).to.equal(
484+
'query { Posts Lorem { id } }'
485+
);
486+
});
487+
488+
it('handles arrays of numbers by adding the key but no values to the query', () => {
489+
const query = {
490+
query: {
491+
Posts: [1, 2, 3],
492+
Lorem: {
493+
id: true
494+
},
495+
Ipsum: false,
496+
}
497+
};
498+
expect(jsonToGraphQLQuery(query)).to.equal(
499+
'query { Posts Lorem { id } }'
500+
);
501+
});
502+
503+
it('handles arrays of string by adding the key but no values to the query', () => {
504+
const Posts: any[] = [null]
505+
const query = {
506+
query: {
507+
Posts,
508+
Lorem: {
509+
id: true
510+
},
511+
Ipsum: false,
512+
}
513+
};
514+
expect(jsonToGraphQLQuery(query)).to.equal(
515+
'query { Posts Lorem { id } }'
516+
);
517+
});
518+
519+
it('handles arrays of string by adding the key but no values to the query and print pretty', () => {
520+
const Posts: any[] = []
521+
const query = {
522+
query: {
523+
Posts,
524+
Lorem: {
525+
id: true
526+
},
527+
Ipsum: false,
528+
}
529+
};
530+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
531+
`query {
532+
Posts
533+
Lorem {
534+
id
535+
}
536+
}`);
537+
});
538+
429539
it('ignores a field that exists in the initial object', () => {
430540
const query = {
431541
query: {

src/jsonToGraphQLQuery.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,56 +80,65 @@ function convertQuery(node: any, level: number, output: Array<[string, number]>,
8080
Object.keys(node)
8181
.filter((key) => filterNonConfigFields(key, options.ignoreFields))
8282
.forEach((key) => {
83+
let value = node[key]
84+
if (typeof value === 'object') {
85+
if (Array.isArray(value)) {
86+
if (value[0] && typeof value[0] === 'object') {
87+
value = value[0]
88+
}
89+
else {
90+
output.push([`${key}`, level])
91+
return
92+
}
93+
}
8394

84-
if (typeof node[key] === 'object') {
85-
86-
const fieldCount = Object.keys(node[key])
95+
const fieldCount = Object.keys(value)
8796
.filter((keyCount) => filterNonConfigFields(keyCount, options.ignoreFields)).length;
8897
const subFields = fieldCount > 0;
89-
const argsExist = typeof node[key].__args === 'object';
90-
const directivesExist = typeof node[key].__directives === 'object';
91-
const inlineFragmentsExist = typeof node[key].__on === 'object';
98+
const argsExist = typeof value.__args === 'object';
99+
const directivesExist = typeof value.__directives === 'object';
100+
const inlineFragmentsExist = typeof value.__on === 'object';
92101

93102
let token = `${key}`;
94103

95-
if (typeof node[key].__aliasFor === 'string') {
96-
token = `${token}: ${node[key].__aliasFor}`;
104+
if (typeof value.__aliasFor === 'string') {
105+
token = `${token}: ${value.__aliasFor}`;
97106
}
98107

99-
if (typeof node[key].__variables === 'object') {
100-
token = `${token} (${buildVariables(node[key].__variables)})`;
108+
if (typeof value.__variables === 'object') {
109+
token = `${token} (${buildVariables(value.__variables)})`;
101110
}
102111
else if (argsExist || directivesExist) {
103112
let argsStr: string;
104113
let dirsStr: string;
105114
if (directivesExist) {
106115
// TODO: Add support for multiple directives on one node.
107-
const numDirectives = Object.keys(node[key].__directives).length;
116+
const numDirectives = Object.keys(value.__directives).length;
108117
if (numDirectives > 1) {
109118
throw new Error(`Too many directives. The object/key ` +
110-
`'${Object.keys(node[key])[0]}' had ${numDirectives} directives, ` +
119+
`'${Object.keys(value)[0]}' had ${numDirectives} directives, ` +
111120
`but only 1 directive per object/key is supported at this time.`);
112121
}
113-
dirsStr = `@${buildDirectives(node[key].__directives)}`;
122+
dirsStr = `@${buildDirectives(value.__directives)}`;
114123
}
115124
if (argsExist) {
116-
argsStr = `(${buildArgs(node[key].__args)})`;
125+
argsStr = `(${buildArgs(value.__args)})`;
117126
}
118127
const spacer = directivesExist && argsExist ? ' ' : '';
119128
token = `${token} ${dirsStr ? dirsStr : ''}${spacer}${argsStr ? argsStr : ''}`;
120129
}
121130

122131
// DEPRECATED: Should be removed in version 2.0.0
123-
if (typeof node[key].__alias === 'string') {
124-
token = `${node[key].__alias}: ${token}`;
132+
if (typeof value.__alias === 'string') {
133+
token = `${value.__alias}: ${token}`;
125134
}
126135

127136
output.push([token + (subFields || inlineFragmentsExist ? ' {' : ''), level]);
128-
convertQuery(node[key], level + 1, output, options);
137+
convertQuery(value, level + 1, output, options);
129138

130139
if (inlineFragmentsExist) {
131140
const inlineFragments: Array<{ __typeName: string }>
132-
= node[key].__on instanceof Array ? node[key].__on : [node[key].__on];
141+
= value.__on instanceof Array ? value.__on : [value.__on];
133142
inlineFragments.forEach((inlineFragment) => {
134143
const name = inlineFragment.__typeName;
135144
output.push([`... on ${name} {`, level + 1]);
@@ -142,7 +151,7 @@ function convertQuery(node: any, level: number, output: Array<[string, number]>,
142151
output.push(['}', level]);
143152
}
144153

145-
} else if (options.includeFalsyKeys === true || node[key]) {
154+
} else if (options.includeFalsyKeys === true || value) {
146155
output.push([`${key}`, level]);
147156
}
148157
});

0 commit comments

Comments
 (0)