Skip to content

Commit 215954a

Browse files
committed
Added green tests
1 parent f193186 commit 215954a

File tree

3 files changed

+184
-18
lines changed

3 files changed

+184
-18
lines changed

README.md

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ const query = {
2626
post_date: true
2727
}
2828
}
29-
}
30-
const graphql_query = jsonToGraphQL(query);
29+
};
30+
const graphql_query = jsonToGraphQL(query, { pretty: true });
3131
```
3232

3333
Resulting `graphql_query`
3434

3535
```graphql
3636
query {
3737
Posts {
38-
id,
39-
title,
38+
id
39+
title
4040
post_date
4141
}
4242
}
@@ -53,23 +53,23 @@ const query = {
5353
__args: {
5454
orderBy: 'post_date',
5555
userId: 12
56-
}
56+
},
5757
id: true,
5858
title: true,
5959
post_date: true
6060
}
6161
}
62-
}
63-
const graphql_query = jsonToGraphQL(query);
62+
};
63+
const graphql_query = jsonToGraphQL(query, { pretty: true });
6464
```
6565

6666
Resulting `graphql_query`
6767

6868
```graphql
6969
query {
7070
Posts (orderBy: "post_date", userId: 12) {
71-
id,
72-
title,
71+
id
72+
title
7373
post_date
7474
}
7575
}
@@ -92,21 +92,20 @@ const query = {
9292
}
9393
}
9494
}
95-
}
96-
const graphql_query = jsonToGraphQL(query);
95+
};
96+
const graphql_query = jsonToGraphQL(query, { pretty: true });
9797
```
9898

9999
Resulting `graphql_query`
100100

101101
```graphql
102102
query {
103-
Posts (orderBy: "post_date", userId: 12) {
104-
id,
105-
name,
106-
post_date,
103+
Posts {
104+
id
105+
title
107106
comments {
108-
id,
109-
comment,
107+
id
108+
comment
110109
user
111110
}
112111
}

src/__tests__/jsonToGraphQL.tests.ts

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,117 @@ describe('jsonToGraphQL()', () => {
1616
}).to.throw('query object not specified');
1717
});
1818

19+
it('throws if object has no keys', () => {
20+
expect(() => {
21+
jsonToGraphQL({});
22+
}).to.throw('query object has no data');
23+
});
24+
25+
it('converts a simple query', () => {
26+
const query = {
27+
query: {
28+
Posts: {
29+
id: true,
30+
title: true,
31+
post_date: true
32+
}
33+
}
34+
};
35+
expect(jsonToGraphQL(query, { pretty: true })).to.equal(
36+
`query {
37+
Posts {
38+
id
39+
title
40+
post_date
41+
}
42+
}`);
43+
});
44+
45+
it('converts a query with arguments', () => {
46+
const query = {
47+
query: {
48+
Posts: {
49+
__args: {
50+
orderBy: 'post_date',
51+
userId: 12
52+
},
53+
id: true,
54+
title: true,
55+
post_date: true
56+
}
57+
}
58+
};
59+
expect(jsonToGraphQL(query, { pretty: true })).to.equal(
60+
`query {
61+
Posts (orderBy: "post_date", userId: 12) {
62+
id
63+
title
64+
post_date
65+
}
66+
}`);
67+
});
68+
69+
it('converts a query with nested objects', () => {
70+
const query = {
71+
query: {
72+
Posts: {
73+
id: true,
74+
title: true,
75+
comments: {
76+
id: true,
77+
comment: true,
78+
user: true
79+
}
80+
}
81+
}
82+
};
83+
expect(jsonToGraphQL(query, { pretty: true })).to.equal(
84+
`query {
85+
Posts {
86+
id
87+
title
88+
comments {
89+
id
90+
comment
91+
user
92+
}
93+
}
94+
}`);
95+
});
96+
97+
it('converts a query with nested objects and arguments', () => {
98+
const query = {
99+
query: {
100+
Posts: {
101+
__args: {
102+
arg1: 20,
103+
arg2: 'flibble'
104+
},
105+
id: true,
106+
title: true,
107+
comments: {
108+
__args: {
109+
offensiveOnly: true
110+
},
111+
id: true,
112+
comment: true,
113+
user: true
114+
}
115+
}
116+
}
117+
};
118+
expect(jsonToGraphQL(query, { pretty: true })).to.equal(
119+
`query {
120+
Posts (arg1: 20, arg2: "flibble") {
121+
id
122+
title
123+
comments (offensiveOnly: true) {
124+
id
125+
comment
126+
user
127+
}
128+
}
129+
}`);
130+
});
131+
19132
});

src/jsonToGraphQL.ts

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

2-
export function jsonToGraphQL(query: any) {
2+
function buildArgs(argsObj: any): string {
3+
const args = [];
4+
for (const argName in argsObj) {
5+
args.push(`${argName}: ${JSON.stringify(argsObj[argName])}`);
6+
}
7+
return args.join(', ');
8+
}
9+
10+
function getIndent(level: number): string {
11+
return Array((level * 4) + 1).join(' ');
12+
}
13+
14+
function convertQuery(node: any, level: number, output: Array<[string, number]>) {
15+
for (const key in node) {
16+
if (key != '__args') {
17+
if (typeof node[key] == 'object') {
18+
if (typeof node[key].__args == 'object') {
19+
output.push([`${key} (${buildArgs(node[key].__args)}) {`, level]);
20+
}
21+
else {
22+
output.push([`${key} {`, level]);
23+
}
24+
convertQuery(node[key], level + 1, output);
25+
output.push(['}', level]);
26+
}
27+
else {
28+
output.push([`${key}`, level]);
29+
}
30+
}
31+
}
32+
}
33+
34+
export interface IJsonToGraphQLOptions {
35+
pretty?: boolean;
36+
}
37+
38+
export function jsonToGraphQL(query: any, options: IJsonToGraphQLOptions = {}) {
339
if (!query || typeof query != 'object') {
440
throw new Error('query object not specified');
541
}
42+
if (Object.keys(query).length == 0) {
43+
throw new Error('query object has no data');
44+
}
45+
46+
const queryLines: Array<[string, number]> = [];
47+
convertQuery(query, 0, queryLines);
48+
49+
if (options.pretty) {
50+
let output = '';
51+
queryLines.forEach(([line, level]) => {
52+
if (output) { output += '\n'; }
53+
output += getIndent(level) + line;
54+
});
55+
return output;
56+
}
57+
else {
58+
return queryLines.join(' ');
59+
}
660
}

0 commit comments

Comments
 (0)