swc/crates/swc_bundler/tests/.cache/deno/d57983204e1f809a3eff59710d532b336db31a98.ts
2021-11-09 20:42:49 +09:00

72 lines
2.4 KiB
TypeScript

// Loaded from https://deno.land/x/graphql_deno@v15.0.0/lib/validation/rules/UniqueDirectivesPerLocationRule.js
import { GraphQLError } from '../../error/GraphQLError.js';
import { Kind } from '../../language/kinds.js';
import { isTypeDefinitionNode, isTypeExtensionNode } from '../../language/predicates.js';
import { specifiedDirectives } from '../../type/directives.js';
/**
* Unique directive names per location
*
* A GraphQL document is only valid if all non-repeatable directives at
* a given location are uniquely named.
*/
export function UniqueDirectivesPerLocationRule(context) {
const uniqueDirectiveMap = Object.create(null);
const schema = context.getSchema();
const definedDirectives = schema ? schema.getDirectives() : specifiedDirectives;
for (const directive of definedDirectives) {
uniqueDirectiveMap[directive.name] = !directive.isRepeatable;
}
const astDefinitions = context.getDocument().definitions;
for (const def of astDefinitions) {
if (def.kind === Kind.DIRECTIVE_DEFINITION) {
uniqueDirectiveMap[def.name.value] = !def.repeatable;
}
}
const schemaDirectives = Object.create(null);
const typeDirectivesMap = Object.create(null);
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) {
if (node.directives == null) {
return;
}
let seenDirectives;
if (node.kind === Kind.SCHEMA_DEFINITION || node.kind === Kind.SCHEMA_EXTENSION) {
seenDirectives = schemaDirectives;
} else if (isTypeDefinitionNode(node) || isTypeExtensionNode(node)) {
const typeName = node.name.value;
seenDirectives = typeDirectivesMap[typeName];
if (seenDirectives === undefined) {
typeDirectivesMap[typeName] = seenDirectives = Object.create(null);
}
} else {
seenDirectives = Object.create(null);
}
for (const directive of node.directives) {
const directiveName = directive.name.value;
if (uniqueDirectiveMap[directiveName]) {
if (seenDirectives[directiveName]) {
context.reportError(new GraphQLError(`The directive "@${directiveName}" can only be used once at this location.`, [seenDirectives[directiveName], directive]));
} else {
seenDirectives[directiveName] = directive;
}
}
}
}
};
}