Skip to content

Commit b124832

Browse files
committed
Tweaks to test and readme content
1 parent 00a19f7 commit b124832

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

README.md

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

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+
1424
## Features
1525

1626
* Converts a JavaScript object to a GraphQL Query string
1727
* Full support for nested query / mutation nodes and arguments
28+
* Optionally strip specific object keys using the `ignoreFields` option
1829
* Support for input arguments via [`__args`](#query-with-arguments)
1930
* Support for query aliases via [`__alias`](#using-aliases)
2031
* Support for Enum values via [`EnumType`](#query-with-enum-values)
@@ -24,13 +35,6 @@ npm install json-to-graphql-query
2435

2536
See the [CHANGELOG](CHANGELOG.md)
2637

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-
3438
### Simple Query
3539

3640
```typescript
@@ -266,18 +270,18 @@ query ($variable1: String!, $variableWithDefault: String = "default_value") {
266270
}
267271
```
268272

269-
### Ignore fields of the initial object
273+
### Ignoring fields in the query object
274+
270275
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:
273277

274278
```typescript
275279
import { jsonToGraphQLQuery, VariableType } from 'json-to-graphql-query';
276280

277281
const query = {
278282
query: {
279283
Posts: {
280-
__ignore: {
284+
shouldBeIgnored: {
281285
variable1: 'a value'
282286
},
283287
id: true,
@@ -286,7 +290,10 @@ const query = {
286290
}
287291
}
288292
};
289-
const graphql_query = jsonToGraphQLQuery(query, { pretty: true, ignoreFields: ['__ignore'] });
293+
const graphql_query = jsonToGraphQLQuery(query, {
294+
pretty: true,
295+
ignoreFields: ['shouldBeIgnored']
296+
});
290297
```
291298

292299
Resulting `graphql_query`

src/__tests__/jsonToGraphQLQuery.tests.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ describe('jsonToGraphQL()', () => {
286286
const query = {
287287
query: {
288288
Posts: {
289-
__ignore: {
289+
thisShouldBeIgnored: {
290290
test: 'a value'
291291
},
292292
id: true,
@@ -295,7 +295,10 @@ describe('jsonToGraphQL()', () => {
295295
}
296296
}
297297
};
298-
expect(jsonToGraphQLQuery(query, { pretty: true, ignoreFields: ['__ignore'] })).to.equal(
298+
expect(jsonToGraphQLQuery(query, {
299+
pretty: true,
300+
ignoreFields: ['thisShouldBeIgnored']
301+
})).to.equal(
299302
`query {
300303
Posts {
301304
id

0 commit comments

Comments
 (0)