Skip to content

Commit caa766f

Browse files
committed
WIP: Added types for jsonToGraphQLQuery function.
1 parent 621b80b commit caa766f

15 files changed

+428
-326
lines changed

src/__tests__/aliases.tests.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
1-
21
import { expect } from 'chai';
3-
import { jsonToGraphQLQuery } from '../';
2+
import { jsonToGraphQLQuery, QueryJSON } from '../';
43

54
describe('jsonToGraphQLQuery() - aliases', () => {
6-
75
it('supports multiple aliases for one type', () => {
8-
const query = {
6+
const query: QueryJSON = {
97
query: {
108
lorem: {
119
__aliasFor: 'Posts',
1210
__args: {
1311
arg1: 20,
1412
},
15-
id: true
13+
id: true,
1614
},
1715
larem: {
1816
__aliasFor: 'Posts',
1917
__args: {
2018
arg2: 10,
2119
},
22-
id: true
23-
}
24-
}
20+
id: true,
21+
},
22+
},
2523
};
2624
expect(jsonToGraphQLQuery(query)).to.equal(
2725
'query { lorem: Posts (arg1: 20) { id } larem: Posts (arg2: 10) { id } }'
2826
);
2927
});
30-
3128
});

src/__tests__/arguments.tests.ts

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
21
import { expect } from 'chai';
3-
import { jsonToGraphQLQuery } from '../';
2+
import { jsonToGraphQLQuery, QueryJSON } from '../';
43

54
describe('jsonToGraphQLQuery() - arguments', () => {
6-
75
it('converts a query with simple arguments', () => {
86
const query = {
97
query: {
108
Posts: {
119
__args: {
1210
orderBy: 'post_date',
13-
userId: 12
11+
userId: 12,
1412
},
1513
id: true,
1614
title: true,
17-
post_date: true
18-
}
19-
}
15+
post_date: true,
16+
},
17+
},
2018
};
2119
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
2220
`query {
@@ -25,7 +23,8 @@ describe('jsonToGraphQLQuery() - arguments', () => {
2523
title
2624
post_date
2725
}
28-
}`);
26+
}`
27+
);
2928
});
3029

3130
it('converts a query with JSON arguments', () => {
@@ -35,15 +34,15 @@ describe('jsonToGraphQLQuery() - arguments', () => {
3534
__args: {
3635
where: {
3736
published: true,
38-
rating: { _gt: 3 }
37+
rating: { _gt: 3 },
3938
},
40-
orderBy: 'post_date'
39+
orderBy: 'post_date',
4140
},
4241
id: true,
4342
title: true,
44-
post_date: true
45-
}
46-
}
43+
post_date: true,
44+
},
45+
},
4746
};
4847
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
4948
`query {
@@ -52,25 +51,23 @@ describe('jsonToGraphQLQuery() - arguments', () => {
5251
title
5352
post_date
5453
}
55-
}`);
54+
}`
55+
);
5656
});
5757

5858
it('converts a query with JSON arguments containing arrays of objects', () => {
59-
const query = {
59+
const query: QueryJSON = {
6060
query: {
6161
Posts: {
6262
__args: {
63-
or: [
64-
{ published: true },
65-
{ rating: [{ _gt: 3 }] }
66-
],
67-
orderBy: 'post_date'
63+
or: [{ published: true }, { rating: [{ _gt: 3 }] }],
64+
orderBy: 'post_date',
6865
},
6966
id: true,
7067
title: true,
71-
post_date: true
72-
}
73-
}
68+
post_date: true,
69+
},
70+
},
7471
};
7572
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
7673
`query {
@@ -79,7 +76,8 @@ describe('jsonToGraphQLQuery() - arguments', () => {
7976
title
8077
post_date
8178
}
82-
}`);
79+
}`
80+
);
8381
});
8482

8583
it('converts a query with null arguments and nested nulls', () => {
@@ -90,13 +88,13 @@ describe('jsonToGraphQLQuery() - arguments', () => {
9088
where: {
9189
id: null,
9290
},
93-
orderBy: null
91+
orderBy: null,
9492
},
9593
id: true,
9694
title: true,
97-
post_date: true
98-
}
99-
}
95+
post_date: true,
96+
},
97+
},
10098
} as any;
10199
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
102100
`query {
@@ -105,7 +103,8 @@ describe('jsonToGraphQLQuery() - arguments', () => {
105103
title
106104
post_date
107105
}
108-
}`);
106+
}`
107+
);
109108
});
110109

