Skip to content

Commit cc97c8e

Browse files
committed
refactored the type complexity analysis function to accept query variables and the query AST
1 parent 2063e3f commit cc97c8e

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

src/analysis/typeComplexityAnalysis.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parse } from 'graphql';
1+
import { DocumentNode } from 'graphql';
22

33
/**
44
* This function should
@@ -13,10 +13,15 @@ import { parse } from 'graphql';
1313
*
1414
* @param {string} queryString
1515
* @param {TypeWeightObject} typeWeights
16+
* @param {any | undefined} varibales
1617
* @param {string} complexityOption
1718
*/
1819
// TODO add queryVaribables parameter
19-
function getQueryTypeComplexity(queryString: string, typeWeights: TypeWeightObject): number {
20+
function getQueryTypeComplexity(
21+
queryString: DocumentNode,
22+
varibales: any | undefined,
23+
typeWeights: TypeWeightObject
24+
): number {
2025
throw Error('getQueryComplexity is not implemented.');
2126
}
2227

test/analysis/typeComplexityAnalysis.test.ts

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { parse } from 'graphql';
12
import getQueryTypeComplexity from '../../src/analysis/typeComplexityAnalysis';
23

34
/**
@@ -168,45 +169,46 @@ const typeWeights: TypeWeightObject = {
168169

169170
xdescribe('Test getQueryTypeComplexity function', () => {
170171
let query = '';
172+
const variables: any | undefined = undefined;
171173
describe('Calculates the correct type complexity for queries', () => {
172174
test('with one feild', () => {
173175
query = `Query { scalars { num } }`;
174-
expect(getQueryTypeComplexity(query, typeWeights)).toBe(2); // Query 1 + Scalars 1
176+
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(2); // Query 1 + Scalars 1
175177
});
176178

177179
test('with two or more fields', () => {
178180
query = `Query { scalars { num } test { name } }`;
179-
expect(getQueryTypeComplexity(query, typeWeights)).toBe(3); // Query 1 + scalars 1 + test 1
181+
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(3); // Query 1 + scalars 1 + test 1
180182
});
181183

182184
test('with one level of nested fields', () => {
183185
query = `Query { scalars { num, test { name } } }`;
184-
expect(getQueryTypeComplexity(query, typeWeights)).toBe(3); // Query 1 + scalars 1 + test 1
186+
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(3); // Query 1 + scalars 1 + test 1
185187
});
186188

187189
test('with multiple levels of nesting', () => {
188190
query = `Query { scalars { num, test { name, scalars { id } } } }`;
189-
expect(getQueryTypeComplexity(query, typeWeights)).toBe(4); // Query 1 + scalars 1 + test 1 + scalars 1
191+
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(4); // Query 1 + scalars 1 + test 1 + scalars 1
190192
});
191193

192194
test('with aliases', () => {
193195
query = `Query { foo: scalar { num } bar: scalar { id }}`;
194-
expect(getQueryTypeComplexity(query, typeWeights)).toBe(3); // Query 1 + scalar 1 + scalar 1
196+
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(3); // Query 1 + scalar 1 + scalar 1
195197
});
196198

197199
test('with all scalar fields', () => {
198200
query = `Query { scalars { id, num, float, bool, string } }`;
199-
expect(getQueryTypeComplexity(query, typeWeights)).toBe(2); // Query 1 + scalar 1
201+
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(2); // Query 1 + scalar 1
200202
});
201203

202204
test('with arguments and variables', () => {
203205
query = `Query { hero(episode: EMPIRE) { id, name } }`;
204-
expect(getQueryTypeComplexity(query, typeWeights)).toBe(2); // Query 1 + hero/character 1
206+
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(2); // Query 1 + hero/character 1
205207
query = `Query { human(id: 1) { id, name, appearsIn } }`;
206-
expect(getQueryTypeComplexity(query, typeWeights)).toBe(3); // Query 1 + human/character 1 + appearsIn/episode
208+
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(3); // Query 1 + human/character 1 + appearsIn/episode
207209
// argument passed in as a variable
208-
query = `Query { hero(episode: $ep) { id, name } }`;
209-
expect(getQueryTypeComplexity(query, typeWeights)).toBe(2); // Query 1 + hero/character 1
210+
query = `Query varibaleQuery ($ep: Episode){ hero(episode: $ep) { id, name } }`;
211+
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(2); // Query 1 + hero/character 1
210212
});
211213

212214
test('with fragments', () => {
@@ -225,7 +227,7 @@ xdescribe('Test getQueryTypeComplexity function', () => {
225227
appearsIn
226228
}
227229
}`;
228-
expect(getQueryTypeComplexity(query, typeWeights)).toBe(5); // Query 1 + 2*(character 1 + appearsIn/episode 1)
230+
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(5); // Query 1 + 2*(character 1 + appearsIn/episode 1)
229231
});
230232

231233
test('with inline fragments', () => {
@@ -241,7 +243,7 @@ xdescribe('Test getQueryTypeComplexity function', () => {
241243
}
242244
}
243245
}`;
244-
expect(getQueryTypeComplexity(query, typeWeights)).toBe(2); // Query 1 + hero/character 1)
246+
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(2); // Query 1 + hero/character 1)
245247
});
246248

247249
/**
@@ -255,12 +257,14 @@ xdescribe('Test getQueryTypeComplexity function', () => {
255257
name
256258
}
257259
}`;
258-
expect(getQueryTypeComplexity(query, typeWeights)).toBe(false); // ?
260+
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(false); // ?
259261
});
260262

261-
test('with lists detrmined by arguments', () => {
263+
test('with lists detrmined by arguments and variables', () => {
262264
query = `Query {reviews(episode: EMPIRE, first: 3) { stars, commentary } }`;
263-
expect(getQueryTypeComplexity(query, typeWeights)).toBe(4); // 1 Query + 3 reviews
265+
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(4); // 1 Query + 3 reviews
266+
query = `Query queryVaribales($first: Int) {reviews(episode: EMPIRE, first: $first) { stars, commentary } }`;
267+
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(4); // 1 Query + 3 reviews
264268
});
265269

266270
test('with nested lists', () => {
@@ -276,7 +280,7 @@ xdescribe('Test getQueryTypeComplexity function', () => {
276280
}
277281
}
278282
}`;
279-
expect(getQueryTypeComplexity(query, typeWeights)).toBe(17); // 1 Query + 1 human/character + (5 friends/character X 3 friends/characters)
283+
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(17); // 1 Query + 1 human/character + (5 friends/character X 3 friends/characters)
280284
});
281285

282286
test('accounting for __typename feild', () => {
@@ -294,19 +298,25 @@ xdescribe('Test getQueryTypeComplexity function', () => {
294298
}
295299
}
296300
}`;
297-
expect(getQueryTypeComplexity(query, typeWeights)).toBe(5); // 1 Query + 4 search results
301+
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(5); // 1 Query + 4 search results
298302
});
299303

300304
// todo: directives @skip, @include and custom directives
301305

302306
// todo: expand on error handling
303307
test('Throws an error if for a bad query', () => {
304308
query = `Query { hello { hi } }`; // type doesn't exist
305-
expect(() => getQueryTypeComplexity(query, typeWeights)).toThrow('Error');
309+
expect(() => getQueryTypeComplexity(parse(query), variables, typeWeights)).toThrow(
310+
'Error'
311+
);
306312
query = `Query { hero(episode: EMPIRE){ starship } }`; // field doesn't exist
307-
expect(() => getQueryTypeComplexity(query, typeWeights)).toThrow('Error');
313+
expect(() => getQueryTypeComplexity(parse(query), variables, typeWeights)).toThrow(
314+
'Error'
315+
);
308316
query = `Query { hero(episode: EMPIRE) { id, name }`; // missing a closing bracket
309-
expect(() => getQueryTypeComplexity(query, typeWeights)).toThrow('Error');
317+
expect(() => getQueryTypeComplexity(parse(query), variables, typeWeights)).toThrow(
318+
'Error'
319+
);
310320
});
311321
});
312322

0 commit comments

Comments
 (0)