Skip to content

Commit ce78af9

Browse files
committed
Simplify basic directive test (with no params)
1 parent 3fa613a commit ce78af9

File tree

2 files changed

+134
-79
lines changed

2 files changed

+134
-79
lines changed

README.md

Lines changed: 52 additions & 10 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
@@ -60,7 +64,7 @@ query {
6064
}
6165
```
6266

63-
### Query with arguments
67+
### Query with arguments
6468

6569
```typescript
6670
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
@@ -200,7 +204,7 @@ query {
200204
}
201205
```
202206

203-
### Query with Enum Values
207+
### Query with Enum Values
204208

205209
```typescript
206210
import { jsonToGraphQLQuery, EnumType } from 'json-to-graphql-query';
@@ -231,7 +235,7 @@ query {
231235
}
232236
```
233237

234-
### Query with variables
238+
### Query with variables
235239

236240
```typescript
237241
import { jsonToGraphQLQuery, VariableType } from 'json-to-graphql-query';
@@ -266,6 +270,44 @@ query ($variable1: String!, $variableWithDefault: String = "default_value") {
266270
}
267271
```
268272

273+
### Ignoring fields in the query object
274+
275+
We sometimes want to ignore specific fields in the initial object, for instance __typename in Apollo queries.
276+
You can specify these fields using the `ignoreFields` option:
277+
278+
```typescript
279+
import { jsonToGraphQLQuery, VariableType } from 'json-to-graphql-query';
280+
281+
const query = {
282+
query: {
283+
Posts: {
284+
shouldBeIgnored: {
285+
variable1: 'a value'
286+
},
287+
id: true,
288+
title: true,
289+
post_date: true
290+
}
291+
}
292+
};
293+
const graphql_query = jsonToGraphQLQuery(query, {
294+
pretty: true,
295+
ignoreFields: ['shouldBeIgnored']
296+
});
297+
```
298+
299+
Resulting `graphql_query`
300+
301+
```graphql
302+
query {
303+
Posts {
304+
id
305+
title
306+
post_date
307+
}
308+
}
309+
```
310+
269311
## TO-DO List
270312

271313
* Support Named Queries / Mutations

src/__tests__/jsonToGraphQLQuery.tests.ts

Lines changed: 82 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import { expect } from 'chai';
3-
import { jsonToGraphQLQuery, filterNonConfigFields } from '../';
3+
import { jsonToGraphQLQuery } from '../';
44

55
describe('jsonToGraphQL()', () => {
66

@@ -146,82 +146,64 @@ describe('jsonToGraphQL()', () => {
146146
}`);
147147
});
148148

