Skip to content

Commit 1a75e56

Browse files
committed
converted type weight keys to lowercase
1 parent 68ac079 commit 1a75e56

File tree

2 files changed

+44
-42
lines changed

2 files changed

+44
-42
lines changed

src/analysis/buildTypeWeights.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { Maybe } from 'graphql/jsutils/Maybe';
2020
import { ObjMap } from 'graphql/jsutils/ObjMap';
2121
import { GraphQLSchema } from 'graphql/type/schema';
2222

23-
const KEYWORDS = ['first', 'last', 'limit'];
23+
export const KEYWORDS = ['first', 'last', 'limit'];
2424

2525
/**
2626
* Default TypeWeight Configuration:
@@ -93,7 +93,7 @@ function buildTypeWeightsFromSchema(
9393
) {
9494
if (isObjectType(currentType) || isInterfaceType(currentType)) {
9595
// Add the type to the result
96-
result[type] = {
96+
result[type.toLowerCase()] = {
9797
fields: {},
9898
weight: typeWeights.object || DEFAULT_OBJECT_WEIGHT,
9999
};
@@ -106,16 +106,17 @@ function buildTypeWeightsFromSchema(
106106
isScalarType(fieldType) ||
107107
(isNonNullType(fieldType) && isScalarType(fieldType.ofType))
108108
) {
109-
result[type].fields[field] = typeWeights.scalar || DEFAULT_SCALAR_WEIGHT;
109+
result[type.toLowerCase()].fields[field] =
110+
typeWeights.scalar || DEFAULT_SCALAR_WEIGHT;
110111
}
111112
});
112113
} else if (isEnumType(currentType)) {
113-
result[currentType.name] = {
114+
result[currentType.name.toLowerCase()] = {
114115
fields: {},
115116
weight: typeWeights.scalar || DEFAULT_SCALAR_WEIGHT,
116117
};
117118
} else if (isUnionType(currentType)) {
118-
result[currentType.name] = {
119+
result[currentType.name.toLowerCase()] = {
119120
fields: {},
120121
weight: typeWeights.object || DEFAULT_OBJECT_WEIGHT,
121122
};
@@ -127,7 +128,7 @@ function buildTypeWeightsFromSchema(
127128
const queryType: Maybe<GraphQLObjectType> = schema.getQueryType();
128129

129130
if (queryType) {
130-
result.Query = {
131+
result.query = {
131132
weight: typeWeights.query || DEFAULT_QUERY_WEIGHT,
132133
// fields gets populated with the query fields and associated weights.
133134
fields: {},
@@ -154,13 +155,14 @@ function buildTypeWeightsFromSchema(
154155
// Set the field weight to a function that accepts
155156
// TODO: Accept ArgumentNode[] and look for the arg we need.
156157
// TODO: Test this function
157-
result.Query.fields[field] = (args: ArgumentNode[]): number => {
158+
result.query.fields[field] = (args: ArgumentNode[]): number => {
158159
// Function should receive object with arg, value as k, v pairs
159160
// function iterate on this object looking for a keyword then returns
160161
const limitArg: ArgumentNode | undefined = args.find(
161162
(cur) => cur.name.value === arg.name
162163
);
163164

165+
// FIXME: Need to use the value of this variable
164166
// const isVariable = (node: any): node is VariableNode => {
165167
// if (node as VariableNode) return true;
166168
// return false;
@@ -178,25 +180,25 @@ function buildTypeWeightsFromSchema(
178180
if (isIntNode(node)) {
179181
const multiplier = Number(node.value || arg.defaultValue);
180182

181-
return result[listType.name].weight * multiplier;
183+
return result[listType.name.toLowerCase()].weight * multiplier;
182184
}
183185
}
184186

185187
// FIXME: The list is unbounded. Return the object weight
186-
return result[listType.name].weight;
188+
return result[listType.name.toLowerCase()].weight;
187189
};
188190
} else {
189191
// TODO: determine the type of the list and use the appropriate weight
190192
// TODO: This should multiply as well
191-
result.Query.fields[field] = typeWeights.scalar || DEFAULT_SCALAR_WEIGHT;
193+
result.query.fields[field] = typeWeights.scalar || DEFAULT_SCALAR_WEIGHT;
192194
}
193195
}
194196
});
195197

196198
// if the field is a scalar set weight accordingly
197199
// FIXME: Enums shouldn't be here???
198200
if (isScalarType(resolveType) || isEnumType(resolveType)) {
199-
result.Query.fields[field] = typeWeights.scalar || DEFAULT_SCALAR_WEIGHT;
201+
result.query.fields[field] = typeWeights.scalar || DEFAULT_SCALAR_WEIGHT;
200202
}
201203
});
202204
}

test/analysis/buildTypeWeights.test.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('Test buildTypeWeightsFromSchema function', () => {
3030
`);
3131

3232
expect(buildTypeWeightsFromSchema(schema)).toEqual({
33-
Query: {
33+
query: {
3434
weight: 1,
3535
fields: {
3636
name: 0,
@@ -57,18 +57,18 @@ describe('Test buildTypeWeightsFromSchema function', () => {
5757
`);
5858

5959
expect(buildTypeWeightsFromSchema(schema)).toEqual({
60-
Query: {
60+
query: {
6161
weight: 1,
6262
fields: {},
6363
},
64-
User: {
64+
user: {
6565
weight: 1,
6666
fields: {
6767
name: 0,
6868
email: 0,
6969
},
7070
},
71-
Movie: {
71+
movie: {
7272
weight: 1,
7373
fields: {
7474
name: 0,
@@ -95,17 +95,17 @@ describe('Test buildTypeWeightsFromSchema function', () => {
9595
`);
9696

9797
expect(buildTypeWeightsFromSchema(schema)).toEqual({
98-
Query: {
98+
query: {
9999
weight: 1,
100100
fields: {},
101101
},
102-
User: {
102+
user: {
103103
weight: 1,
104104
fields: {
105105
name: 0,
106106
},
107107
},
108-
Movie: {
108+
movie: {
109109
weight: 1,
110110
fields: {
111111
name: 0,
@@ -126,7 +126,7 @@ describe('Test buildTypeWeightsFromSchema function', () => {
126126
`);
127127

128128
expect(buildTypeWeightsFromSchema(schema)).toEqual({
129-
Test: {
129+
test: {
130130
weight: 1,
131131
fields: {
132132
num: 0,
@@ -149,11 +149,11 @@ describe('Test buildTypeWeightsFromSchema function', () => {
149149
name: String!
150150
}`);
151151
expect(buildTypeWeightsFromSchema(schema)).toEqual({
152-
Query: {
152+
query: {
153153
weight: 1,
154154
fields: {},
155155
},
156-
Character: {
156+
character: {
157157
weight: 1,
158158
fields: {
159159
id: 0,
@@ -178,18 +178,18 @@ describe('Test buildTypeWeightsFromSchema function', () => {
178178
JEDI
179179
}`);
180180
expect(buildTypeWeightsFromSchema(schema)).toEqual({
181-
Query: {
181+
query: {
182182
weight: 1,
183183
fields: {},
184184
},
185-
Character: {
185+
character: {
186186
weight: 1,
187187
fields: {
188188
id: 0,
189189
name: 0,
190190
},
191191
},
192-
Episode: {
192+
episode: {
193193
weight: 0,
194194
fields: {},
195195
},
@@ -218,22 +218,22 @@ describe('Test buildTypeWeightsFromSchema function', () => {
218218

219219
// TODO: Add tests for what the function does
220220
expect(buildTypeWeightsFromSchema(schema)).toEqual({
221-
Query: {
221+
query: {
222222
weight: 1,
223223
fields: {
224224
// FIXME: check the best solution during implementation and update the tests here.
225225
reviews: expect.any(Function), // FIXME: Better way to test this?
226226
// code from PR review -> reviews: (type) => args[multiplierName] * typeWeightObject[type].weight
227227
},
228228
},
229-
Review: {
229+
review: {
230230
weight: 1,
231231
fields: {
232232
stars: 0,
233233
commentary: 0,
234234
},
235235
},
236-
Episode: {
236+
episode: {
237237
weight: 0,
238238
fields: {},
239239
},
@@ -252,7 +252,7 @@ describe('Test buildTypeWeightsFromSchema function', () => {
252252
}
253253
`);
254254
expect(buildTypeWeightsFromSchema(schema)).toEqual({
255-
Human: {
255+
human: {
256256
weight: 1,
257257
fields: {
258258
// FIXME: check the best solution during implementation and update the tests here.
@@ -281,22 +281,22 @@ describe('Test buildTypeWeightsFromSchema function', () => {
281281
primaryFunction: String
282282
}`);
283283
expect(buildTypeWeightsFromSchema(schema)).toEqual({
284-
Character: {
284+
character: {
285285
weight: 1,
286286
fields: {
287287
id: 0,
288288
name: 0,
289289
},
290290
},
291-
Human: {
291+
human: {
292292
weight: 1,
293293
fields: {
294294
id: 0,
295295
name: 0,
296296
homePlanet: 0,
297297
},
298298
},
299-
Droid: {
299+
droid: {
300300
weight: 1,
301301
fields: {
302302
id: 0,
@@ -317,17 +317,17 @@ describe('Test buildTypeWeightsFromSchema function', () => {
317317
primaryFunction: String
318318
}`);
319319
expect(buildTypeWeightsFromSchema(schema)).toEqual({
320-
SearchResult: {
320+
searchresult: {
321321
weight: 1,
322322
fields: {},
323323
},
324-
Human: {
324+
human: {
325325
weight: 1,
326326
fields: {
327327
homePlanet: 0,
328328
},
329329
},
330-
Droid: {
330+
droid: {
331331
weight: 1,
332332
fields: {
333333
primaryFunction: 0,
@@ -366,17 +366,17 @@ describe('Test buildTypeWeightsFromSchema function', () => {
366366
// This expected output is using default type weight settings.
367367
// Each test will override values for feild weights configuration.
368368
expectedOutput = {
369-
Query: {
369+
query: {
370370
weight: 1,
371371
fields: {},
372372
},
373-
User: {
373+
user: {
374374
weight: 1,
375375
fields: {
376376
name: 0,
377377
},
378378
},
379-
Movie: {
379+
movie: {
380380
weight: 1,
381381
fields: {
382382
name: 0,
@@ -390,7 +390,7 @@ describe('Test buildTypeWeightsFromSchema function', () => {
390390
const typeWeightObject = buildTypeWeightsFromSchema(schema, {
391391
query: 2,
392392
});
393-
expectedOutput.Query.weight = 2;
393+
expectedOutput.query.weight = 2;
394394

395395
expect(typeWeightObject).toEqual(expectedOutput);
396396
});
@@ -400,8 +400,8 @@ describe('Test buildTypeWeightsFromSchema function', () => {
400400
object: 2,
401401
});
402402

403-
expectedOutput.User.weight = 2;
404-
expectedOutput.Movie.weight = 2;
403+
expectedOutput.user.weight = 2;
404+
expectedOutput.movie.weight = 2;
405405

406406
expect(typeWeightObject).toEqual(expectedOutput);
407407
});
@@ -411,8 +411,8 @@ describe('Test buildTypeWeightsFromSchema function', () => {
411411
scalar: 2,
412412
});
413413

414-
expectedOutput.User.fields.name = 2;
415-
expectedOutput.Movie.fields.name = 2;
414+
expectedOutput.user.fields.name = 2;
415+
expectedOutput.movie.fields.name = 2;
416416

417417
expect(typeWeightObject).toEqual(expectedOutput);
418418
});

0 commit comments

Comments
 (0)