Skip to content

Commit f3a8775

Browse files
committed
Added support for args and directives on the same query.
1 parent b1cf928 commit f3a8775

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

src/__tests__/jsonToGraphQLQuery.tests.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,15 @@ describe('jsonToGraphQL()', () => {
167167
} as any;
168168
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
169169
`query {
170-
Posts @client (where: {id: 10}, orderBy: 'flibble') {
170+
Posts @client (where: {id: 10}, orderBy: "flibble") {
171171
id
172172
title
173173
post_date
174174
}
175175
}`);
176176
});
177177

178-
it('Converts a complex query with directives with no arguments', () => {
178+
it('converts a complex query with directives with no arguments', () => {
179179
const query = {
180180
query: {
181181
diet: {
@@ -215,8 +215,8 @@ describe('jsonToGraphQL()', () => {
215215
});
216216

217217
// TODO: Need this test still? How to handle variables unless $ declared explicitly?
218-
// it('Converts a JavaScript object into a valid query, including single directives ' +
219-
// 'with args, so long as any variables used are enclosed in a string with "$" included.', () => {
218+
// it('converts a JavaScript object into a valid query, including single directives ' +
219+
// 'with args, so long as any variables used are enclosed in a string with "$" included', () => {
220220
// interface ILooseObject { [key: string]: any; }
221221
// let input: ILooseObject = {
222222
// someOtherAbritraryKey: {
@@ -236,12 +236,12 @@ describe('jsonToGraphQL()', () => {
236236
// });
237237

238238
// TODO
239-
// it('Converts a JavaScript object into a valid query, including *multiple* directives ' +
240-
// 'with args, so long as any variables used are enclosed in a string with "$" included.', () => {
239+
// it('converts a JavaScript object into a valid query, including *multiple* directives ' +
240+
// 'with args, so long as any variables used are enclosed in a string with "$" included', () => {
241241
// });
242242

243243
// TODO
244-
// it('Creates a query, stripping/ignoring certain, specified keys.', () => {
244+
// it('creates a query, stripping/ignoring certain, specified keys', () => {
245245
// // Example usage: jsonToGraphqlQuery(preInput, { keysToStrip: ['__typename'] });
246246
// });
247247

src/jsonToGraphQLQuery.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,32 @@ function convertQuery(node: any, level: number, output: Array<[ string, number ]
8282
const fieldCount = Object.keys(node[key])
8383
.filter((keyCount) => filterNonConfigFields(keyCount, options.ignoreFields)).length;
8484
const subFields = fieldCount > 0;
85+
const argsExist = typeof node[key].__args === 'object';
86+
const directivesExist = typeof node[key].__directives === 'object';
8587
let token: string;
86-
87-
if (typeof node[key].__args === 'object') {
88-
token = `${key} (${buildArgs(node[key].__args)})`;
89-
}
90-
else if (typeof node[key].__variables === 'object') {
88+
if (typeof node[key].__variables === 'object') {
9189
token = `${key} (${buildVariables(node[key].__variables)})`;
9290
}
93-
else if (typeof node[key].__directives === 'object') {
94-
// TODO: Add support for multiple directives on one node.
95-
const numDirectives = Object.keys(node[key].__directives).length;
96-
if (numDirectives > 1) {
97-
throw new Error(`Too many directives. The object/key ` +
98-
`'${Object.keys(node[key])[0]}' had ${numDirectives} directives, ` +
99-
`but only 1 directive per object/key is supported at this time.`);
91+
else if (argsExist || directivesExist) {
92+
let argsStr: string;
93+
let dirsStr: string;
94+
if (directivesExist) {
95+
// TODO: Add support for multiple directives on one node. Then condense blocks into terniary lines.
96+
// const directives = Object.keys(node[key].__directives);
97+
const numDirectives = Object.keys(node[key].__directives).length;
98+
if (numDirectives > 1) {
99+
throw new Error(`Too many directives. The object/key ` +
100+
`'${Object.keys(node[key])[0]}' had ${numDirectives} directives, ` +
101+
`but only 1 directive per object/key is supported at this time.`);
102+
}
103+
// directives.map(((x) => { dirsStr += ` @${buildDirectives(node[key].__directives)}`; }));
104+
dirsStr = `@${buildDirectives(node[key].__directives)}`;
105+
}
106+
if (argsExist) {
107+
argsStr = `(${buildArgs(node[key].__args)})`;
100108
}
101-
token = `${key} @${buildDirectives(node[key].__directives)}`;
109+
const spacer = directivesExist && argsExist ? ' ' : '';
110+
token = `${key} ${dirsStr ? dirsStr : ''}${spacer}${argsStr ? argsStr : ''}`;
102111
}
103112
else {
104113
token = `${key}`;

0 commit comments

Comments
 (0)