Skip to content

Commit 52d0715

Browse files
committed
Add support for JSON arguments
1 parent 1c10224 commit 52d0715

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ Mainly useful for applications that need to generate graphql queries dynamically
1111
npm install json-to-graphql-query
1212
```
1313

14+
## Features
15+
16+
* Converts a JavaScript object to a GraphQL Query
17+
* Supports nested objects & arguments
18+
* Supports JSON input types for arguments (see arguments example below)
19+
1420
## Usage
1521

1622
**jsonToGraphQLQuery(** queryObject: object, options?: object **)**
@@ -56,8 +62,8 @@ const query = {
5662
query: {
5763
Posts: {
5864
__args: {
59-
orderBy: 'post_date',
60-
userId: 12
65+
where: { id: 2 }
66+
orderBy: 'post_date'
6167
},
6268
id: true,
6369
title: true,
@@ -72,7 +78,7 @@ Resulting `graphql_query`
7278

7379
```graphql
7480
query {
75-
Posts (orderBy: "post_date", userId: 12) {
81+
Posts (where: {id: 2}, orderBy: "post_date") {
7682
id
7783
title
7884
post_date

src/__tests__/jsonToGraphQLQuery.tests.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('jsonToGraphQL()', () => {
4242
}`);
4343
});
4444

45-
it('converts a query with arguments', () => {
45+
it('converts a query with simple arguments', () => {
4646
const query = {
4747
query: {
4848
Posts: {
@@ -66,6 +66,33 @@ describe('jsonToGraphQL()', () => {
6666
}`);
6767
});
6868

69+
it('converts a query with JSON arguments', () => {
70+
const query = {
71+
query: {
72+
Posts: {
73+
__args: {
74+
where: {
75+
published: true,
76+
rating: { _gt: 3 }
77+
},
78+
orderBy: 'post_date'
79+
},
80+
id: true,
81+
title: true,
82+
post_date: true
83+
}
84+
}
85+
};
86+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
87+
`query {
88+
Posts (where: {published: true, rating: {_gt: 3}}, orderBy: "post_date") {
89+
id
90+
title
91+
post_date
92+
}
93+
}`);
94+
});
95+
6996
it('converts a query with nested objects', () => {
7097
const query = {
7198
query: {

src/jsonToGraphQLQuery.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11

2+
function stringify(obj_from_json: any) {
3+
// Cheers to Derek: https://stackoverflow.com/questions/11233498/json-stringify-without-quotes-on-properties
4+
if (typeof obj_from_json !== 'object' || Array.isArray(obj_from_json)) {
5+
// not an object, stringify using native function
6+
return JSON.stringify(obj_from_json);
7+
}
8+
// Implements recursive object serialization according to JSON spec
9+
// but without quotes around the keys.
10+
const props: string = Object
11+
.keys(obj_from_json)
12+
.map((key) => `${key}: ${stringify(obj_from_json[key])}`)
13+
.join(', ');
14+
return `{${props}}`;
15+
}
16+
217
function buildArgs(argsObj: any): string {
318
const args = [];
419
for (const argName in argsObj) {
5-
args.push(`${argName}: ${JSON.stringify(argsObj[argName])}`);
20+
args.push(`${argName}: ${stringify(argsObj[argName])}`);
621
}
722
return args.join(', ');
823
}

0 commit comments

Comments
 (0)