@@ -11,10 +11,21 @@ Mainly useful for applications that need to generate graphql queries dynamically
11
11
npm install json-to-graphql-query
12
12
```
13
13
14
+ ## Usage
15
+
16
+ ``` ts
17
+ const query = jsonToGraphQLQuery (queryObject : object , options ?: object );
18
+ ```
19
+
20
+ Supported Options:
21
+ * ` pretty: boolean ` - optional - set to ` true ` to enable pretty-printed output
22
+ * ` ignoreFields: string[] ` - optional - you can pass an array of object key names that you want removed from the query
23
+
14
24
## Features
15
25
16
26
* Converts a JavaScript object to a GraphQL Query string
17
27
* Full support for nested query / mutation nodes and arguments
28
+ * Optionally strip specific object keys using the ` ignoreFields ` option
18
29
* Support for input arguments via [ ` __args ` ] ( #query-with-arguments )
19
30
* Support for query aliases via [ ` __alias ` ] ( #using-aliases )
20
31
* Support for Enum values via [ ` EnumType ` ] ( #query-with-enum-values )
@@ -24,13 +35,6 @@ npm install json-to-graphql-query
24
35
25
36
See the [ CHANGELOG] ( CHANGELOG.md )
26
37
27
- ## Usage
28
-
29
- ** jsonToGraphQLQuery(** queryObject: object, options?: object ** )**
30
-
31
- Supported options:
32
- * ** pretty** : boolean - Set to ` true ` to enable pretty-printed output
33
-
34
38
### Simple Query
35
39
36
40
``` typescript
@@ -266,18 +270,18 @@ query ($variable1: String!, $variableWithDefault: String = "default_value") {
266
270
}
267
271
```
268
272
269
- ### Ignore fields of the initial object
273
+ ### Ignoring fields in the query object
274
+
270
275
We sometimes want to ignore specific fields in the initial object, for instance __ typename in Apollo queries.
271
- While we can put the field value to ` false ` , we might want to keep its value.
272
- You can specify those fields not to be put into the final GraphQL query though the ` ignoreFields ` option:
276
+ You can specify these fields using the ` ignoreFields ` option:
273
277
274
278
``` typescript
275
279
import { jsonToGraphQLQuery , VariableType } from ' json-to-graphql-query' ;
276
280
277
281
const query = {
278
282
query: {
279
283
Posts: {
280
- __ignore : {
284
+ shouldBeIgnored : {
281
285
variable1: ' a value'
282
286
},
283
287
id: true ,
@@ -286,7 +290,10 @@ const query = {
286
290
}
287
291
}
288
292
};
289
- const graphql_query = jsonToGraphQLQuery (query , { pretty: true , ignoreFields: [' __ignore' ] });
293
+ const graphql_query = jsonToGraphQLQuery (query , {
294
+ pretty: true ,
295
+ ignoreFields: [' shouldBeIgnored' ]
296
+ });
290
297
```
291
298
292
299
Resulting ` graphql_query `
0 commit comments