Skip to content

Commit 26c44a1

Browse files
committed
Merge branch 'bret-hubbard-master'
2 parents 5165bd7 + 88f6389 commit 26c44a1

12 files changed

+767
-538
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11

22
# json-to-graphql-query Changelog
33

4+
## 1.9.0
5+
6+
* Added support for array values. We now use the first object found in an array for the
7+
node names. If the array does not contain an object, we just return the corresponding key.
8+
* Added `includeFalsyKeys` option, to disable the default behaviour of excluding keys with falsy values.
9+
Thanks @bret-hubbard for both of these additions :)
10+
411
## 1.8.0
512

613
* Added support for Inline Fragments. Thanks again @jeniffer9 :)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const query = jsonToGraphQLQuery(queryObject: object, options?: object);
2020
Supported Options:
2121
* `pretty` - boolean - optional - set to `true` to enable pretty-printed output
2222
* `ignoreFields` - string[] - optional - you can pass an array of object key names that you want removed from the query
23+
* `includeFalsyKeys` - boolean - optional - disable the default behaviour if excluding keys with a falsy value
2324

2425
## Features
2526

@@ -171,6 +172,8 @@ query {
171172
}
172173
}
173174
```
175+
NOTE: You can tell jsonToGraphQLQuery() not to exclude keys with a falsy value
176+
by setting the `includeFalsyKeys` option.
174177

175178
### Using aliases
176179

src/__tests__/aliases.tests.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
import { expect } from 'chai';
3+
import { jsonToGraphQLQuery } from '../';
4+
5+
describe('jsonToGraphQLQuery() - aliases', () => {
6+
7+
it('uses aliases for fields', () => {
8+
// Deprecated...
9+
const query = {
10+
query: {
11+
Posts: {
12+
__alias: 'lorem',
13+
__args: {
14+
arg1: 20,
15+
},
16+
id: true
17+
}
18+
}
19+
};
20+
expect(jsonToGraphQLQuery(query)).to.equal(
21+
'query { lorem: Posts (arg1: 20) { id } }'
22+
);
23+
});
24+
25+
it('supports multiple aliases for one type', () => {
26+
const query = {
27+
query: {
28+
lorem: {
29+
__aliasFor: 'Posts',
30+
__args: {
31+
arg1: 20,
32+
},
33+
id: true
34+
},
35+
larem: {
36+
__aliasFor: 'Posts',
37+
__args: {
38+
arg2: 10,
39+
},
40+
id: true
41+
}
42+
}
43+
};
44+
expect(jsonToGraphQLQuery(query)).to.equal(
45+
'query { lorem: Posts (arg1: 20) { id } larem: Posts (arg2: 10) { id } }'
46+
);
47+
});
48+
49+
});

src/__tests__/arguments.tests.ts

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
2+
import { expect } from 'chai';
3+
import { jsonToGraphQLQuery } from '../';
4+
5+
describe('jsonToGraphQLQuery() - arguments', () => {
6+
7+
it('converts a query with simple arguments', () => {
8+
const query = {
9+
query: {
10+
Posts: {
11+
__args: {
12+
orderBy: 'post_date',
13+
userId: 12
14+
},
15+
id: true,
16+
title: true,
17+
post_date: true
18+
}
19+
}
20+
};
21+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
22+
`query {
23+
Posts (orderBy: "post_date", userId: 12) {
24+
id
25+
title
26+
post_date
27+
}
28+
}`);
29+
});
30+
31+
it('converts a query with JSON arguments', () => {
32+
const query = {
33+
query: {
34+
Posts: {
35+
__args: {
36+
where: {
37+
published: true,
38+
rating: { _gt: 3 }
39+
},
40+
orderBy: 'post_date'
41+
},
42+
id: true,
43+
title: true,
44+
post_date: true
45+
}
46+
}
47+
};
48+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
49+
`query {
50+
Posts (where: {published: true, rating: {_gt: 3}}, orderBy: "post_date") {
51+
id
52+
title
53+
post_date
54+
}
55+
}`);
56+
});
57+
58+
it('converts a query with JSON arguments containing arrays of objects', () => {
59+
const query = {
60+
query: {
61+
Posts: {
62+
__args: {
63+
or: [
64+
{ published: true },
65+
{ rating: [{ _gt: 3 }] }
66+
],
67+
orderBy: 'post_date'
68+
},
69+
id: true,
70+
title: true,
71+
post_date: true
72+
}
73+
}
74+
};
75+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
76+
`query {
77+
Posts (or: [{published: true}, {rating: [{_gt: 3}]}], orderBy: "post_date") {
78+
id
79+
title
80+
post_date
81+
}
82+
}`);
83+
});
84+
85+
it('converts a query with null arguments and nested nulls', () => {
86+
const query = {
87+
query: {
88+
Posts: {
89+
__args: {
90+
where: {
91+
id: null,
92+
},
93+
orderBy: null
94+
},
95+
id: true,
96+
title: true,
97+
post_date: true
98+
}
99+
}
100+
} as any;
101+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
102+
`query {
103+
Posts (where: {id: null}, orderBy: null) {
104+
id
105+
title
106+
post_date
107+
}
108+
}`);
109+
});
110+
111+
it('converts a query with nested objects and arguments', () => {
112+
const query = {
113+
query: {
114+
Posts: {
115+
__args: {
116+
arg1: 20,
117+
arg2: 'flibble'
118+
},
119+
id: true,
120+
title: true,
121+
comments: {
122+
__args: {
123+
offensiveOnly: true
124+
},
125+
id: true,
126+
comment: true,
127+
user: true
128+
}
129+
}
130+
}
131+
};
132+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
133+
`query {
134+
Posts (arg1: 20, arg2: "flibble") {
135+
id
136+
title
137+
comments (offensiveOnly: true) {
138+
id
139+
comment
140+
user
141+
}
142+
}
143+
}`);
144+
});
145+
146+
it('works with pretty mode turned off', () => {
147+
const query = {
148+
query: {
149+
Posts: {
150+
__args: {
151+
arg1: 20,
152+
arg2: 'flibble'
153+
},
154+
id: true,
155+
title: true
156+
}
157+
}
158+
};
159+
expect(jsonToGraphQLQuery(query)).to.equal(
160+
'query { Posts (arg1: 20, arg2: "flibble") { id title } }'
161+
);
162+
});
163+
164+
});

src/__tests__/directives.tests.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
2+
import { expect } from 'chai';
3+
import { jsonToGraphQLQuery } from '../';
4+
5+
describe('jsonToGraphQLQuery() - directives', () => {
6+
7+
it('converts a simple query with args and directives with no arguments', () => {
8+
const query = {
9+
query: {
10+
Posts: {
11+
__args: {
12+
where: {
13+
id: 10,
14+
},
15+
orderBy: 'flibble'
16+
},
17+
__directives: {
18+
client: true
19+
},
20+
id: true,
21+
title: true,
22+
post_date: true
23+
}
24+
}
25+
} as any;
26+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
27+
`query {
28+
Posts @client (where: {id: 10}, orderBy: "flibble") {
29+
id
30+
title
31+
post_date
32+
}
33+
}`);
34+
});
35+
36+
it('converts a complex query with directives with no arguments', () => {
37+
const query = {
38+
query: {
39+
diet: {
40+
__directives: {
41+
client: true
42+
},
43+
id: 'diet',
44+
options: {
45+
mood: {
46+
category: 'Diet',
47+
id: 'mood',
48+
selected: true,
49+
},
50+
weight: {
51+
category: 'Diet',
52+
icon: 'fa fa-question-circle',
53+
id: 'weight',
54+
selected: false,
55+
text: 'Weight'
56+
},
57+
},
58+
title: 'Diet'
59+
},
60+
someOtherAbritraryKey: {
61+
__directives: {
62+
client: true
63+
},
64+
arb1: 'arbitrary value',
65+
arb2: 'some other arbitrary value'
66+
}
67+
}
68+
};
69+
const expected = 'query { diet @client { id options { ' +
70+
'mood { category id selected } weight { category icon id text } } ' +
71+
'title } someOtherAbritraryKey @client { arb1 arb2 } }';
72+
expect(jsonToGraphQLQuery(query)).to.equal(expected);
73+
});
74+
75+
// TODO: Need this test still? How to handle variables unless $ declared explicitly?
76+
// it('converts a JavaScript object into a valid query, including single directives ' +
77+
// 'with args, so long as any variables used are enclosed in a string with "$" included', () => {
78+
// interface ILooseObject { [key: string]: any; }
79+
// let input: ILooseObject = {
80+
// someOtherAbritraryKey: {
81+
// __typename: 'someArbitraryObjType',
82+
// arb1: 'arbitrary value',
83+
// arb2: 'some other arbitrary value'
84+
// }
85+
// };
86+
// Object.keys(input)
87+
// .filter(filterNonConfigFields)
88+
// .forEach((key) => {
89+
// input[key]['__directives'] = { include: {if: '$isAwesome'}, };
90+
// });
91+
// input = {query: input};
92+
// const expected = 'query { someOtherAbritraryKey @include(if: $isAwesome) { arb1 arb2 } }';
93+
// expect(jsonToGraphQLQuery(input)).to.equal(expected);
94+
// });
95+
96+
// TODO
97+
// it('converts a JavaScript object into a valid query, including *multiple* directives ' +
98+
// 'with args, so long as any variables used are enclosed in a string with "$" included', () => {
99+
// });
100+
101+
// TODO
102+
// it('creates a query, stripping/ignoring certain, specified keys', () => {
103+
// // Example usage: jsonToGraphqlQuery(preInput, { keysToStrip: ['__typename'] });
104+
// });
105+
106+
});

0 commit comments

Comments
 (0)