graphql
Version:
A Query Language and Runtime which can target any service.
1 lines • 3.07 kB
Source Map (JSON)
{"version":3,"file":"UniqueDirectiveNamesRule.js","sourceRoot":"","sources":["../../../src/validation/rules/UniqueDirectiveNamesRule.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,qCAAoC;AAgC3D,MAAM,UAAU,wBAAwB,CACtC,OAA6B;IAE7B,MAAM,mBAAmB,GAAG,IAAI,GAAG,EAAoB,CAAC;IACxD,MAAM,MAAM,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC;IAEnC,OAAO;QACL,mBAAmB,CAAC,IAAI;YACtB,MAAM,aAAa,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC;YAEtC,IAAI,MAAM,EAAE,YAAY,CAAC,aAAa,CAAC,EAAE,CAAC;gBACxC,OAAO,CAAC,WAAW,CACjB,IAAI,YAAY,CACd,eAAe,aAAa,yDAAyD,EACrF,EAAE,KAAK,EAAE,IAAI,CAAC,IAAI,EAAE,CACrB,CACF,CAAC;gBACF,OAAO;YACT,CAAC;YAED,MAAM,SAAS,GAAG,mBAAmB,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;YACzD,IAAI,SAAS,EAAE,CAAC;gBACd,OAAO,CAAC,WAAW,CACjB,IAAI,YAAY,CACd,2CAA2C,aAAa,IAAI,EAC5D,EAAE,KAAK,EAAE,CAAC,SAAS,EAAE,IAAI,CAAC,IAAI,CAAC,EAAE,CAClC,CACF,CAAC;YACJ,CAAC;iBAAM,CAAC;gBACN,mBAAmB,CAAC,GAAG,CAAC,aAAa,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;YACpD,CAAC;YAED,OAAO,KAAK,CAAC;QACf,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["/** @category Validation Rules */\n\nimport { GraphQLError } from '../../error/GraphQLError.ts';\n\nimport type { NameNode } from '../../language/ast.ts';\nimport type { ASTVisitor } from '../../language/visitor.ts';\n\nimport type { SDLValidationContext } from '../ValidationContext.ts';\n\n/**\n * Unique directive names\n *\n * A GraphQL document is only valid if all defined directives have unique names.\n * @param context - The validation context used while checking the document.\n * @returns A visitor that reports validation errors for this rule.\n * @example\n * ```ts\n * import { buildSchema } from 'graphql';\n * import { UniqueDirectiveNamesRule } from 'graphql/validation';\n *\n * const invalidSDL = `\n * directive @tag on FIELD directive @tag on QUERY type Query { name: String }\n * `;\n *\n * UniqueDirectiveNamesRule.name; // => 'UniqueDirectiveNamesRule'\n * buildSchema(invalidSDL); // throws an error\n *\n * const validSDL = `\n * directive @tag on FIELD type Query { name: String }\n * `;\n *\n * buildSchema(validSDL); // does not throw\n * ```\n */\nexport function UniqueDirectiveNamesRule(\n context: SDLValidationContext,\n): ASTVisitor {\n const knownDirectiveNames = new Map<string, NameNode>();\n const schema = context.getSchema();\n\n return {\n DirectiveDefinition(node) {\n const directiveName = node.name.value;\n\n if (schema?.getDirective(directiveName)) {\n context.reportError(\n new GraphQLError(\n `Directive \"@${directiveName}\" already exists in the schema. It cannot be redefined.`,\n { nodes: node.name },\n ),\n );\n return;\n }\n\n const knownName = knownDirectiveNames.get(directiveName);\n if (knownName) {\n context.reportError(\n new GraphQLError(\n `There can be only one directive named \"@${directiveName}\".`,\n { nodes: [knownName, node.name] },\n ),\n );\n } else {\n knownDirectiveNames.set(directiveName, node.name);\n }\n\n return false;\n },\n };\n}\n"]}