Skip to content

Commit 3baa881

Browse files
authored
Merge pull request #4 from wellguimaraes/master
Added alias support and made it possible to disable fields
2 parents 2865e96 + e72e38d commit 3baa881

File tree

5 files changed

+147
-27
lines changed

5 files changed

+147
-27
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ lib
33
*.lock
44
*.log
55
*.tgz
6-
package-lock.json
6+
package-lock.json
7+
.idea/

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,76 @@ query {
127127
}
128128
```
129129

130+
### Query with disabled fields
131+
132+
```typescript
133+
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
134+
135+
const query = {
136+
query: {
137+
Posts: {
138+
id: true,
139+
title: false,
140+
comments: {
141+
id: true,
142+
comment: false,
143+
user: true
144+
}
145+
},
146+
User: false
147+
}
148+
};
149+
const graphql_query = jsonToGraphQLQuery(query, { pretty: true });
150+
```
151+
152+
Resulting `graphql_query`
153+
154+
```graphql
155+
query {
156+
Posts {
157+
id
158+
comments {
159+
id
160+
user
161+
}
162+
}
163+
}
164+
```
165+
166+
### Use aliases
167+
168+
```typescript
169+
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
170+
171+
const query = {
172+
query: {
173+
Posts: {
174+
__alias: 'allPosts',
175+
id: true,
176+
comments: {
177+
id: true,
178+
comment: true
179+
}
180+
}
181+
}
182+
};
183+
const graphql_query = jsonToGraphQLQuery(query, { pretty: true });
184+
```
185+
186+
Resulting `graphql_query`
187+
188+
```graphql
189+
query {
190+
allPosts:Posts {
191+
id
192+
comments {
193+
id
194+
comment
195+
}
196+
}
197+
}
198+
```
199+
130200
## TO-DO List
131201

132202
* Fragments

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-to-graphql-query",
3-
"version": "1.1.3",
3+
"version": "1.3.0",
44
"main": "lib/index.js",
55
"license": "MIT",
66
"scripts": {

src/__tests__/jsonToGraphQLQuery.tests.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,42 @@ describe('jsonToGraphQL()', () => {
266266
'query { Posts (arg1: 20, arg2: "flibble") { id title } }'
267267
);
268268
});
269+
270+
it('uses aliases for fields', () => {
271+
const query = {
272+
query: {
273+
Posts: {
274+
__alias: 'lorem',
275+
__args: {
276+
arg1: 20,
277+
},
278+
id: true
279+
}
280+
}
281+
};
282+
expect(jsonToGraphQLQuery(query)).to.equal(
283+
'query { lorem:Posts (arg1: 20) { id } }'
284+
);
285+
});
286+
287+
it('does not include fields which value is false', () => {
288+
const query = {
289+
query: {
290+
Posts: {
291+
__args: {
292+
a: false
293+
},
294+
id: true,
295+
name: false
296+
},
297+
Lorem: {
298+
id: true
299+
},
300+
Ipsum: false
301+
}
302+
};
303+
expect(jsonToGraphQLQuery(query)).to.equal(
304+
'query { Posts (a: false) { id } Lorem { id } }'
305+
);
306+
});
269307
});

src/jsonToGraphQLQuery.ts

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function stringify(obj_from_json: any): string {
1616
.keys(obj_from_json)
1717
.map((key) => `${key}: ${stringify(obj_from_json[key])}`)
1818
.join(', ');
19+
1920
return `{${props}}`;
2021
}
2122

@@ -31,32 +32,42 @@ function getIndent(level: number): string {
3132
return Array((level * 4) + 1).join(' ');
3233
}
3334

34-
function convertQuery(node: any, level: number, output: Array<[string, number]>) {
35-
for (const key in node) {
36-
if (key != '__args') {
37-
if (typeof node[key] == 'object') {
38-
const fieldCount = Object.keys(node[key]).length;
39-
let token: string;
40-
let subFields: boolean;
41-
if (typeof node[key].__args == 'object') {
42-
token = `${key} (${buildArgs(node[key].__args)})`;
43-
subFields = fieldCount > 1;
44-
}
45-
else {
46-
token = `${key}`;
47-
subFields = fieldCount > 0;
48-
}
49-
output.push([token + (subFields ? ' {' : ''), level]);
50-
convertQuery(node[key], level + 1, output);
51-
if (subFields) {
52-
output.push(['}', level]);
53-
}
54-
}
55-
else {
56-
output.push([`${key}`, level]);
57-
}
35+
function filterNonConfigFields(it) {
36+
return it !== '__args' && it !== '__alias'
37+
}
38+
39+
function convertQuery(node: any, level: number, output: Array<[ string, number ]>) {
40+
Object.keys(node)
41+
.filter(filterNonConfigFields)
42+
.forEach(key => {
43+
if (typeof node[ key ] === 'object') {
44+
const fieldCount = Object.keys(node[ key ]).filter(filterNonConfigFields).length
45+
46+
let token: string
47+
let subFields: boolean
48+
49+
if (typeof node[ key ].__args === 'object') {
50+
token = `${key} (${buildArgs(node[ key ].__args)})`
51+
subFields = fieldCount > 1
52+
} else {
53+
token = `${key}`
54+
subFields = fieldCount > 0
5855
}
59-
}
56+
57+
if (typeof node[ key ].__alias === 'string') {
58+
token = `${node[ key ].__alias}:${token}`
59+
}
60+
61+
output.push([ token + (subFields ? ' {' : ''), level ])
62+
convertQuery(node[ key ], level + 1, output)
63+
64+
if (subFields) {
65+
output.push([ '}', level ])
66+
}
67+
} else if (node[ key ]) {
68+
output.push([ `${key}`, level ])
69+
}
70+
})
6071
}
6172

6273
export interface IJsonToGraphQLOptions {

0 commit comments

Comments
 (0)