Skip to content

Commit a475a54

Browse files
committed
Merge branch 'em/lists-variables-nesting-refactoring' of github.com:oslabs-beta/GraphQL-Gate into sh/non-null-tests
2 parents f07b9be + 4602833 commit a475a54

File tree

5 files changed

+41
-20
lines changed

5 files changed

+41
-20
lines changed

src/@types/buildTypeWeights.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ export interface TypeWeightConfig {
2121
scalar?: number;
2222
connection?: number;
2323
}
24+
export interface TypeWeightSet {
25+
mutation: number;
26+
query: number;
27+
object: number;
28+
scalar: number;
29+
connection: number;
30+
}
2431
type Variables = {
2532
[index: string]: readonly unknown;
2633
};

src/analysis/buildTypeWeights.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,27 @@ import { ObjMap } from 'graphql/jsutils/ObjMap';
2323
import { GraphQLSchema } from 'graphql/type/schema';
2424
import {
2525
TypeWeightConfig,
26+
TypeWeightSet,
2627
TypeWeightObject,
2728
Variables,
2829
Type,
29-
Field,
3030
} from '../@types/buildTypeWeights';
3131

3232
export const KEYWORDS = ['first', 'last', 'limit'];
33-
type ListType =
34-
| GraphQLScalarType<unknown, unknown>
35-
| GraphQLObjectType<any, any>
36-
| GraphQLList<GraphQLOutputType>
37-
| GraphQLOutputType;
33+
3834
// These variables exist to provide a default value for typescript when accessing a weight
3935
// since all props are optioal in TypeWeightConfig
4036
const DEFAULT_MUTATION_WEIGHT = 10;
4137
const DEFAULT_OBJECT_WEIGHT = 1;
4238
const DEFAULT_SCALAR_WEIGHT = 0;
4339
const DEFAULT_CONNECTION_WEIGHT = 2;
4440
const DEFAULT_QUERY_WEIGHT = 1;
45-
export const defaultTypeWeightsConfig: TypeWeightConfig = {
41+
export const defaultTypeWeightsConfig: TypeWeightSet = {
4642
mutation: DEFAULT_MUTATION_WEIGHT,
4743
object: DEFAULT_OBJECT_WEIGHT,
4844
scalar: DEFAULT_SCALAR_WEIGHT,
4945
connection: DEFAULT_CONNECTION_WEIGHT,
46+
query: DEFAULT_QUERY_WEIGHT,
5047
};
5148

5249
// FIXME: What about Interface defaults
@@ -56,24 +53,24 @@ export const defaultTypeWeightsConfig: TypeWeightConfig = {
5653
*
5754
* @param {(GraphQLObjectType | GraphQLInterfaceType)} type
5855
* @param {TypeWeightObject} typeWeightObject
59-
* @param {TypeWeightConfig} typeWeights
56+
* @param {TypeWeightSet} typeWeights
6057
* @return {*} {Type}
6158
*/
6259
function parseObjectFields(
6360
type: GraphQLObjectType | GraphQLInterfaceType,
6461
typeWeightObject: TypeWeightObject,
65-
typeWeights: TypeWeightConfig
62+
typeWeights: TypeWeightSet
6663
): Type {
6764
let result: Type;
6865
switch (type.name) {
6966
case 'Query':
70-
result = { weight: typeWeights.query || DEFAULT_QUERY_WEIGHT, fields: {} };
67+
result = { weight: typeWeights.query, fields: {} };
7168
break;
7269
case 'Mutation':
73-
result = { weight: typeWeights.mutation || DEFAULT_MUTATION_WEIGHT, fields: {} };
70+
result = { weight: typeWeights.mutation, fields: {} };
7471
break;
7572
default:
76-
result = { weight: typeWeights.object || DEFAULT_OBJECT_WEIGHT, fields: {} };
73+
result = { weight: typeWeights.object, fields: {} };
7774
break;
7875
}
7976

@@ -88,7 +85,7 @@ function parseObjectFields(
8885
(isNonNullType(fieldType) && isScalarType(fieldType.ofType))
8986
) {
9087
result.fields[field] = {
91-
weight: typeWeights.scalar || DEFAULT_SCALAR_WEIGHT,
88+
weight: typeWeights.scalar,
9289
};
9390
} else if (
9491
isInterfaceType(fieldType) ||
@@ -105,7 +102,7 @@ function parseObjectFields(
105102
if (isScalarType(listType) && typeWeights.scalar === 0) {
106103
// list won't compound if weight is zero
107104
result.fields[field] = {
108-
weight: typeWeights.scalar || DEFAULT_SCALAR_WEIGHT,
105+
weight: typeWeights.scalar,
109106
};
110107
} else if (isEnumType(listType) && typeWeights.scalar === 0) {
111108
// list won't compound if weight of enum is zero
@@ -131,7 +128,7 @@ function parseObjectFields(
131128
);
132129
const weight = isCompositeType(listType)
133130
? typeWeightObject[listType.name.toLowerCase()].weight
134-
: typeWeights.scalar || DEFAULT_SCALAR_WEIGHT; // Note this includes enums
131+
: typeWeights.scalar; // Note this includes enums
135132
if (limitArg) {
136133
const node: ValueNode = limitArg.value;
137134
let multiplier = 1;
@@ -175,7 +172,7 @@ function parseObjectFields(
175172
* @param typeWeights
176173
* @returns
177174
*/
178-
function parseTypes(schema: GraphQLSchema, typeWeights: TypeWeightConfig): TypeWeightObject {
175+
function parseTypes(schema: GraphQLSchema, typeWeights: TypeWeightSet): TypeWeightObject {
179176
const typeMap: ObjMap<GraphQLNamedType> = schema.getTypeMap();
180177

181178
const result: TypeWeightObject = {};
@@ -193,13 +190,13 @@ function parseTypes(schema: GraphQLSchema, typeWeights: TypeWeightConfig): TypeW
193190
} else if (isEnumType(currentType)) {
194191
result[typeName] = {
195192
fields: {},
196-
weight: typeWeights.scalar || DEFAULT_SCALAR_WEIGHT,
193+
weight: typeWeights.scalar,
197194
};
198195
} else if (isUnionType(currentType)) {
199196
// FIXME: will need information on fields inorder calculate comlpextiy
200197
result[typeName] = {
201198
fields: {},
202-
weight: typeWeights.object || DEFAULT_OBJECT_WEIGHT,
199+
weight: typeWeights.object,
203200
};
204201
} else {
205202
// ? what else can get through here
@@ -230,7 +227,7 @@ function buildTypeWeightsFromSchema(
230227
if (!schema) throw new Error('Missing Argument: schema is required');
231228

232229
// Merge the provided type weights with the default to account for missing values
233-
const typeWeights: TypeWeightConfig = {
230+
const typeWeights: TypeWeightSet = {
234231
...defaultTypeWeightsConfig,
235232
...typeWeightsConfig,
236233
};

test/analysis/buildTypeWeights.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,18 @@ describe('Test buildTypeWeightsFromSchema function', () => {
656656
expect(typeWeightObject).toEqual(expectedOutput);
657657
});
658658

659+
test('object parameter set to 0', () => {
660+
const typeWeightObject = buildTypeWeightsFromSchema(schema, {
661+
object: 0,
662+
});
663+
664+
expectedOutput.user.weight = 0;
665+
expectedOutput.movie.weight = 0;
666+
// expectedOutput.query.weight = 2;
667+
668+
expect(typeWeightObject).toEqual(expectedOutput);
669+
});
670+
659671
test('scalar parameter', () => {
660672
const typeWeightObject = buildTypeWeightsFromSchema(schema, {
661673
scalar: 2,

test/analysis/typeComplexityAnalysis.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ describe('Test getQueryTypeComplexity function', () => {
234234
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(2); // Query 1 + Scalars 1
235235
});
236236

237+
xtest('with one with capital first letter for field', () => {
238+
query = `query { Scalars { num } }`;
239+
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(2); // Query 1 + Scalars 1
240+
});
241+
237242
test('with two or more fields', () => {
238243
query = `query { scalars { num } test { name } }`;
239244
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(3); // Query 1 + scalars 1 + test 1

test/analysis/weightFunction.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ describe('Weight Function correctly parses Argument Nodes if', () => {
113113
});
114114
const query = `query { heroes(episode: NEWHOPE, first: 3) { stars, episode } }`;
115115
const queryAST: DocumentNode = parse(query);
116-
expect(getQueryTypeComplexity(queryAST, {}, customTypeWeights)).toBe(4); // 1 query and 4 enums
116+
expect(getQueryTypeComplexity(queryAST, {}, customTypeWeights)).toBe(1); // 1 query
117117
});
118118
test('a custom scalar weight was set to greater than 0', () => {
119119
const customTypeWeights: TypeWeightObject = buildTypeWeightsFromSchema(schema, {

0 commit comments

Comments
 (0)