Skip to content

Commit f3c1118

Browse files
committed
wrote tests for buildTypeWeights functionality and configurability
1 parent 06a119c commit f3c1118

File tree

2 files changed

+158
-32
lines changed

2 files changed

+158
-32
lines changed

src/analysis/buildTypeWeights.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,5 @@
11
import { GraphQLSchema } from 'graphql/type/schema';
22

3-
interface Fields {
4-
[index: string]: number;
5-
}
6-
7-
interface Type {
8-
weight: number;
9-
feilds: Fields;
10-
}
11-
12-
interface TypeWeightObject {
13-
[index: string]: Type;
14-
}
15-
16-
interface TypeWeightConfig {
17-
mutation?: number;
18-
query?: number;
19-
object?: number;
20-
scalar?: number;
21-
connection?: number;
22-
}
23-
243
/**
254
* The default type weights object is based off of Shopifys implewentation of query
265
* cost analysis. Our function should input a users configuration of type weights or fall

test/analysis/buildTypeWeights.test.ts

Lines changed: 158 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,167 @@ import { GraphQLSchema } from 'graphql/type/schema';
33
import buildTypeWeightsFromSchema from '../../src/analysis/buildTypeWeights';
44

55
describe('Test buildTypeWeightsFromSchema function', () => {
6-
beforeEach(() => {
7-
let schema: GraphQLSchema;
6+
let schema: GraphQLSchema;
7+
8+
describe('creates the type weight object from graphql schema object with...', () => {
9+
test('a single query type', () => {
10+
schema = buildSchema(`
11+
Query {
12+
name: String
13+
email: String
14+
}`);
15+
16+
const typeWeightObject = buildTypeWeightsFromSchema(schema);
17+
18+
expect(typeWeightObject).toEqual({
19+
Query: {
20+
weight: 1,
21+
fields: {
22+
name: 0,
23+
email: 0,
24+
},
25+
},
26+
});
27+
});
28+
29+
test('multiple types', () => {
30+
schema = buildSchema(`
31+
User {
32+
name: String
33+
email: String
34+
}
35+
36+
Movie {
37+
name: String
38+
director: String
39+
}`);
40+
41+
const typeWeightObject = buildTypeWeightsFromSchema(schema);
42+
43+
expect(typeWeightObject).toEqual({
44+
User: {
45+
weight: 1,
46+
fields: {
47+
name: 0,
48+
email: 0,
49+
},
50+
},
51+
Movie: {
52+
weight: 1,
53+
fields: {
54+
name: 0,
55+
director: 0,
56+
},
57+
},
58+
});
59+
});
60+
61+
test('nested object types', () => {
62+
schema = buildSchema(`
63+
Query {
64+
User {
65+
name: String
66+
email: String
67+
}
68+
69+
Movie {
70+
name: String
71+
director: User
72+
}
73+
}`);
74+
75+
const typeWeightObject = buildTypeWeightsFromSchema(schema);
76+
77+
expect(typeWeightObject).toEqual({
78+
Query: {
79+
weight: 1,
80+
fields: {
81+
User: 1,
82+
Movie: 1,
83+
},
84+
},
85+
User: {
86+
weight: 1,
87+
fields: {
88+
name: 0,
89+
email: 0,
90+
},
91+
},
92+
Movie: {
93+
weight: 1,
94+
fields: {
95+
name: 0,
96+
director: 1,
97+
},
98+
},
99+
});
100+
});
8101
});
9102

10-
describe('query types', () => {
11-
// cretes type weight object from schema with multipl types
12-
test('creates the type weight object from graphql schema object', () => {});
103+
describe('changes type weight object with user configuration of query of...', () => {
104+
let expectedOutput: TypeWeightObject;
105+
106+
beforeEach(() => {
107+
schema = buildSchema(`
108+
Query {
109+
User {
110+
name: String
111+
email: String
112+
}
113+
114+
Movie {
115+
name: String
116+
director: User
117+
}
118+
}`);
119+
120+
expectedOutput = {
121+
Query: {
122+
weight: 1,
123+
fields: {
124+
User: 1,
125+
Movie: 1,
126+
},
127+
},
128+
User: {
129+
weight: 1,
130+
fields: {
131+
name: 0,
132+
email: 0,
133+
},
134+
},
135+
Movie: {
136+
weight: 1,
137+
fields: {
138+
name: 0,
139+
director: 1,
140+
},
141+
},
142+
};
143+
});
144+
145+
test('query parameter', () => {
146+
const typeWeightObject = buildTypeWeightsFromSchema(schema, {
147+
query: 2,
148+
});
149+
expectedOutput.query.weight = 2;
150+
151+
expect(typeWeightObject).toEqual({ expectedOutput });
152+
});
153+
154+
test('all objects types', () => {
155+
const typeWeightObject = buildTypeWeightsFromSchema(schema, {
156+
object: 2,
157+
});
158+
expectedOutput.query.fields.user = 2;
159+
expectedOutput.query.fields.movie = 2;
160+
expectedOutput.user.weight = 2;
161+
expectedOutput.movie.weight = 2;
162+
expectedOutput.movie.fields.director = 2;
13163

14-
// creates tyep weight object from schema with nested types
15-
test('');
164+
expect(typeWeightObject).toEqual({ expectedOutput });
165+
});
16166
});
17167

18-
/**
19-
* Above tests are for query types only
20-
* todo testing functionality for mutations, pagination, lists, etc. is not yet implemented
21-
*/
168+
describe('throws an error on...', () => {});
22169
});

0 commit comments

Comments
 (0)