Skip to content

Commit a96e1c8

Browse files
committed
Fix non-pretty mode
1 parent d368b2e commit a96e1c8

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/__tests__/jsonToGraphQLQuery.tests.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,21 @@ describe('jsonToGraphQL()', () => {
129129
}`);
130130
});
131131

132+
it('works with pretty mode turned off', () => {
133+
const query = {
134+
query: {
135+
Posts: {
136+
__args: {
137+
arg1: 20,
138+
arg2: 'flibble'
139+
},
140+
id: true,
141+
title: true
142+
}
143+
}
144+
};
145+
expect(jsonToGraphQLQuery(query)).to.equal(
146+
'query { Posts (arg1: 20, arg2: "flibble") { id title } }'
147+
);
148+
});
132149
});

src/jsonToGraphQLQuery.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,16 @@ export function jsonToGraphQLQuery(query: any, options: IJsonToGraphQLOptions =
4646
const queryLines: Array<[string, number]> = [];
4747
convertQuery(query, 0, queryLines);
4848

49-
if (options.pretty) {
50-
let output = '';
51-
queryLines.forEach(([line, level]) => {
49+
let output = '';
50+
queryLines.forEach(([line, level]) => {
51+
if (options.pretty) {
5252
if (output) { output += '\n'; }
5353
output += getIndent(level) + line;
54-
});
55-
return output;
56-
}
57-
else {
58-
return queryLines.join(' ');
59-
}
54+
}
55+
else {
56+
if (output) { output += ' '; }
57+
output += line;
58+
}
59+
});
60+
return output;
6061
}

0 commit comments

Comments
 (0)