Skip to content

Commit c435c93

Browse files
committed
merged the changes from buildTypeWeights tests
2 parents 2162d40 + eb23356 commit c435c93

File tree

2 files changed

+193
-18
lines changed

2 files changed

+193
-18
lines changed

src/analysis/buildTypeWeights.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ import { GraphQLSchema } from 'graphql/type/schema';
1515
function buildTypeWeightsFromSchema(
1616
schema: GraphQLSchema,
1717
typeWeightsConfig: TypeWeightConfig = {
18-
mutation: 10,
19-
object: 1,
20-
scalar: 0,
21-
connection: 2,
18+
mutation: 10, // mutation
19+
object: 1, // itnterfaces, unions, objects, query
20+
scalar: 0, // enums, scalars
21+
connection: 2, // pagination stuff
22+
// ? subscription
2223
}
2324
): TypeWeightObject {
2425
throw Error(`getTypeWeightsFromSchema is not implemented.`);

test/analysis/buildTypeWeights.test.ts

Lines changed: 188 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,25 @@ xdescribe('Test buildTypeWeightsFromSchema function', () => {
4141

4242
test('multiple types', () => {
4343
schema = buildSchema(`
44+
type Query {
45+
user: User,
46+
movie: Movie,
47+
}
4448
type User {
4549
name: String
4650
email: String
4751
}
48-
4952
type Movie {
5053
name: String
5154
director: String
5255
}
5356
`);
5457

5558
expect(buildTypeWeightsFromSchema(schema)).toEqual({
59+
Query: {
60+
weight: 1,
61+
fields: {},
62+
},
5663
User: {
5764
weight: 1,
5865
fields: {
@@ -76,12 +83,10 @@ xdescribe('Test buildTypeWeightsFromSchema function', () => {
7683
user: User
7784
movie: Movie
7885
}
79-
8086
type User {
8187
name: String
82-
email: String
88+
film: Movie
8389
}
84-
8590
type Movie {
8691
name: String
8792
director: User
@@ -134,14 +139,182 @@ xdescribe('Test buildTypeWeightsFromSchema function', () => {
134139
});
135140
});
136141

137-
// List type
138-
// Enem types
139-
// Interface
140-
// Unions
141-
// Input types
142+
test('types with arguments', () => {
143+
schema = buildSchema(`
144+
type Query {
145+
character(id: ID!): Character
146+
}
147+
type Character {
148+
id: ID!
149+
name: String!
150+
}`);
151+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
152+
Query: {
153+
weight: 1,
154+
fields: {},
155+
},
156+
Character: {
157+
weight: 1,
158+
fields: {
159+
id: 0,
160+
name: 0,
161+
},
162+
},
163+
});
164+
});
165+
166+
test('enum types', () => {
167+
schema = buildSchema(`
168+
type Query {
169+
hero(episode: Episode): Character
170+
}
171+
type Character {
172+
id: ID!
173+
name: String!
174+
}
175+
enum Episode {
176+
NEWHOPE
177+
EMPIRE
178+
JEDI
179+
}`);
180+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
181+
Query: {
182+
weight: 1,
183+
fields: {},
184+
},
185+
Character: {
186+
weight: 1,
187+
fields: {
188+
id: 0,
189+
name: 0,
190+
},
191+
},
192+
Episode: {
193+
weight: 0,
194+
fields: {},
195+
},
196+
});
197+
});
198+
199+
// ? varibale weight
200+
test('fields returning lists of objects', () => {
201+
schema = buildSchema(`
202+
type Query {
203+
reviews(episode: Episode!, first: Int): [Review]
204+
}
205+
type Review {
206+
episode: Episode
207+
stars: Int!
208+
commentary: String
209+
}
210+
enum Episode {
211+
NEWHOPE
212+
EMPIRE
213+
JEDI
214+
}`);
215+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
216+
Query: {
217+
weight: 1,
218+
fields: {},
219+
},
220+
Review: {
221+
weight: 1, // ? weight is the argument passed as 'first'. it's variable...
222+
fields: {
223+
id: 0,
224+
name: 0,
225+
},
226+
},
227+
Episode: {
228+
weight: 0,
229+
fields: {},
230+
},
231+
});
232+
});
233+
234+
test('interface types', () => {
235+
schema = buildSchema(`
236+
interface Character {
237+
id: ID!
238+
name: String!
239+
friends: [Character]
240+
}
241+
242+
type Human implements Character {
243+
id: ID!
244+
name: String!
245+
homePlanet: String
246+
friends: [Character]
247+
}
248+
249+
type Droid implements Character {
250+
id: ID!
251+
name: String!
252+
friends: [Character]
253+
primaryFunction: String
254+
}`);
255+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
256+
character: {
257+
weight: 1,
258+
fields: {
259+
id: 0,
260+
name: 0,
261+
},
262+
},
263+
human: {
264+
weight: 1,
265+
fields: {
266+
id: 0,
267+
name: 0,
268+
homePlanet: 0,
269+
},
270+
},
271+
droid: {
272+
weight: 1,
273+
fields: {
274+
id: 0,
275+
name: 0,
276+
primaryFunction: 0,
277+
},
278+
},
279+
Episode: {
280+
weight: 0,
281+
fields: {},
282+
},
283+
});
284+
});
285+
286+
test('union tyes', () => {
287+
schema = buildSchema(`
288+
union SearchResult = Human | Droid
289+
type Human{
290+
homePlanet: String
291+
}
292+
type Droid {
293+
primaryFunction: String
294+
}`);
295+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
296+
SearchResult: {
297+
weight: 1,
298+
fields: {},
299+
},
300+
human: {
301+
weight: 1,
302+
fields: {
303+
homePlanet: 0,
304+
},
305+
},
306+
droid: {
307+
weight: 1,
308+
fields: {
309+
primaryFunction: 0,
310+
},
311+
},
312+
});
313+
});
142314

143315
// TODO: Tests should be written to acount for the additional scenarios possible in a schema
144316
// Mutation type
317+
// Input types (a part of mutations?)
145318
// Subscription type
146319
});
147320

@@ -151,13 +324,13 @@ xdescribe('Test buildTypeWeightsFromSchema function', () => {
151324
beforeEach(() => {
152325
schema = buildSchema(`
153326
type Query {
154-
user: User
155-
movie: Movie
327+
user(id: ID!): User
328+
movie(id: ID!): Movie
156329
}
157330
158331
type User {
159332
name: String
160-
email: String
333+
film: Movie
161334
}
162335
163336
type Movie {
@@ -177,7 +350,6 @@ xdescribe('Test buildTypeWeightsFromSchema function', () => {
177350
weight: 1,
178351
fields: {
179352
name: 0,
180-
email: 0,
181353
},
182354
},
183355
Movie: {
@@ -216,7 +388,6 @@ xdescribe('Test buildTypeWeightsFromSchema function', () => {
216388
});
217389

218390
expectedOutput.user.fields.name = 2;
219-
expectedOutput.user.fields.email = 2;
220391
expectedOutput.movie.fields.name = 2;
221392

222393
expect(typeWeightObject).toEqual({ expectedOutput });
@@ -263,5 +434,8 @@ xdescribe('Test buildTypeWeightsFromSchema function', () => {
263434
'negative'
264435
);
265436
});
437+
438+
// TODO: throw validation error if schema is invalid
439+
test('schema is invalid', () => {});
266440
});
267441
});

0 commit comments

Comments
 (0)