Skip to content

Commit 14369f5

Browse files
committed
Feat: Added types for jsonToGraphQLQuery function.
1 parent 621b80b commit 14369f5

14 files changed

+235
-125
lines changed

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/aliases.tests.ts

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

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

55
describe('jsonToGraphQLQuery() - aliases', () => {
66

77
it('supports multiple aliases for one type', () => {
8-
const query = {
8+
const query: QueryJSON = {
99
query: {
1010
lorem: {
1111
__aliasFor: 'Posts',

src/__tests__/arguments.tests.ts

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

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

55
describe('jsonToGraphQLQuery() - arguments', () => {
66

77
it('converts a query with simple arguments', () => {
8-
const query = {
8+
const query: QueryJSON = {
99
query: {
1010
Posts: {
1111
__args: {
@@ -29,7 +29,7 @@ describe('jsonToGraphQLQuery() - arguments', () => {
2929
});
3030

3131
it('converts a query with JSON arguments', () => {
32-
const query = {
32+
const query: QueryJSON = {
3333
query: {
3434
Posts: {
3535
__args: {
@@ -56,7 +56,7 @@ describe('jsonToGraphQLQuery() - arguments', () => {
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: {
@@ -83,7 +83,7 @@ describe('jsonToGraphQLQuery() - arguments', () => {
8383
});
8484

8585
it('converts a query with null arguments and nested nulls', () => {
86-
const query = {
86+
const query: QueryJSON = {
8787
query: {
8888
Posts: {
8989
__args: {
@@ -97,7 +97,7 @@ describe('jsonToGraphQLQuery() - arguments', () => {
9797
post_date: true
9898
}
9999
}
100-
} as any;
100+
};
101101
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
102102
`query {
103103
Posts (where: {id: null}, orderBy: null) {
@@ -109,7 +109,7 @@ describe('jsonToGraphQLQuery() - arguments', () => {
109109
});
110110

111111
it('converts a query with nested objects and arguments', () => {
112-
const query = {
112+
const query: QueryJSON = {
113113
query: {
114114
Posts: {
115115
__args: {
@@ -144,7 +144,7 @@ describe('jsonToGraphQLQuery() - arguments', () => {
144144
});
145145

146146
it('works with pretty mode turned off', () => {
147-
const query = {
147+
const query: QueryJSON = {
148148
query: {
149149
Posts: {
150150
__args: {
@@ -162,7 +162,7 @@ describe('jsonToGraphQLQuery() - arguments', () => {
162162
});
163163

164164
it('Empty args object should not generate parentheses', () => {
165-
const query = {
165+
const query: QueryJSON = {
166166
query: {
167167
Posts: {
168168
__args: {},

src/__tests__/directives.tests.ts

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

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

55
describe('jsonToGraphQLQuery() - directives', () => {
66

77
it('converts a simple query with args and directives with no arguments', () => {
8-
const query = {
8+
const query: QueryJSON = {
99
query: {
1010
Posts: {
1111
__args: {
@@ -22,7 +22,7 @@ describe('jsonToGraphQLQuery() - directives', () => {
2222
post_date: true
2323
}
2424
}
25-
} as any;
25+
};
2626
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
2727
`query {
2828
Posts (where: {id: 10}, orderBy: "flibble") @client {
@@ -34,7 +34,7 @@ describe('jsonToGraphQLQuery() - directives', () => {
3434
});
3535

3636
it('converts a complex query with directives with no arguments', () => {
37-
const query = {
37+
const query: QueryJSON = {
3838
query: {
3939
diet: {
4040
__directives: {
@@ -62,7 +62,7 @@ describe('jsonToGraphQLQuery() - directives', () => {
6262
client: true
6363
},
6464
arb1: 'arbitrary value',
65-
arb2: 'some other arbitrary value'
65+
arb2: 'some other arbitrary value',
6666
}
6767
}
6868
};
@@ -73,7 +73,7 @@ describe('jsonToGraphQLQuery() - directives', () => {
7373
});
7474

7575
it('converts a simple query with args and multiple directives', () => {
76-
const query = {
76+
const query: QueryJSON = {
7777
query: {
7878
Posts: {
7979
__args: {
@@ -93,7 +93,7 @@ describe('jsonToGraphQLQuery() - directives', () => {
9393
post_date: true,
9494
},
9595
},
96-
} as any;
96+
};
9797
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
9898
`query {
9999
Posts (where: {id: 10}, orderBy: "flibble") @client @withArgs(id: [1, 2, 3]) {
@@ -106,7 +106,7 @@ describe('jsonToGraphQLQuery() - directives', () => {
106106
});
107107

108108
it('converts a simple query with args and multiple directives but a directive has an empty object', () => {
109-
const query = {
109+
const query: QueryJSON = {
110110
query: {
111111
Posts: {
112112
__args: {
@@ -124,7 +124,7 @@ describe('jsonToGraphQLQuery() - directives', () => {
124124
post_date: true,
125125
},
126126
},
127-
} as any;
127+
};
128128
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
129129
`query {
130130
Posts (where: {id: 10}, orderBy: "flibble") @client @withArgs {
@@ -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

src/__tests__/falsykeys.tests.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { expect } from 'chai';
2-
import { jsonToGraphQLQuery } from '../';
2+
import { jsonToGraphQLQuery, type QueryJSON } from '../';
33

44
describe('jsonToGraphQLQuery() - falsy keys', () => {
55
it('does not include fields which value is false', () => {
6-
const query = {
6+
const query: QueryJSON = {
77
query: {
88
Posts: {
99
__args: {
@@ -24,7 +24,7 @@ describe('jsonToGraphQLQuery() - falsy keys', () => {
2424
});
2525

2626
it('includes fields with falsy values if includeFalsyKeys is true', () => {
27-
const query = {
27+
const query: QueryJSON = {
2828
query: {
2929
Posts: {
3030
__args: {
@@ -45,7 +45,7 @@ describe('jsonToGraphQLQuery() - falsy keys', () => {
4545
});
4646

4747
it('does not include object with only false keys', () => {
48-
const query = {
48+
const query: QueryJSON = {
4949
query: {
5050
Posts: {
5151
id: true,
@@ -63,7 +63,7 @@ describe('jsonToGraphQLQuery() - falsy keys', () => {
6363
});
6464

6565
it('does include object with only false keys if includeFalsyKeys is true', () => {
66-
const query = {
66+
const query: QueryJSON = {
6767
query: {
6868
Posts: {
6969
id: true,
@@ -81,7 +81,7 @@ describe('jsonToGraphQLQuery() - falsy keys', () => {
8181
});
8282

8383
it('Includes the nested object if includeFalsyKeys is true', () => {
84-
const query = {
84+
const query: QueryJSON = {
8585
query: {
8686
Posts: {
8787
id: true,
@@ -100,7 +100,7 @@ describe('jsonToGraphQLQuery() - falsy keys', () => {
100100
});
101101

102102
it('does not include the object if nested object has falsy values', () => {
103-
const query = {
103+
const query: QueryJSON = {
104104
query: {
105105
Posts: {
106106
id: true,
@@ -117,7 +117,7 @@ describe('jsonToGraphQLQuery() - falsy keys', () => {
117117
});
118118

119119
it('skip objects when deeply nested keys contain falsy values', () => {
120-
const query = {
120+
const query: QueryJSON = {
121121
query: {
122122
id: true,
123123
Posts: {
@@ -145,7 +145,7 @@ describe('jsonToGraphQLQuery() - falsy keys', () => {
145145
});
146146

147147
it('Include values if nested object has falsy values and includeFalsyKeys is true', () => {
148-
const query = {
148+
const query: QueryJSON = {
149149
query: {
150150
Posts: {
151151
id: true,

src/__tests__/fragments.tests.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

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

55
describe('jsonToGraphQLQuery() - fragments', () => {
66

77
it('supports inline fragments', () => {
8-
const query = {
8+
const query: QueryJSON = {
99
query: {
1010
Posts: {
1111
__on: {
@@ -21,7 +21,7 @@ describe('jsonToGraphQLQuery() - fragments', () => {
2121
});
2222

2323
it('supports inline fragments with subfields on same level', () => {
24-
const query = {
24+
const query: QueryJSON = {
2525
query: {
2626
Posts: {
2727
title: true,
@@ -38,7 +38,7 @@ describe('jsonToGraphQLQuery() - fragments', () => {
3838
});
3939

4040
it('supports multiple inline fragments', () => {
41-
const query = {
41+
const query: QueryJSON = {
4242
query: {
4343
Posts: {
4444
__on: [
@@ -49,7 +49,8 @@ describe('jsonToGraphQLQuery() - fragments', () => {
4949
{
5050
__typeName: 'UnconfigurablePost',
5151
name: true
52-
}]
52+
}
53+
]
5354
}
5455
}
5556
};
@@ -59,19 +60,19 @@ describe('jsonToGraphQLQuery() - fragments', () => {
5960
});
6061

6162
it('supports full inline fragments', () => {
62-
const query = {
63-
query: {
64-
Posts: {
65-
__all_on: [
66-
'ConfigurablePost',
67-
'PageInfo'
68-
]
69-
}
70-
}
71-
};
72-
expect(jsonToGraphQLQuery(query)).to.equal(
73-
'query { Posts { ...ConfigurablePost ...PageInfo } }'
74-
);
75-
});
63+
const query: QueryJSON = {
64+
query: {
65+
Posts: {
66+
__all_on: [
67+
'ConfigurablePost',
68+
'PageInfo'
69+
]
70+
}
71+
}
72+
};
73+
expect(jsonToGraphQLQuery(query)).to.equal(
74+
'query { Posts { ...ConfigurablePost ...PageInfo } }'
75+
);
76+
});
7677

7778
});

0 commit comments

Comments
 (0)