Skip to content

Add support default values for query variables, fixes #50 #52

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
May 4, 2021
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
17 changes: 14 additions & 3 deletions src/QueryComplexity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import {
getArgumentValues,
getDirectiveValues,
getVariableValues,
} from 'graphql/execution/values';

import {
Expand Down Expand Up @@ -121,6 +122,7 @@ export default class QueryComplexity {
estimators: Array<ComplexityEstimator>;
includeDirectiveDef: GraphQLDirective;
skipDirectiveDef: GraphQLDirective;
variableValues: Record<string, any>;

constructor(context: ValidationContext, options: QueryComplexityOptions) {
if (
Expand All @@ -139,6 +141,7 @@ export default class QueryComplexity {
this.includeDirectiveDef = this.context.getSchema().getDirective('include');
this.skipDirectiveDef = this.context.getSchema().getDirective('skip');
this.estimators = options.estimators;
this.variableValues = {};

this.OperationDefinition = {
enter: this.onOperationDefinitionEnter,
Expand All @@ -154,6 +157,14 @@ export default class QueryComplexity {
return;
}

// Get variable values from variables that are passed from options, merged
// with default values defined in the operation
this.variableValues = getVariableValues(
this.context.getSchema(),
operation.variableDefinitions ?? [],
this.options.variables ?? {}
).coerced;

switch (operation.operation) {
case 'query':
this.complexity += this.nodeComplexity(
Expand Down Expand Up @@ -246,7 +257,7 @@ export default class QueryComplexity {
const values = getDirectiveValues(
this.includeDirectiveDef,
childNode,
this.options.variables || {}
this.variableValues || {}
);
includeNode = values.if;
break;
Expand All @@ -255,7 +266,7 @@ export default class QueryComplexity {
const values = getDirectiveValues(
this.skipDirectiveDef,
childNode,
this.options.variables || {}
this.variableValues || {}
);
skipNode = values.if;
break;
Expand All @@ -282,7 +293,7 @@ export default class QueryComplexity {
args = getArgumentValues(
field,
childNode,
this.options.variables || {}
this.variableValues || {}
);
} catch (e) {
return this.context.reportError(e);
Expand Down
34 changes: 34 additions & 0 deletions src/__tests__/QueryComplexity-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ describe('QueryComplexity analysis', () => {
expect(complexity).to.equal(1);
});

it('should respect @include(if: false) via default variable value', () => {
const ast = parse(`
query Foo ($shouldSkip: Boolean = false) {
variableScalar(count: 10) @include(if: $shouldSkip)
}
`);

const complexity = getComplexity({
estimators: [simpleEstimator({ defaultComplexity: 1 })],
schema,
query: ast,
});
expect(complexity).to.equal(0);
});

it('should respect @include(if: false)', () => {
const ast = parse(`
query {
Expand Down Expand Up @@ -133,6 +148,25 @@ describe('QueryComplexity analysis', () => {
expect(complexity).to.equal(50);
});

it('should calculate complexity with variables and default value', () => {
const ast = parse(`
query Q($count: Int = 5) {
variableScalar(count: $count)
}
`);

const complexity = getComplexity({
estimators: [
fieldExtensionsEstimator(),
simpleEstimator({ defaultComplexity: 1 }),
],
schema,
query: ast,
variables: {},
});
expect(complexity).to.equal(50);
});

it('should not allow negative cost', () => {
const ast = parse(`
query {
Expand Down