111110
it('converts a query with nested objects and arguments', () => {
@@ -114,20 +113,20 @@ describe('jsonToGraphQLQuery() - arguments', () => {
114113
Posts: {
115114
__args: {
116115
arg1: 20,
117-
arg2: 'flibble'
116+
arg2: 'flibble',
118117
},
119118
id: true,
120119
title: true,
121120
comments: {
122121
__args: {
123-
offensiveOnly: true
122+
offensiveOnly: true,
124123
},
125124
id: true,
126125
comment: true,
127-
user: true
128-
}
129-
}
130-
}
126+
user: true,
127+
},
128+
},
129+
},
131130
};
132131
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
133132
`query {
@@ -140,7 +139,8 @@ describe('jsonToGraphQLQuery() - arguments', () => {
140139
user
141140
}
142141
}
143-
}`);
142+
}`
143+
);
144144
});
145145

146146
it('works with pretty mode turned off', () => {
@@ -149,12 +149,12 @@ describe('jsonToGraphQLQuery() - arguments', () => {
149149
Posts: {
150150
__args: {
151151
arg1: 20,
152-
arg2: 'flibble'
152+
arg2: 'flibble',
153153
},
154154
id: true,
155-
title: true
156-
}
157-
}
155+
title: true,
156+
},
157+
},
158158
};
159159
expect(jsonToGraphQLQuery(query)).to.equal(
160160
'query { Posts (arg1: 20, arg2: "flibble") { id title } }'
@@ -167,13 +167,12 @@ describe('jsonToGraphQLQuery() - arguments', () => {
167167
Posts: {
168168
__args: {},
169169
id: true,
170-
title: true
171-
}
172-
}
170+
title: true,
171+
},
172+
},
173173
};
174174
expect(jsonToGraphQLQuery(query)).to.equal(
175175
'query { Posts { id title } }'
176176
);
177177
});
178-
179178
});

src/__tests__/directives.tests.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
21
import { expect } from 'chai';
3-
import { jsonToGraphQLQuery } from '../';
2+
import { jsonToGraphQLQuery, QueryJSON } from '../';
43

54
describe('jsonToGraphQLQuery() - directives', () => {
6-
75
it('converts a simple query with args and directives with no arguments', () => {
86
const query = {
97
query: {
@@ -12,16 +10,16 @@ describe('jsonToGraphQLQuery() - directives', () => {
1210
where: {
1311
id: 10,
1412
},
15-
orderBy: 'flibble'
13+
orderBy: 'flibble',
1614
},
1715
__directives: {
18-
client: true
16+
client: true,
1917
},
2018
id: true,
2119
title: true,
22-
post_date: true
23-
}
24-
}
20+
post_date: true,
21+
},
22+
},
2523
} as any;
2624
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
2725
`query {
@@ -30,15 +28,16 @@ describe('jsonToGraphQLQuery() - directives', () => {
3028
title
3129
post_date
3230
}
33-
}`);
31+
}`
32+
);
3433
});
3534

3635
it('converts a complex query with directives with no arguments', () => {
37-
const query = {
36+
const query: QueryJSON = {
3837
query: {
3938
diet: {
4039
__directives: {
41-
client: true
40+
client: true,
4241
},
4342
id: 'diet',
4443
options: {
@@ -52,21 +51,22 @@ describe('jsonToGraphQLQuery() - directives', () => {
5251
icon: 'fa fa-question-circle',
5352
id: 'weight',
5453
selected: false,
55-
text: 'Weight'
54+
text: 'Weight',
5655
},
5756
},
58-
title: 'Diet'
57+
title: 'Diet',
5958
},
6059
someOtherAbritraryKey: {
6160
__directives: {
62-
client: true
61+
client: true,
6362
},
6463
arb1: 'arbitrary value',
65-
arb2: 'some other arbitrary value'
66-
}
67-
}
64+
arb2: 'some other arbitrary value',
65+
},
66+
},
6867
};
69-
const expected = 'query { diet @client { id options { ' +
68+
const expected =
69+
'query { diet @client { id options { ' +
7070
'mood { category id selected } weight { category icon id text } } ' +
7171
'title } someOtherAbritraryKey @client { arb1 arb2 } }';
7272
expect(jsonToGraphQLQuery(query)).to.equal(expected);
@@ -137,7 +137,7 @@ describe('jsonToGraphQLQuery() - directives', () => {
137137
});
138138

139139
it('converts a complex query with multiple directives', () => {
140-
const query = {
140+
const query: QueryJSON = {
141141
query: {
142142
diet: {
143143
__directives: {
@@ -160,7 +160,7 @@ describe('jsonToGraphQLQuery() - directives', () => {
160160
},
161161
title: 'Diet',
162162
},
163-
someOtherAbritraryKey: {
163+
someOtherArbitraryKey: {
164164
__directives: {
165165
client: true,
166166
withArgs: {
@@ -175,7 +175,7 @@ describe('jsonToGraphQLQuery() - directives', () => {
175175
const expected =
176176
'query { diet @client { id options { ' +
177177
'mood { category id selected } weight { category icon id text } } ' +
178-
'title } someOtherAbritraryKey @client @withArgs(id: [1, 2, 3]) { arb1 arb2 } }';
178+
'title } someOtherArbitraryKey @client @withArgs(id: [1, 2, 3]) { arb1 arb2 } }';
179179
expect(jsonToGraphQLQuery(query)).to.equal(expected);
180180
});
181181

@@ -209,5 +209,4 @@ describe('jsonToGraphQLQuery() - directives', () => {
209209
// it('creates a query, stripping/ignoring certain, specified keys', () => {
210210
// // Example usage: jsonToGraphqlQuery(preInput, { keysToStrip: ['__typename'] });
211211
// });
212-
213212
});

0 commit comments

Comments
 (0)