Skip to content

Commit 4911724

Browse files
committed
split the test for lists into two, one for lists with arguments and the other lists of unknown size. Also added the function into the query field for lists wit arguments
1 parent 259da24 commit 4911724

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

src/@types/buildTypeWeights.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
interface Fields {
2-
readonly [index: string]: number;
2+
readonly [index: string]: number | ((arg: number, type: Type) => number);
33
}
44

55
interface Type {

test/analysis/buildTypeWeights.test.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,7 @@ xdescribe('Test buildTypeWeightsFromSchema function', () => {
195195
});
196196
});
197197

198-
// ? varibale weight
199-
test('fields returning lists of objects', () => {
198+
test('fields returning lists of objects of determinate size', () => {
200199
schema = buildSchema(`
201200
type Query {
202201
reviews(episode: Episode!, first: Int): [Review]
@@ -214,13 +213,16 @@ xdescribe('Test buildTypeWeightsFromSchema function', () => {
214213
expect(buildTypeWeightsFromSchema(schema)).toEqual({
215214
Query: {
216215
weight: 1,
217-
fields: {},
216+
fields: {
217+
// ? we could maybe use a closure with the type already included to make this function more easily called
218+
reviews: (arg: number, type: Type) => arg * type.weight,
219+
},
218220
},
219221
Review: {
220-
weight: 1, // ? weight is the argument passed as 'first'. it's variable...
222+
weight: 1,
221223
fields: {
222-
id: 0,
223-
name: 0,
224+
stars: 0,
225+
commentary: 0,
224226
},
225227
},
226228
Episode: {
@@ -230,44 +232,63 @@ xdescribe('Test buildTypeWeightsFromSchema function', () => {
230232
});
231233
});
232234

235+
// TODO: need to figure out how to handle this situation. Skip for now.
236+
// The field friends returns a list of an unknown number of objects.
237+
xtest('fields returning lists of objects of indetermitae size', () => {
238+
schema = buildSchema(`
239+
type Human {
240+
id: ID!
241+
name: String!
242+
homePlanet: String
243+
friends: [Human]
244+
}
245+
`);
246+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
247+
Human: {
248+
weight: 1,
249+
fields: {
250+
// ? we could maybe use a closure with the type already included to make this function more easily called
251+
friends: (arg: number, type: Type) => arg * type.weight,
252+
},
253+
},
254+
});
255+
});
256+
233257
test('interface types', () => {
234258
schema = buildSchema(`
235259
interface Character {
236260
id: ID!
237-
name: String!
238-
friends: [Character]
261+
name: String!
239262
}
240263
241264
type Human implements Character {
242265
id: ID!
243266
name: String!
244267
homePlanet: String
245-
friends: [Character]
246268
}
247269
248270
type Droid implements Character {
249271
id: ID!
250-
name: String!
251-
friends: [Character]
272+
name: String!
252273
primaryFunction: String
253274
}`);
254275
expect(buildTypeWeightsFromSchema(schema)).toEqual({
255-
character: {
276+
Character: {
256277
weight: 1,
257278
fields: {
258279
id: 0,
259280
name: 0,
260281
},
261282
},
262-
human: {
283+
Human: {
263284
weight: 1,
264285
fields: {
265286
id: 0,
266287
name: 0,
267288
homePlanet: 0,
268289
},
269290
},
270-
droid: {
291+
Droid: {
271292
weight: 1,
272293
fields: {
273294
id: 0,

0 commit comments

Comments
 (0)