Skip to content

Skip complexity calculation by directiveEstimator when astNode is undefined #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/__tests__/QueryComplexity-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import schema from './fixtures/schema';
import ComplexityVisitor, {getComplexity} from '../QueryComplexity';
import {
simpleEstimator,
directiveEstimator,
fieldConfigEstimator,
} from '../index';

Expand Down Expand Up @@ -321,4 +322,45 @@ describe('QueryComplexity analysis', () => {
'At least one complexity estimator has to return a complexity score.'
);
});

it('should return NaN when no astNode available on field when use directiveEstimator', () => {
const ast = parse(`
query {
_service {
sdl
}
}
`);

const complexity = getComplexity({
estimators: [
directiveEstimator(),
],
schema,
query: ast
});
expect(Number.isNaN(complexity)).to.equal(true);
});

it('should skip complexity calculation by directiveEstimator when no astNode available on field', () => {
const ast = parse(`
query {
_service {
sdl
}
}
`);

const complexity = getComplexity({
estimators: [
directiveEstimator(),
simpleEstimator({
defaultComplexity: 1
})
],
schema,
query: ast
});
expect(complexity).to.equal(2);
});
});
11 changes: 10 additions & 1 deletion src/__tests__/fixtures/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ const Union = new GraphQLUnionType({
resolveType: () => Item
});

const SDLInterface = new GraphQLInterfaceType({
name: 'SDLInterface',
fields: {
sdl: { type: GraphQLString }
},
resolveType: () => '"SDL"'
});

const Query = new GraphQLObjectType({
name: 'Query',
fields: () => ({
Expand Down Expand Up @@ -126,7 +134,8 @@ const Query = new GraphQLObjectType({
type: new GraphQLNonNull(GraphQLInt)
}
}
}
},
_service: {type: SDLInterface},
}),
});

Expand Down
5 changes: 5 additions & 0 deletions src/estimators/directive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export default function (options?: {}): ComplexityEstimator {
});

return (args: ComplexityEstimatorArgs) => {
// Ignore if astNode is undefined
if (!args.field.astNode) {
return;
}

const values = getDirectiveValues(directive, args.field.astNode);

// Ignore if no directive set
Expand Down