File tree Expand file tree Collapse file tree 3 files changed +53
-5
lines changed Expand file tree Collapse file tree 3 files changed +53
-5
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,12 @@ Mainly useful for applications that need to generate graphql queries dynamically
11
11
npm install json-to-graphql-query
12
12
```
13
13
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
+
14
20
## Usage
15
21
16
22
** jsonToGraphQLQuery(** queryObject: object, options?: object ** )**
@@ -56,8 +62,8 @@ const query = {
56
62
query: {
57
63
Posts: {
58
64
__args: {
59
- orderBy: ' post_date ' ,
60
- userId: 12
65
+ where: { id: 2 }
66
+ orderBy : ' post_date '
61
67
},
62
68
id: true ,
63
69
title: true ,
@@ -72,7 +78,7 @@ Resulting `graphql_query`
72
78
73
79
``` graphql
74
80
query {
75
- Posts (orderBy : " post_date " , userId : 12 ) {
81
+ Posts (where : { id : 2 }, orderBy : " post_date " ) {
76
82
id
77
83
title
78
84
post_date
Original file line number Diff line number Diff line change @@ -42,7 +42,7 @@ describe('jsonToGraphQL()', () => {
42
42
}` ) ;
43
43
} ) ;
44
44
45
- it ( 'converts a query with arguments' , ( ) => {
45
+ it ( 'converts a query with simple arguments' , ( ) => {
46
46
const query = {
47
47
query : {
48
48
Posts : {
@@ -66,6 +66,33 @@ describe('jsonToGraphQL()', () => {
66
66
}` ) ;
67
67
} ) ;
68
68
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
+
69
96
it ( 'converts a query with nested objects' , ( ) => {
70
97
const query = {
71
98
query : {
Original file line number Diff line number Diff line change 1
1
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
+
2
17
function buildArgs ( argsObj : any ) : string {
3
18
const args = [ ] ;
4
19
for ( const argName in argsObj ) {
5
- args . push ( `${ argName } : ${ JSON . stringify ( argsObj [ argName ] ) } ` ) ;
20
+ args . push ( `${ argName } : ${ stringify ( argsObj [ argName ] ) } ` ) ;
6
21
}
7
22
return args . join ( ', ' ) ;
8
23
}
You can’t perform that action at this time.
0 commit comments