Initial commit
This commit is contained in:
commit
f47e08fdcb
2409 files changed
+363607
No files matched your search
+57
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @flow strict
|
||||
*/
|
||||
|
||||
import type { ASTValidationContext } from '../ValidationContext';
|
||||
import { GraphQLError } from '../../error/GraphQLError';
|
||||
import type { DirectiveNode } from '../../language/ast';
|
||||
import type { ASTVisitor } from '../../language/visitor';
|
||||
|
||||
export function duplicateDirectiveMessage(directiveName: string): string {
|
||||
return (
|
||||
`The directive "${directiveName}" can only be used once at ` +
|
||||
'this location.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unique directive names per location
|
||||
*
|
||||
* A GraphQL document is only valid if all directives at a given location
|
||||
* are uniquely named.
|
||||
*/
|
||||
export function UniqueDirectivesPerLocation(
|
||||
context: ASTValidationContext,
|
||||
): ASTVisitor {
|
||||
return {
|
||||
// Many different AST nodes may contain directives. Rather than listing
|
||||
// them all, just listen for entering any node, and check to see if it
|
||||
// defines any directives.
|
||||
enter(node) {
|
||||
// Flow can't refine that node.directives will only contain directives,
|
||||
// so we cast so the rest of the code is well typed.
|
||||
const directives: ?$ReadOnlyArray<DirectiveNode> = (node: any).directives;
|
||||
if (directives) {
|
||||
const knownDirectives = Object.create(null);
|
||||
for (const directive of directives) {
|
||||
const directiveName = directive.name.value;
|
||||
if (knownDirectives[directiveName]) {
|
||||
context.reportError(
|
||||
new GraphQLError(duplicateDirectiveMessage(directiveName), [
|
||||
knownDirectives[directiveName],
|
||||
directive,
|
||||
]),
|
||||
);
|
||||
} else {
|
||||
knownDirectives[directiveName] = directive;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user