Skip to content

Validate directive arguments inside SDL #1463

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
Aug 28, 2018
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
98 changes: 97 additions & 1 deletion src/validation/__tests__/KnownArgumentNames-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,24 @@
*/

import { describe, it } from 'mocha';
import { expectPassesRule, expectFailsRule } from './harness';
import { buildSchema } from '../../utilities';
import {
expectPassesRule,
expectFailsRule,
expectSDLErrorsFromRule,
} from './harness';
import {
KnownArgumentNames,
KnownArgumentNamesOnDirectives,
unknownArgMessage,
unknownDirectiveArgMessage,
} from '../rules/KnownArgumentNames';

const expectSDLErrors = expectSDLErrorsFromRule.bind(
undefined,
KnownArgumentNamesOnDirectives,
);

function unknownArg(argName, fieldName, typeName, suggestedArgs, line, column) {
return {
message: unknownArgMessage(argName, fieldName, typeName, suggestedArgs),
Expand Down Expand Up @@ -215,4 +226,89 @@ describe('Validate: Known argument names', () => {
],
);
});

describe('within SDL', () => {
it('known arg on directive defined inside SDL', () => {
expectSDLErrors(`
type Query {
foo: String @test(arg: "")
}

directive @test(arg: String) on FIELD_DEFINITION
`).to.deep.equal([]);
});

it('unknown arg on directive defined inside SDL', () => {
expectSDLErrors(`
type Query {
foo: String @test(unknown: "")
}

directive @test(arg: String) on FIELD_DEFINITION
`).to.deep.equal([unknownDirectiveArg('unknown', 'test', [], 3, 29)]);
});

it('misspelled arg name is reported on directive defined inside SDL', () => {
expectSDLErrors(`
type Query {
foo: String @test(agr: "")
}

directive @test(arg: String) on FIELD_DEFINITION
`).to.deep.equal([unknownDirectiveArg('agr', 'test', ['arg'], 3, 29)]);
});

it('unknown arg on standard directive', () => {
expectSDLErrors(`
type Query {
foo: String @deprecated(unknown: "")
}
`).to.deep.equal([
unknownDirectiveArg('unknown', 'deprecated', [], 3, 35),
]);
});

it('unknown arg on overrided standard directive', () => {
expectSDLErrors(`
type Query {
foo: String @deprecated(reason: "")
}
directive @deprecated(arg: String) on FIELD
`).to.deep.equal([
unknownDirectiveArg('reason', 'deprecated', [], 3, 35),
]);
});

it('unknown arg on directive defined in schema extension', () => {
const schema = buildSchema(`
type Query {
foo: String
}
`);
expectSDLErrors(
`
directive @test(arg: String) on OBJECT

extend type Query @test(unknown: "")
`,
schema,
).to.deep.equal([unknownDirectiveArg('unknown', 'test', [], 4, 36)]);
});

it('unknown arg on directive used in schema extension', () => {
const schema = buildSchema(`
directive @test(arg: String) on OBJECT

type Query {
foo: String
}
`);
expectSDLErrors(
`
extend type Query @test(unknown: "")
`,
schema,
).to.deep.equal([unknownDirectiveArg('unknown', 'test', [], 2, 35)]);
});
});
});
88 changes: 87 additions & 1 deletion src/validation/__tests__/ProvidedRequiredArguments-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,24 @@
*/

import { describe, it } from 'mocha';
import { expectPassesRule, expectFailsRule } from './harness';
import { buildSchema } from '../../utilities';
import {
expectPassesRule,
expectFailsRule,
expectSDLErrorsFromRule,
} from './harness';
import {
ProvidedRequiredArguments,
ProvidedRequiredArgumentsOnDirectives,
missingFieldArgMessage,
missingDirectiveArgMessage,
} from '../rules/ProvidedRequiredArguments';

const expectSDLErrors = expectSDLErrorsFromRule.bind(
undefined,
ProvidedRequiredArgumentsOnDirectives,
);

function missingFieldArg(fieldName, argName, typeName, line, column) {
return {
message: missingFieldArgMessage(fieldName, argName, typeName),
Expand Down Expand Up @@ -278,4 +289,79 @@ describe('Validate: Provided required arguments', () => {
);
});
});

