Skip to content

Remove definition iteration from 'ExecutableDefinitions' rule #1466

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

Closed
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
27 changes: 17 additions & 10 deletions src/validation/rules/ExecutableDefinitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
import type { ASTValidationContext } from '../ValidationContext';
import { GraphQLError } from '../../error/GraphQLError';
import { Kind } from '../../language/kinds';
import { isExecutableDefinitionNode } from '../../language/predicates';
import {
isDefinitionNode,
isExecutableDefinitionNode,
} from '../../language/predicates';
import type { ASTNode } from '../../language/ast';
import type { ASTVisitor } from '../../language/visitor';

export function nonExecutableDefinitionMessage(defName: string): string {
Expand All @@ -27,23 +31,26 @@ export function ExecutableDefinitions(
context: ASTValidationContext,
): ASTVisitor {
return {
Document(node) {
for (const definition of node.definitions) {
if (!isExecutableDefinitionNode(definition)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mjmahone I was searching for non-SDL examples of iterating over all definition pattern and found this 😄

enter(node) {
if (isDefinitionNode(node)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can combine these into a single if statement:

if (isDefinitionNode(node) && !isExecutableDefinitionNode(node)) {

Copy link
Member Author

@IvanGoncharov IvanGoncharov Aug 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually no, since you need to return false if isDefinitionNode is true to skip other definitions.
But I starting to think this solution is too complicated and we need to find a generic one for all similar cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah you're right, I missed the return false

if (!isExecutableDefinitionNode(node)) {
context.reportError(
new GraphQLError(
nonExecutableDefinitionMessage(
definition.kind === Kind.SCHEMA_DEFINITION ||
definition.kind === Kind.SCHEMA_EXTENSION
? 'schema'
: definition.name.value,
isSchemaNode(node) ? 'schema' : node.name.value,
),
[definition],
node,
),
);
}
return false;
}
return false;
},
};
}

function isSchemaNode(node: ASTNode): boolean %checks {
return (
node.kind === Kind.SCHEMA_DEFINITION || node.kind === Kind.SCHEMA_EXTENSION
);
}