Skip to content

Report variable coercion errors #78

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 2 commits into from
Aug 27, 2022
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
10 changes: 8 additions & 2 deletions src/QueryComplexity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,18 @@ export default class QueryComplexity {

// Get variable values from variables that are passed from options, merged
// with default values defined in the operation
this.variableValues = getVariableValues(
const { coerced, errors } = getVariableValues(
this.context.getSchema(),
// We have to create a new array here because input argument is not readonly in graphql ~14.6.0
operation.variableDefinitions ? [...operation.variableDefinitions] : [],
this.options.variables ?? {}
).coerced;
);
if (errors && errors.length) {
// We have input validation errors, report errors and abort
errors.forEach((error) => this.context.reportError(error));
return;
}
this.variableValues = coerced;

switch (operation.operation) {
case 'query':
Expand Down
49 changes: 40 additions & 9 deletions src/__tests__/QueryComplexity-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,21 +254,28 @@ describe('QueryComplexity analysis', () => {
expect(visitor.complexity).to.equal(0);
});

it('should ignore unused variables', () => {
it('should report errors for unused variables', () => {
const ast = parse(`
query ($unusedVar: ID!) {
variableScalar(count: 100)
}
`);

const context = new CompatibleValidationContext(schema, ast, typeInfo);
const visitor = new ComplexityVisitor(context, {
maximumComplexity: 100,
estimators: [simpleEstimator({ defaultComplexity: 10 })],
});

visit(ast, visitWithTypeInfo(typeInfo, visitor));
expect(visitor.complexity).to.equal(10);
const errors = validate(schema, ast, [
createComplexityRule({
maximumComplexity: 1000,
estimators: [
simpleEstimator({
defaultComplexity: 1,
}),
],
variables: {
unusedVar: 'someID',
},
}),
]);
expect(errors).to.have.length(1);
expect(errors[0].message).to.contain('$unusedVar');
});

it('should ignore unknown field', () => {
Expand Down Expand Up @@ -860,4 +867,28 @@ describe('QueryComplexity analysis', () => {
// query.scalar(5) + query.requiredArgs(5) * requiredArgs.scalar(5)
expect(complexity).to.equal(30);
});

it('reports variable coercion errors', () => {
const ast = parse(`
query ($input: RGB!){
enumInputArg(enum: $input)
}
`);

const errors = validate(schema, ast, [
createComplexityRule({
maximumComplexity: 1000,
estimators: [
simpleEstimator({
defaultComplexity: 1,
}),
],
variables: {
input: 'INVALIDVALUE',
},
}),
]);
expect(errors).to.have.length(1);
expect(errors[0].message).to.contain('INVALIDVALUE');
});
});
8 changes: 8 additions & 0 deletions src/__tests__/fixtures/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ const Query = new GraphQLObjectType({
},
},
},
enumInputArg: {
type: GraphQLString,
args: {
enum: {
type: EnumType,
},
},
},
_service: { type: SDLInterface },
}),
interfaces: () => [NameInterface, UnionInterface],
Expand Down