Skip to content

Commit fcfcec7

Browse files
author
Bret Hubbard
committed
get first object in array for building query
1 parent d240426 commit fcfcec7

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/__tests__/jsonToGraphQLQuery.tests.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,36 @@ describe('jsonToGraphQL()', () => {
500500
);
501501
});
502502

503+
it('handles arrays of strings by adding the key but no values to the query', () => {
504+
const query = {
505+
query: {
506+
Posts: ['test 1', 'test 2', 'test 3'],
507+
Lorem: {
508+
id: true
509+
},
510+
Ipsum: false,
511+
}
512+
};
513+
expect(jsonToGraphQLQuery(query)).to.equal(
514+
'query { Posts Lorem { id } }'
515+
);
516+
});
517+
518+
it('handles arrays of mixed types by taking the first object of the array', () => {
519+
const query = {
520+
query: {
521+
Posts: [1, null, { id: true, name: true }],
522+
Lorem: {
523+
id: true
524+
},
525+
Ipsum: false,
526+
}
527+
};
528+
expect(jsonToGraphQLQuery(query)).to.equal(
529+
'query { Posts { id name } Lorem { id } }'
530+
);
531+
});
532+
503533
it('handles arrays of string by adding the key but no values to the query', () => {
504534
const Posts: any[] = [null]
505535
const query = {

src/jsonToGraphQLQuery.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,8 @@ function convertQuery(node: any, level: number, output: Array<[string, number]>,
8383
let value = node[key]
8484
if (typeof value === 'object') {
8585
if (Array.isArray(value)) {
86-
if (value[0] && typeof value[0] === 'object') {
87-
value = value[0]
88-
}
89-
else {
86+
value = value.find(item => item && typeof item === 'object')
87+
if (!value) {
9088
output.push([`${key}`, level])
9189
return
9290
}

0 commit comments

Comments
 (0)