Skip to content

Commit 8f5f86e

Browse files
committed
debugging comlpexity algorithm
1 parent 92a0a6a commit 8f5f86e

File tree

5 files changed

+78
-32
lines changed

5 files changed

+78
-32
lines changed

.vscode/launch.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [{
7+
"type": "node",
8+
"request": "launch",
9+
"name": "Jest Tests",
10+
"program": "${workspaceRoot}\\node_modules\\jest\\bin\\jest.js",
11+
"args": [
12+
"-i"
13+
],
14+
// "preLaunchTask": "build",
15+
"internalConsoleOptions": "openOnSessionStart",
16+
"outFiles": [
17+
"${workspaceRoot}/dist/**/*"
18+
],
19+
"envFile": "${workspaceRoot}/.env"
20+
}]
21+
}

.vscode/settings.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,19 @@
55
"source.fixAll.eslint": true
66
},
77
"editor.formatOnSave": true,
8+
"configurations": [{
9+
"type": "node",
10+
"request": "launch",
11+
"name": "Jest Tests",
12+
"program": "${workspaceRoot}\\node_modules\\jest\\bin\\jest.js",
13+
"args": [
14+
"-i"
15+
],
16+
// "preLaunchTask": "build",
17+
"internalConsoleOptions": "openOnSessionStart",
18+
"outFiles": [
19+
"${workspaceRoot}/dist/**/*"
20+
],
21+
"envFile": "${workspaceRoot}/.env"
22+
}]
823
}

src/analysis/ASTnodefunctions.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@ import {
77
Kind,
88
SelectionNode,
99
ArgumentNode,
10+
BooleanValueNode,
1011
} from 'graphql';
1112

12-
// const getArgObj = (args: ArgumentNode[]): { [index: string]: any } => {
13-
// const argObj: { [index: string]: any } = {};
14-
// for (let i = 0; i < args.length; i + 1) {
15-
// if (args[i].kind === Kind.BOOLEAN) {
16-
// argObj[args[i].name.value] = args[i].value.value;
17-
// }
18-
// }
19-
// return argObj;
20-
// };
13+
// TODO: handle variables and arguments
14+
const getArgObj = (args: ArgumentNode[]): { [index: string]: any } => {
15+
const argObj: { [index: string]: any } = {};
16+
for (let i = 0; i < args.length; i + 1) {
17+
const node = args[i];
18+
if (args[i].value.kind !== Kind.VARIABLE) {
19+
if (args[i].value.kind === Kind.INT) {
20+
// FIXME: this does not work
21+
argObj[args[i].name.value] = args[i].value;
22+
}
23+
}
24+
}
25+
return argObj;
26+
};
2127