describe('within SDL', () => {
it('Missing optional args on directive defined inside SDL', () => {
expectSDLErrors(`
type Query {
foo: String @test
}

directive @test(arg1: String, arg2: String! = "") on FIELD_DEFINITION
`).to.deep.equal([]);
});

it('Missing arg on directive defined inside SDL', () => {
expectSDLErrors(`
type Query {
foo: String @test
}

directive @test(arg: String!) on FIELD_DEFINITION
`).to.deep.equal([missingDirectiveArg('test', 'arg', 'String!', 3, 23)]);
});

it('Missing arg on standard directive', () => {
expectSDLErrors(`
type Query {
foo: String @include
}
`).to.deep.equal([
missingDirectiveArg('include', 'if', 'Boolean!', 3, 23),
]);
});

it('Missing arg on overrided standard directive', () => {
expectSDLErrors(`
type Query {
foo: String @deprecated
}
directive @deprecated(reason: String!) on FIELD
`).to.deep.equal([
missingDirectiveArg('deprecated', 'reason', 'String!', 3, 23),
]);
});

it('Missing arg on directive defined in schema extension', () => {
const schema = buildSchema(`
type Query {
foo: String
}
`);
expectSDLErrors(
`
directive @test(arg: String!) on OBJECT

extend type Query @test
`,
schema,
).to.deep.equal([missingDirectiveArg('test', 'arg', 'String!', 4, 30)]);
});

it('Missing arg on directive used in schema extension', () => {
const schema = buildSchema(`
directive @test(arg: String!) on OBJECT

type Query {
foo: String
}
`);
expectSDLErrors(
`
extend type Query @test
`,
schema,
).to.deep.equal([missingDirectiveArg('test', 'arg', 'String!', 2, 29)]);
});
});
});
101 changes: 66 additions & 35 deletions src/validation/rules/KnownArgumentNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
* @flow strict
*/

import type { ValidationContext } from '../ValidationContext';
import type {
ValidationContext,
SDLValidationContext,
} from '../ValidationContext';
import { GraphQLError } from '../../error/GraphQLError';
import type { ASTVisitor } from '../../language/visitor';
import suggestionList from '../../jsutils/suggestionList';
import quotedOrList from '../../jsutils/quotedOrList';
import { Kind } from '../../language/kinds';
import { specifiedDirectives } from '../../type/directives';

export function unknownArgMessage(
argName: string,
Expand Down Expand Up @@ -49,48 +53,75 @@ export function unknownDirectiveArgMessage(
*/
export function KnownArgumentNames(context: ValidationContext): ASTVisitor {
return {
Argument(node, key, parent, path, ancestors) {
...KnownArgumentNamesOnDirectives(context),
Argument(argNode) {
const argDef = context.getArgument();
if (!argDef) {
const argumentOf = ancestors[ancestors.length - 1];
if (argumentOf.kind === Kind.FIELD) {
const fieldDef = context.getFieldDef();
const parentType = context.getParentType();
if (fieldDef && parentType) {
context.reportError(
new GraphQLError(
unknownArgMessage(
node.name.value,
fieldDef.name,
parentType.name,
suggestionList(
node.name.value,
fieldDef.args.map(arg => arg.name),
),
),
[node],
),
);
}
} else if (argumentOf.kind === Kind.DIRECTIVE) {
const directive = context.getDirective();
if (directive) {
const fieldDef = context.getFieldDef();
const parentType = context.getParentType();

if (!argDef && fieldDef && parentType) {
const argName = argNode.name.value;
const knownArgsNames = fieldDef.args.map(arg => arg.name);
context.reportError(
new GraphQLError(
unknownArgMessage(
argName,
fieldDef.name,
parentType.name,
suggestionList(argName, knownArgsNames),
),
argNode,
),
);
}
},
};
}

// @internal
export function KnownArgumentNamesOnDirectives(
context: ValidationContext | SDLValidationContext,
): ASTVisitor {
const directiveArgs = Object.create(null);

const schema = context.getSchema();
const definedDirectives = schema
? schema.getDirectives()
: specifiedDirectives;
for (const directive of definedDirectives) {
directiveArgs[directive.name] = directive.args.map(arg => arg.name);
}

const astDefinitions = context.getDocument().definitions;
for (const def of astDefinitions) {
if (def.kind === Kind.DIRECTIVE_DEFINITION) {
directiveArgs[def.name.value] = def.arguments
? def.arguments.map(arg => arg.name.value)
: [];
}
}

return {
Directive(directiveNode) {
const directiveName = directiveNode.name.value;
const knownArgs = directiveArgs[directiveName];

if (directiveNode.arguments && knownArgs) {
for (const argNode of directiveNode.arguments) {
const argName = argNode.name.value;
if (knownArgs.indexOf(argName) === -1) {
const suggestions = suggestionList(argName, knownArgs);
context.reportError(
new GraphQLError(
unknownDirectiveArgMessage(
node.name.value,
directive.name,
suggestionList(
node.name.value,
directive.args.map(arg => arg.name),
),
),
[node],
unknownDirectiveArgMessage(argName, directiveName, suggestions),
argNode,
),
);
}
}
}

return false;
},
};
}
1 change: 1 addition & 0 deletions src/validation/rules/KnownDirectives.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function KnownDirectives(
context: ValidationContext | SDLValidationContext,
): ASTVisitor {
const locationsMap = Object.create(null);

const schema = context.getSchema();
const definedDirectives = schema
? schema.getDirectives()
Expand Down
Loading