Skip to content

Commit f923041

Browse files
authored
Merge pull request #102 from oslabs-beta/em/names
Updated name of ASTParser/ComplexityAnalysis
2 parents 149a719 + 23fe80e commit f923041

File tree

6 files changed

+47
-47
lines changed

6 files changed

+47
-47
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ This package exposes 3 additional functionalities which comprise the internals o
219219
const typeWeights: TypeWeightObject = typeWeightsFromSchema(schema);
220220
```
221221

222-
2. #### `ComplexityAnalysis` | class to calculate the complexity of the query based on the type weights and variables
222+
2. #### `QueryParser` | class to calculate the complexity of the query based on the type weights and variables
223223

224224
- `typeWeights: TypeWeightObject`
225225
- `variables: Variables` | variables on request
@@ -234,8 +234,8 @@ This package exposes 3 additional functionalities which comprise the internals o
234234

235235
let queryAST: DocumentNode = parse(`...`);
236236

237-
const queryParser: ASTParser = new ComplexityAnalysis(typeWeights, variables);
238-
237+
const queryParser: QueryParser = new QueryParser(typeWeights, variables);
238+
239239
// query must be validatied against the schema before processing the query
240240
const validationErrors = validate(schema, queryAST);
241241

src/analysis/ASTParser.ts renamed to src/analysis/QueryParser.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ import { FieldWeight, TypeWeightObject, Variables } from '../@types/buildTypeWei
2626
* | | | \
2727
* | Field Node | fragmentCache
2828
* | | |
29-
* |---calculateCast |
29+
* |<--calculateCast |
3030
* | |
31-
* |-------------------|
31+
* |<------------------|
3232
*/
3333

34-
class ASTParser {
34+
class QueryParser {
3535
private typeWeights: TypeWeightObject;
3636

3737
private depth: number;
@@ -85,7 +85,7 @@ class ASTParser {
8585
const parentType = this.typeWeights[parentName];
8686
if (!parentType) {
8787
throw new Error(
88-
`ERROR: ASTParser Failed to obtain parentType for parent: ${parentName} and node: ${node.name.value}`
88+
`ERROR: QueryParser Failed to obtain parentType for parent: ${parentName} and node: ${node.name.value}`
8989
);
9090
}
9191

@@ -114,7 +114,7 @@ class ASTParser {
114114
complexity += this.calculateCost(node, parentName, typeName, typeWeight);
115115
} else {
116116
throw new Error(
117-
`ERROR: ASTParser Failed to obtain resolved type name or weight for node: ${parentName}.${node.name.value}`
117+
`ERROR: QueryParser Failed to obtain resolved type name or weight for node: ${parentName}.${node.name.value}`
118118
);
119119
}
120120
} else {
@@ -126,19 +126,19 @@ class ASTParser {
126126
complexity += typeWeight;
127127
} else {
128128
throw new Error(
129-
`ERROR: ASTParser Failed to obtain type weight for ${parentName}.${node.name.value}`
129+
`ERROR: QueryParser Failed to obtain type weight for ${parentName}.${node.name.value}`
130130
);
131131
}
132132
} else {
133133
throw new Error(
134-
`ERROR: ASTParser Failed to obtain type name for ${parentName}.${node.name.value}`
134+
`ERROR: QueryParser Failed to obtain type name for ${parentName}.${node.name.value}`
135135
);
136136
}
137137
}
138138
return complexity;
139139
} catch (err) {
140140
throw new Error(
141-
`ERROR: ASTParser.fieldNode Uncaught error handling ${parentName}.${
141+
`ERROR: QueryParser.fieldNode Uncaught error handling ${parentName}.${
142142
node.name.value
143143
}\n
144144
${err instanceof Error && err.stack}`
@@ -216,7 +216,7 @@ class ASTParser {
216216
complexity += this.selectionSetNode(node.selectionSet, namedType);
217217
this.depth += 1;
218218
} else {
219-
throw new Error(`ERROR: ASTParser.selectionNode: node type not supported`);
219+
throw new Error(`ERROR: QueryParser.selectionNode: node type not supported`);
220220
}
221221

222222
this.depth -= 1;
@@ -283,7 +283,7 @@ class ASTParser {
283283
//
284284
// // Other types include TypeSystemDefinitionNode (Schema, Type, Directvie) and
285285
// // TypeSystemExtensionNode(Schema, Type);
286-
// throw new Error(`ERROR: ASTParser.definitionNode: ${node.kind} type not supported`);
286+
// throw new Error(`ERROR: QueryParser.definitionNode: ${node.kind} type not supported`);
287287
// }
288288
return complexity;
289289
}
@@ -306,4 +306,4 @@ class ASTParser {
306306
}
307307
}
308308

309-
export default ASTParser;
309+
export default QueryParser;

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ export { default as expressGraphQLRateLimiter } from './middleware/index.js';
22

33
export { default as rateLimiter } from './middleware/rateLimiterSetup.js';
44

5-
export { default as ComplexityAnalysis } from './analysis/ASTParser.js';
5+
export { default as QueryParser } from './analysis/QueryParser.js';
66

77
export { default as typeWeightsFromSchema } from './analysis/buildTypeWeights.js';

src/middleware/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import setupRateLimiter from './rateLimiterSetup';
77
import { ExpressMiddlewareConfig, ExpressMiddlewareSet } from '../@types/expressMiddleware';
88
import { RateLimiterResponse } from '../@types/rateLimit';
99
import { connect } from '../utils/redis';
10-
import ASTParser from '../analysis/ASTParser';
10+
import QueryParser from '../analysis/QueryParser';
1111

1212
/**
1313
* Primary entry point for adding GraphQL Rate Limiting middleware to an Express Server
@@ -165,7 +165,7 @@ export default function expressGraphQLRateLimiter(
165165
res.status(400).json({ errors: validationErrors });
166166
}
167167

168-
const queryParser = new ASTParser(typeWeightObject, variables);
168+
const queryParser = new QueryParser(typeWeightObject, variables);
169169
const queryComplexity = queryParser.processQuery(queryAST);
170170

171171
try {

test/analysis/typeComplexityAnalysis.test.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { parse } from 'graphql';
2-
import ASTParser from '../../src/analysis/ASTParser';
2+
import QueryParser from '../../src/analysis/QueryParser';
33
import { TypeWeightObject, Variables } from '../../src/@types/buildTypeWeights';
44

55
/**
@@ -122,7 +122,7 @@ let nonNullMockWeightFunction: jest.Mock<number, []>;
122122
// this object is created by the schema above for use in all the tests below
123123
let typeWeights: TypeWeightObject;
124124

125-
let queryParser: ASTParser;
125+
let queryParser: QueryParser;
126126

127127
describe('Test getQueryTypeComplexity function', () => {
128128
beforeEach(() => {
@@ -281,7 +281,7 @@ describe('Test getQueryTypeComplexity function', () => {
281281

282282
describe('Calculates the correct type complexity for queries', () => {
283283
beforeEach(() => {
284-
queryParser = new ASTParser(typeWeights, variables);
284+
queryParser = new QueryParser(typeWeights, variables);
285285
});
286286
test('with one feild', () => {
287287
query = `query { scalars { num } }`;
@@ -325,7 +325,7 @@ describe('Test getQueryTypeComplexity function', () => {
325325
expect(queryParser.processQuery(parse(query))).toBe(2); // Query 1 + human/character 1
326326
// argument passed in as a variable
327327
variables = { ep: 'EMPIRE' };
328-
queryParser = new ASTParser(typeWeights, variables);
328+
queryParser = new QueryParser(typeWeights, variables);
329329
query = `query variableQuery ($ep: Episode){ hero(episode: $ep) { id, name } }`;
330330
expect(queryParser.processQuery(parse(query))).toBe(2); // Query 1 + hero/character 1
331331
});
@@ -370,7 +370,7 @@ describe('Test getQueryTypeComplexity function', () => {
370370
}`;
371371
mockCharacterFriendsFunction.mockReturnValueOnce(3);
372372
variables = { first: 3 };
373-
queryParser = new ASTParser(typeWeights, variables);
373+
queryParser = new QueryParser(typeWeights, variables);
374374
// Query 1 + 2*(character 1 + appearsIn/episode 0 + 3 * friends/character 1)
375375
expect(queryParser.processQuery(parse(query))).toBe(9);
376376
});
@@ -395,7 +395,7 @@ describe('Test getQueryTypeComplexity function', () => {
395395
}`;
396396
mockCharacterFriendsFunction.mockReturnValueOnce(3);
397397
variables = { first: 3 };
398-
queryParser = new ASTParser(typeWeights, variables);
398+
queryParser = new QueryParser(typeWeights, variables);
399399
// Query 1 + 2*(character 1 + appearsIn/episode 0 + 3 * friends/character 1)
400400
expect(queryParser.processQuery(parse(query))).toBe(9);
401401
});
@@ -421,7 +421,7 @@ describe('Test getQueryTypeComplexity function', () => {
421421
mockCharacterFriendsFunction.mockReturnValueOnce(3);
422422

423423
variables = { first: 3 };
424-
queryParser = new ASTParser(typeWeights, variables);
424+
queryParser = new QueryParser(typeWeights, variables);
425425
// Query 1 + 2*(character 1 + appearsIn/episode 0 + 3 * friends/character 1)
426426
expect(queryParser.processQuery(parse(query))).toBe(9);
427427

@@ -441,7 +441,7 @@ describe('Test getQueryTypeComplexity function', () => {
441441
}`;
442442
mockCharacterFriendsFunction.mockReturnValueOnce(3);
443443
variables = { first: 3 };
444-
queryParser = new ASTParser(typeWeights, variables);
444+
queryParser = new QueryParser(typeWeights, variables);
445445
// Query 1 + 2*(character 1 + 0 selectionCost)
446446
expect(queryParser.processQuery(parse(query))).toBe(3);
447447
});
@@ -519,7 +519,7 @@ describe('Test getQueryTypeComplexity function', () => {
519519
},
520520
};
521521
variables = {};
522-
queryParser = new ASTParser(unionTypeWeights, variables);
522+
queryParser = new QueryParser(unionTypeWeights, variables);
523523
});
524524
test('that have a complexity of zero', () => {
525525
query = `
@@ -581,7 +581,7 @@ describe('Test getQueryTypeComplexity function', () => {
581581
}`;
582582
mockCharacterFriendsFunction.mockReturnValueOnce(3);
583583
variables = { first: 3 };
584-
queryParser = new ASTParser(unionTypeWeights, variables);
584+
queryParser = new QueryParser(unionTypeWeights, variables);
585585
// Query 1 + 1 hero + 3 friends/character
586586
expect(queryParser.processQuery(parse(query))).toBe(5);
587587
});
@@ -604,7 +604,7 @@ describe('Test getQueryTypeComplexity function', () => {
604604
}`;
605605
mockDroidFriendsFunction.mockReturnValueOnce(3);
606606
variables = { first: 3 };
607-
queryParser = new ASTParser(unionTypeWeights, variables);
607+
queryParser = new QueryParser(unionTypeWeights, variables);
608608
// Query 1 + 1 hero + max(Droid 3, Human 0) = 5
609609
expect(queryParser.processQuery(parse(query))).toBe(5);
610610
});
@@ -768,7 +768,7 @@ describe('Test getQueryTypeComplexity function', () => {
768768
}`;
769769
mockDroidFriendsFunction.mockReturnValueOnce(3);
770770
variables = { first: 3 };
771-
queryParser = new ASTParser(typeWeights, variables);
771+
queryParser = new QueryParser(typeWeights, variables);
772772
// Query 1 + 1 hero + max(Droid 3, Human 0) = 5
773773
expect(queryParser.processQuery(parse(query))).toBe(5);
774774
});
@@ -838,7 +838,7 @@ describe('Test getQueryTypeComplexity function', () => {
838838
expect(mockWeightFunction.mock.calls[0].length).toBe(3); // calling with arguments and variables
839839

840840
variables = { first: 4 };
841-
queryParser = new ASTParser(typeWeights, variables);
841+
queryParser = new QueryParser(typeWeights, variables);
842842
mockWeightFunction.mockReturnValueOnce(4);
843843
query = `query queryVariables($first: Int) {reviews(episode: EMPIRE, first: $first) { stars, commentary } }`;
844844
expect(queryParser.processQuery(parse(query))).toBe(5); // 1 Query + 4 reviews
@@ -989,7 +989,7 @@ describe('Test getQueryTypeComplexity function', () => {
989989
});
990990
test('with arguments and varibales', () => {
991991
variables = { directive: false };
992-
queryParser = new ASTParser(typeWeights, variables);
992+
queryParser = new QueryParser(typeWeights, variables);
993993
query = `query ($directive: Boolean!){
994994
hero(episode: EMPIRE) {
995995
id, name
@@ -1003,7 +1003,7 @@ describe('Test getQueryTypeComplexity function', () => {
10031003
// 1 query + 1 hero + 1 human
10041004
expect(queryParser.processQuery(parse(query))).toBe(3);
10051005
variables = { directive: true };
1006-
queryParser = new ASTParser(typeWeights, variables);
1006+
queryParser = new QueryParser(typeWeights, variables);
10071007
query = `query ($directive: Boolean!){
10081008
hero(episode: EMPIRE) {
10091009
id, name
@@ -1099,7 +1099,7 @@ describe('Test getQueryTypeComplexity function', () => {
10991099
episode
11001100
}
11011101
}`;
1102-
queryParser = new ASTParser(typeWeights, variables);
1102+
queryParser = new QueryParser(typeWeights, variables);
11031103
expect(queryParser.processQuery(parse(query))).toBe(11); // Mutation 10 + review 1
11041104
});
11051105

@@ -1108,7 +1108,7 @@ describe('Test getQueryTypeComplexity function', () => {
11081108
query = `mutation createReviewMutation($review: ReviewInput!) {
11091109
createReview(episode: Empire, review: $review)
11101110
}`;
1111-
queryParser = new ASTParser(typeWeights, variables);
1111+
queryParser = new QueryParser(typeWeights, variables);
11121112
expect(queryParser.processQuery(parse(query))).toBe(11); // Mutation 10 + review 1
11131113
});
11141114

@@ -1127,14 +1127,14 @@ describe('Test getQueryTypeComplexity function', () => {
11271127
name
11281128
}
11291129
}`;
1130-
queryParser = new ASTParser(typeWeights, variables);
1130+
queryParser = new QueryParser(typeWeights, variables);
11311131
expect(queryParser.processQuery(parse(query))).toBe(13); // Mutation 10 + review 1 + query 1 + character 1
11321132
});
11331133
});
11341134

11351135
describe('Calculates the depth of the query', () => {
11361136
beforeEach(() => {
1137-
queryParser = new ASTParser(typeWeights, {});
1137+
queryParser = new QueryParser(typeWeights, {});
11381138
});
11391139
test('with one feild', () => {
11401140
query = `query { scalars { num } }`;

test/analysis/weightFunction.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'ts-jest';
22
import { buildSchema, DocumentNode, parse } from 'graphql';
33
import { TypeWeightObject } from '../../src/@types/buildTypeWeights';
44
import buildTypeWeightsFromSchema from '../../src/analysis/buildTypeWeights';
5-
import ASTParser from '../../src/analysis/ASTParser';
5+
import QueryParser from '../../src/analysis/QueryParser';
66
// Test the weight function generated by the typeweights object when a limiting keyword is provided
77

88
// Test cases:
@@ -41,10 +41,10 @@ describe('Weight Function correctly parses Argument Nodes if', () => {
4141
// building the typeWeights object here since we're testing the weight function created in
4242
// the typeWeights object
4343
const typeWeights: TypeWeightObject = buildTypeWeightsFromSchema(schema);
44-
let queryParser: ASTParser;
44+
let queryParser: QueryParser;
4545
describe('a default value is provided in the schema', () => {
4646
beforeEach(() => {
47-
queryParser = new ASTParser(typeWeights, {});
47+
queryParser = new QueryParser(typeWeights, {});
4848
});
4949
test('and a value is not provided with the query', () => {
5050
const query = `query { reviews(episode: NEWHOPE) { stars, episode } }`;
@@ -61,9 +61,9 @@ describe('Weight Function correctly parses Argument Nodes if', () => {
6161
test('and the argument is passed in as a variable', () => {
6262
const query = `query variableQuery ($items: Int){ reviews(episode: NEWHOPE, first: $items) { stars, episode } }`;
6363
const queryAST: DocumentNode = parse(query);
64-
queryParser = new ASTParser(typeWeights, { items: 7, first: 4 });
64+
queryParser = new QueryParser(typeWeights, { items: 7, first: 4 });
6565
expect(queryParser.processQuery(queryAST)).toBe(8);
66-
queryParser = new ASTParser(typeWeights, { first: 4, items: 7 });
66+
queryParser = new QueryParser(typeWeights, { first: 4, items: 7 });
6767
expect(queryParser.processQuery(queryAST)).toBe(8);
6868
});
6969
});
@@ -85,7 +85,7 @@ describe('Weight Function correctly parses Argument Nodes if', () => {
8585
test('and the argument is passed in as a variable', () => {
8686
const query = `query variableQuery ($items: Int){ heroes(episode: NEWHOPE, first: $items) { stars, episode } }`;
8787
const queryAST: DocumentNode = parse(query);
88-
queryParser = new ASTParser(typeWeights, { items: 7 });
88+
queryParser = new QueryParser(typeWeights, { items: 7 });
8989
expect(queryParser.processQuery(queryAST)).toBe(8);
9090
});
9191
});
@@ -108,7 +108,7 @@ describe('Weight Function correctly parses Argument Nodes if', () => {
108108
const customTypeWeights: TypeWeightObject = buildTypeWeightsFromSchema(schema, {
109109
object: 3,
110110
});
111-
queryParser = new ASTParser(customTypeWeights, {});
111+
queryParser = new QueryParser(customTypeWeights, {});
112112
const query = `query { heroes(episode: NEWHOPE, first: 3) { stars, episode } }`;
113113
const queryAST: DocumentNode = parse(query);
114114
expect(queryParser.processQuery(queryAST)).toBe(10);
@@ -118,7 +118,7 @@ describe('Weight Function correctly parses Argument Nodes if', () => {
118118
const customTypeWeights: TypeWeightObject = buildTypeWeightsFromSchema(schema, {
119119
object: 0,
120120
});
121-
queryParser = new ASTParser(customTypeWeights, {});
121+
queryParser = new QueryParser(customTypeWeights, {});
122122
const query = `query { heroes(episode: NEWHOPE, first: 3) { stars, episode } }`;
123123
const queryAST: DocumentNode = parse(query);
124124
expect(queryParser.processQuery(queryAST)).toBe(1); // 1 query
@@ -127,7 +127,7 @@ describe('Weight Function correctly parses Argument Nodes if', () => {
127127
const customTypeWeights: TypeWeightObject = buildTypeWeightsFromSchema(schema, {
128128
scalar: 2,
129129
});
130-
queryParser = new ASTParser(customTypeWeights, {});
130+
queryParser = new QueryParser(customTypeWeights, {});
131131
const query = `query { heroes(episode: NEWHOPE, first: 3) { stars, episode } }`;
132132
const queryAST: DocumentNode = parse(query);
133133
expect(queryParser.processQuery(queryAST)).toBe(16);
@@ -136,7 +136,7 @@ describe('Weight Function correctly parses Argument Nodes if', () => {
136136
test('variable names matching limiting keywords do not interfere with scalar argument values', () => {
137137
const query = `query variableQuery ($items: Int){ heroes(episode: NEWHOPE, first: 3) { stars, episode } }`;
138138
const queryAST: DocumentNode = parse(query);
139-
queryParser = new ASTParser(typeWeights, { first: 7 });
139+
queryParser = new QueryParser(typeWeights, { first: 7 });
140140
expect(queryParser.processQuery(queryAST)).toBe(4);
141141
});
142142

@@ -154,7 +154,7 @@ describe('Weight Function correctly parses Argument Nodes if', () => {
154154
const customTypeWeights: TypeWeightObject = buildTypeWeightsFromSchema(schema, {
155155
scalar: 2,
156156
});
157-
queryParser = new ASTParser(customTypeWeights, {});
157+
queryParser = new QueryParser(customTypeWeights, {});
158158
const query = `query { reviews(episode: NEWHOPE, first: 2) {stars, scalarList(last: 3) }}`;
159159
expect(queryParser.processQuery(parse(query))).toBe(19); // 1 Query + 2 reviews + 2 * (2 stars + (3 * 2 scalarList)
160160
});

0 commit comments

Comments
 (0)