Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a39f6c0

Browse files
Add fallback for missing child complexity in simple estimator
1 parent a940f94 commit a39f6c0

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

‎src/estimators/simple/__tests__/fixtures/schema.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
GraphQLEnumType,
1313
GraphQLUnionType,
1414
GraphQLInterfaceType,
15+
GraphQLScalarType,
1516
} from 'graphql';
1617

1718
const Item: GraphQLObjectType = new GraphQLObjectType({
@@ -70,6 +71,28 @@ const Union = new GraphQLUnionType({
7071
resolveType: () => Item,
7172
});
7273

74+
const ErrorThrower = new GraphQLObjectType({
75+
name: 'ErrorType',
76+
fields: {
77+
errorScalar: {
78+
type: new GraphQLObjectType({
79+
name: 'ErrorScalar',
80+
fields: { irrelevant: { type: GraphQLString } },
81+
}),
82+
args: {
83+
throws: {
84+
type: new GraphQLScalarType({
85+
name: 'Throws',
86+
parseValue() {
87+
throw new Error('Scalar parse error');
88+
},
89+
}),
90+
},
91+
},
92+
},
93+
},
94+
});
95+
7396
const Query = new GraphQLObjectType({
7497
name: 'Query',
7598
fields: () => ({
@@ -111,6 +134,9 @@ const Query = new GraphQLObjectType({
111134
},
112135
},
113136
},
137+
errorThrower: {
138+
type: ErrorThrower,
139+
},
114140
}),
115141
});
116142

‎src/estimators/simple/__tests__/simpleEstimator-test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,25 @@ describe('simple estimator', () => {
210210
visit(ast, visitWithTypeInfo(typeInfo, visitor));
211211
expect(visitor.complexity).to.equal(1);
212212
});
213+
214+
it('should fall back on default complexity when child complexity cannot be computed', () => {
215+
const ast = parse(`
216+
query {
217+
errorThrower {
218+
errorScalar(throws: "an error") {
219+
irrelevant
220+
}
221+
}
222+
}
223+
`);
224+
225+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
226+
const visitor = new ComplexityVisitor(context, {
227+
maximumComplexity: 100,
228+
estimators: [simpleEstimator({ defaultComplexity: 1 })],
229+
});
230+
231+
visit(ast, visitWithTypeInfo(typeInfo, visitor));
232+
expect(visitor.complexity).to.equal(1);
233+
});
213234
});

‎src/estimators/simple/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export default function (options?: {
1111
? options.defaultComplexity
1212
: 1;
1313
return (args: ComplexityEstimatorArgs): number | void => {
14-
return defaultComplexity + args.childComplexity;
14+
const { childComplexity } = args;
15+
return (
16+
defaultComplexity + (Number.isNaN(childComplexity) ? 0 : childComplexity)
17+
);
1518
};
1619
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /