@@ -24,14 +24,6 @@ import { GraphQLSchema } from 'graphql/type/schema';
2424
2525export const KEYWORDS = [ 'first' , 'last' , 'limit' ] ;
2626
27- /**
28- * Default TypeWeight Configuration:
29- * mutation: 10
30- * object: 1
31- * scalar: 0
32- * connection: 2
33- */
34-
3527// These variables exist to provide a default value for typescript when accessing a weight
3628// since all props are optioal in TypeWeightConfig
3729const DEFAULT_MUTATION_WEIGHT = 10 ;
@@ -42,13 +34,27 @@ const DEFAULT_QUERY_WEIGHT = 1;
4234
4335// FIXME: What about Union, Enum and Interface defaults
4436
37+ /**
38+ * Default TypeWeight Configuration:
39+ * mutation: 10
40+ * object: 1
41+ * scalar: 0
42+ * connection: 2
43+ */
4544export const defaultTypeWeightsConfig : TypeWeightConfig = {
4645 mutation : DEFAULT_MUTATION_WEIGHT ,
4746 object : DEFAULT_OBJECT_WEIGHT ,
4847 scalar : DEFAULT_SCALAR_WEIGHT ,
4948 connection : DEFAULT_CONNECTION_WEIGHT ,
5049} ;
5150
51+ /**
52+ * Parses the Query type in the provided schema object and outputs a new TypeWeightObject
53+ * @param schema
54+ * @param typeWeightObject
55+ * @param typeWeights
56+ * @returns
57+ */
5258function parseQuery (
5359 schema : GraphQLSchema ,
5460 typeWeightObject : TypeWeightObject ,
@@ -81,60 +87,62 @@ function parseQuery(
8187 // Get the type that comprises the list
8288 const listType = resolveType . ofType ;
8389
84- // Composite Types are Objects, Interfaces and Unions.
85- if ( isCompositeType ( listType ) ) {
86- // Set the field weight to a function that accepts
87-
88- // FIXME: This function can only handle integer arguments for one of the keyword params.
89- // In order to handle variable arguments, we may need to accept a second parameter so that the complexity aglorithm
90- // can pass in the variables as well.
91- result . query . fields [ field ] = ( args : ArgumentNode [ ] ) : number => {
92- // TODO: Test this function
93- const limitArg : ArgumentNode | undefined = args . find (
94- ( cur ) => cur . name . value === arg . name
95- ) ;
96-
97- if ( limitArg ) {
98- const node : ValueNode = limitArg . value ;
99-
100- if ( Kind . INT === node . kind ) {
101- const multiplier = Number ( node . value || arg . defaultValue ) ;
102-
103- return result [ listType . name . toLowerCase ( ) ] . weight * multiplier ;
104- }
105-
106- if ( Kind . VARIABLE === node . kind ) {
107- // TODO: Get variable value and return
108- // const multiplier: number =
109- // return result[listType.name.toLowerCase()].weight * multiplier;
110- throw new Error (
111- 'ERROR: buildTypeWeights Variable arge values not supported;'
112- ) ;
113- }
90+ // FIXME: This function can only handle integer arguments for one of the keyword params.
91+ // In order to handle variable arguments, we may need to accept a second parameter so that the complexity aglorithm
92+ // can pass in the variables as well.
93+ // FIXME: If the weight of the resolveType is 0 the weight can be set to 0 rather than a function.
94+ result . query . fields [ field ] = ( args : ArgumentNode [ ] ) : number => {
95+ // TODO: Test this function
96+ const limitArg : ArgumentNode | undefined = args . find (
97+ ( cur ) => cur . name . value === arg . name
98+ ) ;
99+
100+ if ( limitArg ) {
101+ const node : ValueNode = limitArg . value ;
102+
103+ if ( Kind . INT === node . kind ) {
104+ const multiplier = Number ( node . value || arg . defaultValue ) ;
105+ const weight = isCompositeType ( listType )
106+ ? result [ listType . name . toLowerCase ( ) ] . weight
107+ : typeWeights . scalar || DEFAULT_SCALAR_WEIGHT ; // Note this includes enums
108+
109+ return weight * multiplier ;
110+ }
111+
112+ if ( Kind . VARIABLE === node . kind ) {
113+ // TODO: Get variable value and return
114+ // const multiplier: number =
115+ // return result[listType.name.toLowerCase()].weight * multiplier;
116+ throw new Error (
117+ 'ERROR: buildTypeWeights Variable arge values not supported;'
118+ ) ;
114119 }
120+ }
115121
116- // FIXME: The list is unbounded. Return the object weight for
117- throw new Error (
118- `ERROR: buildTypeWeights: Unbouned list complexity not supported. Query results should be limited with ${ KEYWORDS } `
119- ) ;
120- } ;
121- } else {
122- // TODO: determine the type of the list and use the appropriate weight
123- // TODO: This should multiply as well
124- result . query . fields [ field ] = typeWeights . scalar || DEFAULT_SCALAR_WEIGHT ;
125- }
122+ // FIXME: The list is unbounded. Return the object weight for
123+ throw new Error (
124+ `ERROR: buildTypeWeights: Unbouned list complexity not supported. Query results should be limited with ${ KEYWORDS } `
125+ ) ;
126+ } ;
126127 }
127128 } ) ;
128129
129- // if the field is a scalar set weight accordingly
130- // TODO: Allow config for enum weights
130+ // if the field is a scalar or an enum set weight accordingly
131131 if ( isScalarType ( resolveType ) || isEnumType ( resolveType ) ) {
132132 result . query . fields [ field ] = typeWeights . scalar || DEFAULT_SCALAR_WEIGHT ;
133133 }
134134 } ) ;
135135 return result ;
136136}
137137
138+ /**
139+ * Parses all types in the provided schema object excempt for Query, Mutation
140+ * and built in types that begin with '__' and outputs a new TypeWeightObject
141+ * @param schema
142+ * @param typeWeightObject
143+ * @param typeWeights
144+ * @returns
145+ */
138146function parseTypes (
139147 schema : GraphQLSchema ,
140148 typeWeightObject : TypeWeightObject ,
0 commit comments