Skip to content

Commit 8c2213e

Browse files
committed
Update README and small fixes
1 parent 3baa881 commit 8c2213e

File tree

4 files changed

+34
-35
lines changed

4 files changed

+34
-35
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ npm install json-to-graphql-query
1313

1414
## Features
1515

16-
* Converts a JavaScript object to a GraphQL Query
16+
* Converts a JavaScript object to a GraphQL Query string
1717
* Full support for nested query nodes and arguments
18-
* Support for Enum values
19-
* Supports JSON input types for arguments
18+
* Support for aliases via `__alias`
19+
* Support for input arguments via `__args`
20+
* Support for Enum values via `EnumType`
2021

2122
See usage examples below :)
2223

@@ -163,7 +164,7 @@ query {
163164
}
164165
```
165166

166-
### Use aliases
167+
### Using aliases
167168

168169
```typescript
169170
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
@@ -187,7 +188,7 @@ Resulting `graphql_query`
187188

188189
```graphql
189190
query {
190-
allPosts:Posts {
191+
allPosts: Posts {
191192
id
192193
comments {
193194
id

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.3.0",
3+
"version": "1.2.0",
44
"main": "lib/index.js",
55
"license": "MIT",
66
"scripts": {

src/__tests__/jsonToGraphQLQuery.tests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ describe('jsonToGraphQL()', () => {
280280
}
281281
};
282282
expect(jsonToGraphQLQuery(query)).to.equal(
283-
'query { lorem:Posts (arg1: 20) { id } }'
283+
'query { lorem: Posts (arg1: 20) { id } }'
284284
);
285285
});
286286

src/jsonToGraphQLQuery.ts

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,42 +32,40 @@ function getIndent(level: number): string {
3232
return Array((level * 4) + 1).join(' ');
3333
}
3434

35-
function filterNonConfigFields(it) {
36-
return it !== '__args' && it !== '__alias'
35+
function filterNonConfigFields(fieldName: string) {
36+
return fieldName !== '__args' && fieldName !== '__alias';
3737
}
3838

3939
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
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+
const subFields = fieldCount > 0;
46+
let token: string;
4547

46-
let token: string
47-
let subFields: boolean
48+
if (typeof node[key].__args === 'object') {
49+
token = `${key} (${buildArgs(node[key].__args)})`;
50+
}
51+
else {
52+
token = `${key}`;
53+
}
4854

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
55-
}
56-
57-
if (typeof node[ key ].__alias === 'string') {
58-
token = `${node[ key ].__alias}:${token}`
59-
}
55+
if (typeof node[key].__alias === 'string') {
56+
token = `${node[key].__alias}: ${token}`;
57+
}
6058

61-
output.push([ token + (subFields ? ' {' : ''), level ])
62-
convertQuery(node[ key ], level + 1, output)
59+
output.push([ token + (fieldCount > 0 ? ' {' : ''), level ]);
60+
convertQuery(node[key], level + 1, output);
6361

64-
if (subFields) {
65-
output.push([ '}', level ])
62+
if (subFields) {
63+
output.push([ '}', level ]);
64+
}
65+
} else if (node[key]) {
66+
output.push([ `${key}`, level ]);
6667
}
67-
} else if (node[ key ]) {
68-
output.push([ `${key}`, level ])
69-
}
70-
})
68+
});
7169
}
7270

7371
export interface IJsonToGraphQLOptions {

0 commit comments

Comments
 (0)