Skip to content

Commit e05fced

Browse files
committed
ads support for query variables
1 parent 23fdc8f commit e05fced

File tree

5 files changed

+70
-7
lines changed

5 files changed

+70
-7
lines changed

src/__tests__/jsonToGraphQLQuery.tests.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import { expect } from 'chai';
3-
import { jsonToGraphQLQuery, EnumType } from '../';
3+
import { jsonToGraphQLQuery, EnumType, VariableType } from '../';
44

55
describe('jsonToGraphQL()', () => {
66

@@ -305,3 +305,41 @@ describe('jsonToGraphQL()', () => {
305305
);
306306
});
307307
});
308+
309+
it('converts a query variables', () => {
310+
const query = {
311+
query: {
312+
__variables: {
313+
variableName: 'String!'
314+
},
315+
Posts: {
316+
__args: {
317+
arg1: 20,
318+
arg2: new VariableType('variableName')
319+
},
320+
id: true,
321+
title: true,
322+
comments: {
323+
__args: {
324+
offensiveOnly: true
325+
},
326+
id: true,
327+
comment: true,
328+
user: true
329+
}
330+
}
331+
}
332+
};
333+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
334+
`query ($variableName: String!) {
335+
Posts (arg1: 20, arg2: $variableName) {
336+
id
337+
title
338+
comments (offensiveOnly: true) {
339+
id
340+
comment
341+
user
342+
}
343+
}
344+
}`);
345+
});

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
21
export * from './jsonToGraphQLQuery';
2+
export {EnumType} from './types/EnumType';
3+
export {VariableType} from './types/VariableType';

src/jsonToGraphQLQuery.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
import {EnumType} from './types/EnumType';
2+
import {VariableType} from './types/VariableType';
3+
14
function stringify(obj_from_json: any): string {
25
if (obj_from_json instanceof EnumType) {
36
return obj_from_json.value;
47
}
8+
// variables should be prefixed with dollar sign and not e quoted
9+
else if (obj_from_json instanceof VariableType) {
10+
return `$${obj_from_json.value}`;
11+
}
512
// Cheers to Derek: https://stackoverflow.com/questions/11233498/json-stringify-without-quotes-on-properties
613
else if (typeof obj_from_json !== 'object' || obj_from_json === null) {
714
// not an object, stringify using native function
@@ -28,12 +35,20 @@ function buildArgs(argsObj: any): string {
2835
return args.join(', ');
2936
}
3037

38+
function buildVariables(varsObj: any): string {
39+
const args = [];
40+
for (const varName in varsObj) {
41+
args.push(`$${varName}: ${varsObj[varName]}`);
42+
}
43+
return args.join(', ');
44+
}
45+
3146
function getIndent(level: number): string {
3247
return Array((level * 4) + 1).join(' ');
3348
}
3449

3550
function filterNonConfigFields(fieldName: string) {
36-
return fieldName !== '__args' && fieldName !== '__alias';
51+
return fieldName !== '__args' && fieldName !== '__alias' && fieldName !== '__variables';
3752
}
3853

3954
function convertQuery(node: any, level: number, output: Array<[ string, number ]>) {
@@ -48,6 +63,9 @@ function convertQuery(node: any, level: number, output: Array<[ string, number ]
4863
if (typeof node[key].__args === 'object') {
4964
token = `${key} (${buildArgs(node[key].__args)})`;
5065
}
66+
else if (typeof node[key].__variables === 'object') {
67+
token = `${key} (${buildVariables(node[key].__variables)})`;
68+
}
5169
else {
5270
token = `${key}`;
5371
}
@@ -96,7 +114,3 @@ export function jsonToGraphQLQuery(query: any, options: IJsonToGraphQLOptions =
96114
});
97115
return output;
98116
}
99-
100-
export class EnumType {
101-
constructor(public value: string) {}
102-
}

src/types/EnumType.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class EnumType {
2+
constructor(public value: string) {}
3+
}
4+
5+
export {EnumType};

src/types/VariableType.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class VariableType {
2+
constructor(public value: string) {}
3+
}
4+
5+
export {VariableType};

0 commit comments

Comments
 (0)