2228
export function fieldNode(
2329
node: FieldNode,
@@ -26,18 +32,20 @@ export function fieldNode(
2632
parentName: string
2733
): number {
2834
let complexity = 0;
35+
// console.log('fieldNode', node, parentName);
2936
// check if the field name is in the type weight object.
3037
if (node.name.value.toLocaleLowerCase() in typeWeights) {
3138
// if it is, than the field is an object type, add itss type weight to the total
3239
complexity += typeWeights[node.name.value].weight;
3340
// call the function to handle selection set node with selectionSet property if it is not undefined
34-
if (node.selectionSet)
41+
if (node.selectionSet) {
3542
complexity *= selectionSetNode(
3643
node.selectionSet,
3744
typeWeights,
3845
variables,
3946
node.name.value
4047
);
48+
}
4149
} else {
4250
// otherwise the field is a scalar or a list.
4351
const fieldWeight = typeWeights[parentName].fields[node.name.value];
@@ -48,7 +56,11 @@ export function fieldNode(
4856
// otherwise the the feild weight is a list, invoke the function with variables
4957
// TODO: calculate the complexity for lists with arguments and varibales
5058
// iterate through the arguments to build the object to
51-
// complexity += fieldWeight(getArgObj(node.arguments));
59+
// eslint-disable-next-line no-lonely-if
60+
if (node.arguments) {
61+
const argumentsCopy = [...node.arguments];
62+
complexity += fieldWeight(getArgObj(argumentsCopy));
63+
}
5264
}
5365
}
5466
return complexity;
@@ -61,6 +73,7 @@ export function selectionNode(
6173
parentName: string
6274
): number {
6375
let complexity = 0;
76+
// console.log('selectionNode', node, parentName);
6477
// check the kind property against the set of selection nodes that are possible
6578
if (node.kind === Kind.FIELD) {
6679
// call the function that handle field nodes
@@ -77,6 +90,7 @@ export function selectionSetNode(
7790
parentName: string
7891
): number {
7992
let complexity = 0;
93+
console.log('selectionSetNode', node.selections.length, parentName);
8094
// iterate shrough the 'selections' array on the seletion set node
8195
for (let i = 0; i < node.selections.length; i + 1) {
8296
// call the function to handle seletion nodes
@@ -92,6 +106,7 @@ export function definitionNode(
92106
variables: any | undefined
93107
): number {
94108
let complexity = 0;
109+
console.log('definitionTode', node);
95110
// check the kind property against the set of definiton nodes that are possible
96111
if (node.kind === Kind.OPERATION_DEFINITION) {
97112
// check if the operation is in the type weights object.

src/analysis/typeComplexityAnalysis.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import { ASTNode, DocumentNode, Kind } from 'graphql';
2-
import {
3-
selectionSetNode,
4-
fieldNode,
5-
documentNode,
6-
operationDefinitionNode,
7-
} from './ASTnodefunctions';
1+
import { DocumentNode } from 'graphql';
2+
import { documentNode } from './ASTnodefunctions';
83

94
/**
105
* Calculate the complexity for the query by recursivly traversing through the query AST,

test/analysis/typeComplexityAnalysis.test.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const typeWeights: TypeWeightObject = {
103103
fields: {
104104
// FIXME: update the function def that is supposed te be here to match implementation
105105
// FIXME: add the function definition for the 'search' field which returns a list
106-
reviews: (arg, type) => arg * type.weight,
106+
reviews: (arg) => 1,
107107
},
108108
},
109109
episode: {
@@ -172,36 +172,36 @@ 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 = `query { scalars { num } }`;
176176
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(2); // Query 1 + Scalars 1
177177
});
178178

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

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

189-
test('with multiple levels of nesting', () => {
189+
xtest('with multiple levels of nesting', () => {
190190
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

194-
test('with aliases', () => {
194+
xtest('with aliases', () => {
195195
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

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

204-
test('with arguments and variables', () => {
204+
xtest('with arguments and variables', () => {
205205
query = `Query { hero(episode: EMPIRE) { id, name } }`;
206206
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(2); // Query 1 + hero/character 1
207207
query = `Query { human(id: 1) { id, name, appearsIn } }`;
@@ -212,7 +212,7 @@ describe('Test getQueryTypeComplexity function', () => {
212212
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(2); // Query 1 + hero/character 1
213213
});
214214

215-
test('with fragments', () => {
215+
xtest('with fragments', () => {
216216
query = `
217217
Query {
218218
leftComparison: hero(episode: EMPIRE) {
@@ -231,7 +231,7 @@ describe('Test getQueryTypeComplexity function', () => {
231231
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(5); // Query 1 + 2*(character 1 + appearsIn/episode 1)
232232
});
233233

234-
test('with inline fragments', () => {
234+
xtest('with inline fragments', () => {
235235
query = `
236236
Query {
237237
hero(episode: EMPIRE) {
@@ -261,15 +261,15 @@ describe('Test getQueryTypeComplexity function', () => {
261261
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(false); // ?
262262
});
263263

264-
test('with lists detrmined by arguments and variables', () => {
264+
xtest('with lists detrmined by arguments and variables', () => {
265265
query = `Query {reviews(episode: EMPIRE, first: 3) { stars, commentary } }`;
266266
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(4); // 1 Query + 3 reviews
267267
variables = { first: 3 };
268268
query = `Query queryVaribales($first: Int) {reviews(episode: EMPIRE, first: $first) { stars, commentary } }`;
269269
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(4); // 1 Query + 3 reviews
270270
});
271271

272-
test('with nested lists', () => {
272+
xtest('with nested lists', () => {
273273
query = `
274274
query {
275275
human(id: 1) {
@@ -285,7 +285,7 @@ describe('Test getQueryTypeComplexity function', () => {
285285
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(17); // 1 Query + 1 human/character + (5 friends/character X 3 friends/characters)
286286
});
287287

288-
test('accounting for __typename feild', () => {
288+
xtest('accounting for __typename feild', () => {
289289
query = `
290290
query {
291291
search(text: "an", first: 4) {
@@ -306,7 +306,7 @@ describe('Test getQueryTypeComplexity function', () => {
306306
// todo: directives @skip, @include and custom directives
307307

308308
// todo: expand on error handling
309-
test('Throws an error if for a bad query', () => {
309+
xtest('Throws an error if for a bad query', () => {
310310
query = `Query { hello { hi } }`; // type doesn't exist
311311
expect(() => getQueryTypeComplexity(parse(query), variables, typeWeights)).toThrow(
312312
'Error'

0 commit comments

Comments
 (0)