1- import { ArgumentNode , IntValueNode , ValueNode } from 'graphql/language' ;
2- import { KEYWORDS } from '../../src/analysis/buildTypeWeights ' ;
1+ import { ArgumentNode } from 'graphql/language' ;
2+ import { parse } from 'graphql ' ;
33import getQueryTypeComplexity from '../../src/analysis/typeComplexityAnalysis' ;
44
55/**
@@ -170,45 +170,47 @@ const typeWeights: TypeWeightObject = {
170170
171171xdescribe ( 'Test getQueryTypeComplexity function' , ( ) => {
172172 let query = '' ;
173+ let variables : any | undefined ;
173174 describe ( 'Calculates the correct type complexity for queries' , ( ) => {
174175 test ( 'with one feild' , ( ) => {
175176 query = `Query { scalars { num } }` ;
176- expect ( getQueryTypeComplexity ( query , typeWeights ) ) . toBe ( 2 ) ; // Query 1 + Scalars 1
177+ expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 2 ) ; // Query 1 + Scalars 1
177178 } ) ;
178179
179180 test ( 'with two or more fields' , ( ) => {
180181 query = `Query { scalars { num } test { name } }` ;
181- expect ( getQueryTypeComplexity ( query , typeWeights ) ) . toBe ( 3 ) ; // Query 1 + scalars 1 + test 1
182+ expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 3 ) ; // Query 1 + scalars 1 + test 1
182183 } ) ;
183184
184185 test ( 'with one level of nested fields' , ( ) => {
185186 query = `Query { scalars { num, test { name } } }` ;
186- expect ( getQueryTypeComplexity ( query , typeWeights ) ) . toBe ( 3 ) ; // Query 1 + scalars 1 + test 1
187+ expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 3 ) ; // Query 1 + scalars 1 + test 1
187188 } ) ;
188189
189190 test ( 'with multiple levels of nesting' , ( ) => {
190191 query = `Query { scalars { num, test { name, scalars { id } } } }` ;
191- expect ( getQueryTypeComplexity ( query , typeWeights ) ) . toBe ( 4 ) ; // Query 1 + scalars 1 + test 1 + scalars 1
192+ expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 4 ) ; // Query 1 + scalars 1 + test 1 + scalars 1
192193 } ) ;
193194
194195 test ( 'with aliases' , ( ) => {
195196 query = `Query { foo: scalar { num } bar: scalar { id }}` ;
196- expect ( getQueryTypeComplexity ( query , typeWeights ) ) . toBe ( 3 ) ; // Query 1 + scalar 1 + scalar 1
197+ expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 3 ) ; // Query 1 + scalar 1 + scalar 1
197198 } ) ;
198199
199200 test ( 'with all scalar fields' , ( ) => {
200201 query = `Query { scalars { id, num, float, bool, string } }` ;
201- expect ( getQueryTypeComplexity ( query , typeWeights ) ) . toBe ( 2 ) ; // Query 1 + scalar 1
202+ expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 2 ) ; // Query 1 + scalar 1
202203 } ) ;
203204
204205 test ( 'with arguments and variables' , ( ) => {
205206 query = `Query { hero(episode: EMPIRE) { id, name } }` ;
206- expect ( getQueryTypeComplexity ( query , typeWeights ) ) . toBe ( 2 ) ; // Query 1 + hero/character 1
207+ expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 2 ) ; // Query 1 + hero/character 1
207208 query = `Query { human(id: 1) { id, name, appearsIn } }` ;
208- expect ( getQueryTypeComplexity ( query , typeWeights ) ) . toBe ( 3 ) ; // Query 1 + human/character 1 + appearsIn/episode
209+ expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 3 ) ; // Query 1 + human/character 1 + appearsIn/episode
209210 // argument passed in as a variable
210- query = `Query { hero(episode: $ep) { id, name } }` ;
211- expect ( getQueryTypeComplexity ( query , typeWeights ) ) . toBe ( 2 ) ; // Query 1 + hero/character 1
211+ variables = { ep : 'EMPIRE' } ;
212+ query = `Query varibaleQuery ($ep: Episode){ hero(episode: $ep) { id, name } }` ;
213+ expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 2 ) ; // Query 1 + hero/character 1
212214 } ) ;
213215
214216 test ( 'with fragments' , ( ) => {
@@ -227,7 +229,7 @@ xdescribe('Test getQueryTypeComplexity function', () => {
227229 appearsIn
228230 }
229231 }` ;
230- expect ( getQueryTypeComplexity ( query , typeWeights ) ) . toBe ( 5 ) ; // Query 1 + 2*(character 1 + appearsIn/episode 1)
232+ expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 5 ) ; // Query 1 + 2*(character 1 + appearsIn/episode 1)
231233 } ) ;
232234
233235 test ( 'with inline fragments' , ( ) => {
@@ -243,7 +245,7 @@ xdescribe('Test getQueryTypeComplexity function', () => {
243245 }
244246 }
245247 }` ;
246- expect ( getQueryTypeComplexity ( query , typeWeights ) ) . toBe ( 2 ) ; // Query 1 + hero/character 1)
248+ expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 2 ) ; // Query 1 + hero/character 1)
247249 } ) ;
248250
249251 /**
@@ -257,12 +259,15 @@ xdescribe('Test getQueryTypeComplexity function', () => {
257259 name
258260 }
259261 }` ;
260- expect ( getQueryTypeComplexity ( query , typeWeights ) ) . toBe ( false ) ; // ?
262+ expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( false ) ; // ?
261263 } ) ;
262264
263- test ( 'with lists detrmined by arguments' , ( ) => {
265+ test ( 'with lists detrmined by arguments and variables ' , ( ) => {
264266 query = `Query {reviews(episode: EMPIRE, first: 3) { stars, commentary } }` ;
265- expect ( getQueryTypeComplexity ( query , typeWeights ) ) . toBe ( 4 ) ; // 1 Query + 3 reviews
267+ expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 4 ) ; // 1 Query + 3 reviews
268+ variables = { first : 3 } ;
269+ query = `Query queryVaribales($first: Int) {reviews(episode: EMPIRE, first: $first) { stars, commentary } }` ;
270+ expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 4 ) ; // 1 Query + 3 reviews
266271 } ) ;
267272
268273 test ( 'with nested lists' , ( ) => {
@@ -278,7 +283,7 @@ xdescribe('Test getQueryTypeComplexity function', () => {
278283 }
279284 }
280285 }` ;
281- expect ( getQueryTypeComplexity ( query , typeWeights ) ) . toBe ( 17 ) ; // 1 Query + 1 human/character + (5 friends/character X 3 friends/characters)
286+ expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 17 ) ; // 1 Query + 1 human/character + (5 friends/character X 3 friends/characters)
282287 } ) ;
283288
284289 test ( 'accounting for __typename feild' , ( ) => {
@@ -296,19 +301,25 @@ xdescribe('Test getQueryTypeComplexity function', () => {
296301 }
297302 }
298303 }` ;
299- expect ( getQueryTypeComplexity ( query , typeWeights ) ) . toBe ( 5 ) ; // 1 Query + 4 search results
304+ expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 5 ) ; // 1 Query + 4 search results
300305 } ) ;
301306
302307 // todo: directives @skip , @include and custom directives
303308
304309 // todo: expand on error handling
305310 test ( 'Throws an error if for a bad query' , ( ) => {
306311 query = `Query { hello { hi } }` ; // type doesn't exist
307- expect ( ( ) => getQueryTypeComplexity ( query , typeWeights ) ) . toThrow ( 'Error' ) ;
312+ expect ( ( ) => getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toThrow (
313+ 'Error'
314+ ) ;
308315 query = `Query { hero(episode: EMPIRE){ starship } }` ; // field doesn't exist
309- expect ( ( ) => getQueryTypeComplexity ( query , typeWeights ) ) . toThrow ( 'Error' ) ;
316+ expect ( ( ) => getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toThrow (
317+ 'Error'
318+ ) ;
310319 query = `Query { hero(episode: EMPIRE) { id, name }` ; // missing a closing bracket
311- expect ( ( ) => getQueryTypeComplexity ( query , typeWeights ) ) . toThrow ( 'Error' ) ;
320+ expect ( ( ) => getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toThrow (
321+ 'Error'
322+ ) ;
312323 } ) ;
313324 } ) ;
314325
0 commit comments