Skip to content

Commit 57a8cd8

Browse files
committed
type complexity analysis working for first test. changed i + 1 to i += 1
1 parent 8f5f86e commit 57a8cd8

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

.vscode/launch.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
"type": "node",
88
"request": "launch",
99
"name": "Jest Tests",
10-
"program": "${workspaceRoot}\\node_modules\\jest\\bin\\jest.js",
10+
"program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
1111
"args": [
12-
"-i"
12+
"-i", "--verbose", "--no-cache"
1313
],
1414
// "preLaunchTask": "build",
15-
"internalConsoleOptions": "openOnSessionStart",
16-
"outFiles": [
17-
"${workspaceRoot}/dist/**/*"
18-
],
19-
"envFile": "${workspaceRoot}/.env"
15+
// "internalConsoleOptions": "openOnSessionStart",
16+
// "outFiles": [
17+
// "${workspaceRoot}/dist/**/*"
18+
// ],
19+
// "envFile": "${workspaceRoot}/.env"
2020
}]
2121
}

src/analysis/ASTnodefunctions.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
Kind,
88
SelectionNode,
99
ArgumentNode,
10-
BooleanValueNode,
1110
} from 'graphql';
1211

1312
// TODO: handle variables and arguments
@@ -39,7 +38,7 @@ export function fieldNode(
3938
complexity += typeWeights[node.name.value].weight;
4039
// call the function to handle selection set node with selectionSet property if it is not undefined
4140
if (node.selectionSet) {
42-
complexity *= selectionSetNode(
41+
complexity += selectionSetNode(
4342
node.selectionSet,
4443
typeWeights,
4544
variables,
@@ -90,9 +89,8 @@ export function selectionSetNode(
9089
parentName: string
9190
): number {
9291
let complexity = 0;
93-
console.log('selectionSetNode', node.selections.length, parentName);
9492
// iterate shrough the 'selections' array on the seletion set node
95-
for (let i = 0; i < node.selections.length; i + 1) {
93+
for (let i = 0; i < node.selections.length; i += 1) {
9694
// call the function to handle seletion nodes
9795
// pass the current parent through because selection sets act only as intermediaries
9896
complexity += selectionNode(node.selections[i], typeWeights, variables, parentName);
@@ -106,7 +104,6 @@ export function definitionNode(
106104
variables: any | undefined
107105
): number {
108106
let complexity = 0;
109-
console.log('definitionTode', node);
110107
// check the kind property against the set of definiton nodes that are possible
111108
if (node.kind === Kind.OPERATION_DEFINITION) {
112109
// check if the operation is in the type weights object.
@@ -134,7 +131,7 @@ export function documentNode(
134131
): number {
135132
let complexity = 0;
136133
// iterate through 'definitions' array on the document node
137-
for (let i = 0; i < node.definitions.length; i + 1) {
134+
for (let i = 0; i < node.definitions.length; i += 1) {
138135
// call the function to handle the various types of definition nodes
139136
complexity += definitionNode(node.definitions[i], typeWeights, variables);
140137
}

test/analysis/typeComplexityAnalysis.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -172,49 +172,49 @@ describe('Test getQueryTypeComplexity function', () => {
172172
let variables: any | undefined;
173173
describe('Calculates the correct type complexity for queries', () => {
174174
test('with one feild', () => {
175-
query = `query { scalars { num } }`;
175+
query = `uery { scalars { num } }`;
176176
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(2); // Query 1 + Scalars 1
177177
});
178178

179-
xtest('with two or more fields', () => {
180-
query = `Query { scalars { num } test { name } }`;
179+
test('with two or more fields', () => {
180+
query = `query { scalars { num } test { name } }`;
181181
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(3); // Query 1 + scalars 1 + test 1
182182
});
183183

184184
xtest('with one level of nested fields', () => {
185-
query = `Query { scalars { num, test { name } } }`;
185+
query = `query { scalars { num, test { name } } }`;
186186
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(3); // Query 1 + scalars 1 + test 1
187187
});
188188

189189
xtest('with multiple levels of nesting', () => {
190-
query = `Query { scalars { num, test { name, scalars { id } } } }`;
190+
query = `query { scalars { num, test { name, scalars { id } } } }`;
191191
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(4); // Query 1 + scalars 1 + test 1 + scalars 1
192192
});
193193

194194
xtest('with aliases', () => {
195-
query = `Query { foo: scalar { num } bar: scalar { id }}`;
195+
query = `query { foo: scalar { num } bar: scalar { id }}`;
196196
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(3); // Query 1 + scalar 1 + scalar 1
197197
});
198198

199199
xtest('with all scalar fields', () => {
200-
query = `Query { scalars { id, num, float, bool, string } }`;
200+
query = `query { scalars { id, num, float, bool, string } }`;
201201
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(2); // Query 1 + scalar 1
202202
});
203203

204204
xtest('with arguments and variables', () => {
205-
query = `Query { hero(episode: EMPIRE) { id, name } }`;
205+
query = `query { hero(episode: EMPIRE) { id, name } }`;
206206
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(2); // Query 1 + hero/character 1
207-
query = `Query { human(id: 1) { id, name, appearsIn } }`;
207+
query = `query { human(id: 1) { id, name, appearsIn } }`;
208208
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(3); // Query 1 + human/character 1 + appearsIn/episode
209209
// argument passed in as a variable
210210
variables = { ep: 'EMPIRE' };
211-
query = `Query varibaleQuery ($ep: Episode){ hero(episode: $ep) { id, name } }`;
211+
query = `query varibaleQuery ($ep: Episode){ hero(episode: $ep) { id, name } }`;
212212
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(2); // Query 1 + hero/character 1
213213
});
214214

215215
xtest('with fragments', () => {
216216
query = `
217-
Query {
217+
query {
218218
leftComparison: hero(episode: EMPIRE) {
219219
...comparisonFields
220220
}
@@ -233,7 +233,7 @@ describe('Test getQueryTypeComplexity function', () => {
233233

234234
xtest('with inline fragments', () => {
235235
query = `
236-
Query {
236+
query {
237237
hero(episode: EMPIRE) {
238238
name
239239
... on Droid {
@@ -252,7 +252,7 @@ describe('Test getQueryTypeComplexity function', () => {
252252
*/
253253
xtest('with lists of unknown size', () => {
254254
query = `
255-
Query {
255+
query {
256256
search(text: 'hi') {
257257
id
258258
name
@@ -307,15 +307,15 @@ describe('Test getQueryTypeComplexity function', () => {
307307

308308
// todo: expand on error handling
309309
xtest('Throws an error if for a bad query', () => {
310-
query = `Query { hello { hi } }`; // type doesn't exist
310+
query = `query { hello { hi } }`; // type doesn't exist
311311
expect(() => getQueryTypeComplexity(parse(query), variables, typeWeights)).toThrow(
312312
'Error'
313313
);
314-
query = `Query { hero(episode: EMPIRE){ starship } }`; // field doesn't exist
314+
query = `query { hero(episode: EMPIRE){ starship } }`; // field doesn't exist
315315
expect(() => getQueryTypeComplexity(parse(query), variables, typeWeights)).toThrow(
316316
'Error'
317317
);
318-
query = `Query { hero(episode: EMPIRE) { id, name }`; // missing a closing bracket
318+
query = `query { hero(episode: EMPIRE) { id, name }`; // missing a closing bracket
319319
expect(() => getQueryTypeComplexity(parse(query), variables, typeWeights)).toThrow(
320320
'Error'
321321
);

0 commit comments

Comments
 (0)