@@ -113,6 +113,25 @@ function parseObjectFields(
113113 resolveTo : listType . toString ( ) . toLocaleLowerCase ( ) ,
114114 } ;
115115 } else {
116+ // if the @listCost directive is given for the field,
117+ // apply the cost argument's value to the field's weight
118+ const directives = fields [ field ] . astNode ?. directives ;
119+
120+ if ( directives && directives . length > 0 ) {
121+ directives . forEach ( ( dir ) => {
122+ if ( dir . name . value === 'listCost' ) {
123+ if ( dir . arguments && dir . arguments [ 0 ] . value . kind === Kind . INT ) {
124+ result . fields [ field ] = {
125+ resolveTo : listType . toString ( ) . toLocaleLowerCase ( ) ,
126+ weight : Number ( dir . arguments [ 0 ] . value . value ) ,
127+ } ;
128+ }
129+ throw new SyntaxError ( `@listCost directive improperly configured` ) ;
130+ }
131+ } ) ;
132+ }
133+
134+ // if no directive is supplied to list field
116135 fields [ field ] . args . forEach ( ( arg : GraphQLArgument ) => {
117136 // If field has an argument matching one of the limiting keywords and resolves to a list
118137 // then the weight of the field should be dependent on both the weight of the resolved type and the limiting argument.
@@ -145,14 +164,16 @@ function parseObjectFields(
145164 return multiplier * ( selectionsCost + weight ) ;
146165 // ? what else can get through here
147166 }
167+
148168 // if there is no argument provided with the query, check the schema for a default
149169 if ( arg . defaultValue ) {
150170 return Number ( arg . defaultValue ) * ( selectionsCost + weight ) ;
151171 }
152172
153- // FIXME: The list is unbounded. Return the object weight for
173+ // if an unbounded list has no @listCost directive attached
154174 throw new Error (
155- `ERROR: buildTypeWeights: Unbouned list complexity not supported. Query results should be limited with ${ KEYWORDS } `
175+ `ERROR: buildTypeWeights: Use directive @listCost(cost: Int!) on unbounded lists,
176+ or limit query results with ${ KEYWORDS } `
156177 ) ;
157178 } ,
158179 } ;
0 commit comments