149-
it('Converts a JavaScript object into a valid query, including a single directive w/ no arguments.', () => {
150-
interface ILooseObject { [key: string]: any; }
151-
let input: ILooseObject = {
152-
__typename: 'everyday-health-focuses',
153-
diet: {
154-
__typename: 'diet',
155-
id: 'diet',
156-
options: {
157-
// tslint:disable-next-line:object-literal-key-quotes
158-
__typename: 'diet-options',
159-
'calorie-count': {
160-
__typename: 'calorie-count',
161-
category: 'Diet',
162-
icon: 'fa fa-question-circle',
163-
id: 'calorie-count',
164-
selected: false,
165-
text: 'Calorie Count'
166-
},
167-
// tslint:disable-next-line:object-literal-key-quotes
168-
mood: {
169-
__typename: 'mood',
170-
category: 'Diet',
171-
icon: 'fa fa-question-circle',
172-
id: 'mood',
173-
selected: false,
174-
text: 'Mood'
149+
it('Converts a complex query with directives with no arguments', () => {
150+
const query = {
151+
query: {
152+
diet: {
153+
__directives: {
154+
client: true
175155
},
176-
// tslint:disable-next-line:object-literal-key-quotes
177-
weight: {
178-
__typename: 'weight',
179-
category: 'Diet',
180-
icon: 'fa fa-question-circle',
181-
id: 'weight',
182-
selected: false,
183-
text: 'Weight'
156+
id: 'diet',
157+
options: {
158+
mood: {
159+
category: 'Diet',
160+
id: 'mood',
161+
selected: true,
162+
},
163+
weight: {
164+
category: 'Diet',
165+
icon: 'fa fa-question-circle',
166+
id: 'weight',
167+
selected: false,
168+
text: 'Weight'
169+
},
184170
},
171+
title: 'Diet'
185172
},
186-
title: 'Diet'
187-
},
188-
someOtherAbritraryKey: {
189-
__typename: 'someArbitraryObjType',
190-
arb1: 'arbitrary value',
191-
arb2: 'some other arbitrary value'
173+
someOtherAbritraryKey: {
174+
__directives: {
175+
client: true
176+
},
177+
arb1: 'arbitrary value',
178+
arb2: 'some other arbitrary value'
179+
}
192180
}
193181
};
194-
Object.keys(input)
195-
.filter(filterNonConfigFields)
196-
.forEach((key) => {
197-
input[key]['__directives'] = { client: true, };
198-
});
199-
input = {query: input};
200-
const expected = 'query { diet @client { id options { calorie-count { category ' +
201-
'icon id text } mood { category icon id text } weight { category icon id text } } ' +
182+
const expected = 'query { diet @client { id options { ' +
183+
'mood { category id selected } weight { category icon id text } } ' +
202184
'title } someOtherAbritraryKey @client { arb1 arb2 } }';
203-
expect(jsonToGraphQLQuery(input)).to.equal(expected);
185+
expect(jsonToGraphQLQuery(query)).to.equal(expected);
204186
});
205187

206-
it('Converts a JavaScript object into a valid query, including single directives ' +
207-
'with args, so long as any variables used are enclosed in a string with "$" included.', () => {
208-
interface ILooseObject { [key: string]: any; }
209-
let input: ILooseObject = {
210-
someOtherAbritraryKey: {
211-
__typename: 'someArbitraryObjType',
212-
arb1: 'arbitrary value',
213-
arb2: 'some other arbitrary value'
214-
}
215-
};
216-
Object.keys(input)
217-
.filter(filterNonConfigFields)
218-
.forEach((key) => {
219-
input[key]['__directives'] = { include: {if: '$isAwesome'}, };
220-
});
221-
input = {query: input};
222-
const expected = 'query { someOtherAbritraryKey @include(if: $isAwesome) { arb1 arb2 } }';
223-
expect(jsonToGraphQLQuery(input)).to.equal(expected);
224-
});
188+
// it('Converts a JavaScript object into a valid query, including single directives ' +
189+
// 'with args, so long as any variables used are enclosed in a string with "$" included.', () => {
190+
// interface ILooseObject { [key: string]: any; }
191+
// let input: ILooseObject = {
192+
// someOtherAbritraryKey: {
193+
// __typename: 'someArbitraryObjType',
194+
// arb1: 'arbitrary value',
195+
// arb2: 'some other arbitrary value'
196+
// }
197+
// };
198+
// Object.keys(input)
199+
// .filter(filterNonConfigFields)
200+
// .forEach((key) => {
201+
// input[key]['__directives'] = { include: {if: '$isAwesome'}, };
202+
// });
203+
// input = {query: input};
204+
// const expected = 'query { someOtherAbritraryKey @include(if: $isAwesome) { arb1 arb2 } }';
205+
// expect(jsonToGraphQLQuery(input)).to.equal(expected);
206+
// });
225207

226208
// TODO
227209
// it('Converts a JavaScript object into a valid query, including *multiple* directives ' +
@@ -395,4 +377,35 @@ describe('jsonToGraphQL()', () => {
395377
}`);
396378
});
397379

380+
it('we can ignore apollo __typename keys', () => {
381+
const query = {
382+
query: {
383+
Posts: {
384+
__typename: 'Posts',
385+
id: true,
386+
title: true,
387+
post_date: true,
388+
subObject: {
389+
__typename: 'subObject',
390+
test: 'a value'
391+
},
392+
}
393+
}
394+
};
395+
expect(jsonToGraphQLQuery(query, {
396+
pretty: true,
397+
ignoreFields: ['__typename']
398+
})).to.equal(
399+
`query {
400+
Posts {
401+
id
402+
title
403+
post_date
404+
subObject {
405+
test
406+
}
407+
}
408+
}`);
409+
});
410+
398411
});

0 commit comments

Comments
 (0)