1
1
import {
2
- getVisitFn , GraphQLError , GraphQLNonNull , GraphQLList , GraphQLObjectType ,
2
+ getVisitFn ,
3
+ GraphQLError ,
4
+ GraphQLNonNull ,
5
+ GraphQLList ,
6
+ GraphQLObjectType ,
3
7
} from 'graphql' ;
4
8
import * as IntrospectionTypes from 'graphql/type/introspection' ;
5
9
import warning from 'warning' ;
@@ -33,9 +37,8 @@ export class CostCalculator {
33
37
return ;
34
38
}
35
39
36
- cost += costFactor * fragmentCalculator . calculateCost (
37
- fragmentCalculators ,
38
- ) ;
40
+ cost +=
41
+ costFactor * fragmentCalculator . calculateCost ( fragmentCalculators ) ;
39
42
} ) ;
40
43
41
44
this . cost = cost ;
@@ -44,14 +47,17 @@ export class CostCalculator {
44
47
}
45
48
46
49
export class ComplexityVisitor {
47
- constructor ( context , {
48
- scalarCost = 1 ,
49
- objectCost = 0 ,
50
- listFactor = 10 ,
51
-
52
- // Special list factor to make schema queries not have huge costs.
53
- introspectionListFactor = 2 ,
54
- } ) {
50
+ constructor (
51
+ context ,
52
+ {
53
+ scalarCost = 1 ,
54
+ objectCost = 0 ,
55
+ listFactor = 10 ,
56
+
57
+ // Special list factor to make schema queries not have huge costs.
58
+ introspectionListFactor = 2 ,
59
+ } ,
60
+ ) {
55
61
this . context = context ;
56
62
57
63
this . scalarCost = scalarCost ;
@@ -105,8 +111,9 @@ export class ComplexityVisitor {
105
111
if ( type instanceof GraphQLNonNull ) {
106
112
return this . getTypeCostFactor ( type . ofType ) ;
107
113
} else if ( type instanceof GraphQLList ) {
108
- const typeListFactor = this . isIntrospectionList ( type ) ?
109
- this . introspectionListFactor : this . listFactor ;
114
+ const typeListFactor = this . isIntrospectionList ( type )
115
+ ? this . introspectionListFactor
116
+ : this . listFactor ;
110
117
return typeListFactor * this . getTypeCostFactor ( type . ofType ) ;
111
118
}
112
119
@@ -141,8 +148,9 @@ export class ComplexityVisitor {
141
148
return this . getTypeCost ( type . ofType ) ;
142
149
}
143
150
144
- return type instanceof GraphQLObjectType ?
145
- this . objectCost : this . scalarCost ;
151
+ return type instanceof GraphQLObjectType
152
+ ? this . objectCost
153
+ : this . scalarCost ;
146
154
}
147
155
148
156
getDirectiveValue ( directiveName ) {
@@ -153,33 +161,34 @@ export class ComplexityVisitor {
153
161
return null ;
154
162
}
155
163
156
- const directive = astNode . directives . find ( ( { name } ) => (
157
- name . value === directiveName
158
- ) ) ;
164
+ const directive = astNode . directives . find (
165
+ ( { name } ) => name . value === directiveName ,
166
+ ) ;
159
167
if ( ! directive ) {
160
168
return null ;
161
169
}
162
170
163
- const valueArgument = directive . arguments . find ( argument => (
164
- argument . name . value === 'value'
165
- ) ) ;
171
+ const valueArgument = directive . arguments . find (
172
+ argument => argument . name . value === 'value' ,
173
+ ) ;
166
174
167
175
if ( ! valueArgument ) {
168
176
const fieldName = fieldDef . name ;
169
177
const parentTypeName = this . context . getParentType ( ) . name ;
170
178
171
179
throw new Error (
172
180
`No \`value\` argument defined in \`@${ directiveName } \` directive ` +
173
- `on \`${ fieldName } \` field on \`${ parentTypeName } \`.` ,
181
+ `on \`${ fieldName } \` field on \`${ parentTypeName } \`.` ,
174
182
) ;
175
183
}
176
184
177
185
return parseFloat ( valueArgument . value . value ) ;
178
186
}
179
187
180
188
getCalculator ( ) {
181
- return this . currentFragment === null ?
182
- this . rootCalculator : this . fragmentCalculators [ this . currentFragment ] ;
189
+ return this . currentFragment === null
190
+ ? this . rootCalculator
191
+ : this . fragmentCalculators [ this . currentFragment ] ;
183
192
}
184
193
185
194
enterFragmentSpread ( node ) {
@@ -216,9 +225,8 @@ export function createComplexityLimitRule(
216
225
'formatErrorMessage is ignored when createError is specified.' ,
217
226
) ;
218
227
219
- formatErrorMessage = ( // eslint-disable-line no-param-reassign
220
- formatErrorMessage || complexityLimitExceededErrorMessage
221
- ) ;
228
+ formatErrorMessage = // eslint-disable-line no-param-reassign
229
+ formatErrorMessage || complexityLimitExceededErrorMessage ;
222
230
223
231
return function ComplexityLimit ( context ) {
224
232
const visitor = new ComplexityVisitor ( context , options ) ;
@@ -246,9 +254,9 @@ export function createComplexityLimitRule(
246
254
247
255
if ( cost > maxCost ) {
248
256
context . reportError (
249
- createError ?
250
- createError ( cost , node ) :
251
- new GraphQLError ( formatErrorMessage ( cost ) , [ node ] ) ,
257
+ createError
258
+ ? createError ( cost , node )
259
+ : new GraphQLError ( formatErrorMessage ( cost ) , [ node ] ) ,
252
260
) ;
253
261
}
254
262
}
0 commit comments