123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.unknownDirectiveMessage = unknownDirectiveMessage;
- exports.misplacedDirectiveMessage = misplacedDirectiveMessage;
- exports.KnownDirectives = KnownDirectives;
- var _GraphQLError = require("../../error/GraphQLError");
- var _kinds = require("../../language/kinds");
- var _directiveLocation = require("../../language/directiveLocation");
- var _directives = require("../../type/directives");
- function unknownDirectiveMessage(directiveName) {
- return "Unknown directive \"".concat(directiveName, "\".");
- }
- function misplacedDirectiveMessage(directiveName, location) {
- return "Directive \"".concat(directiveName, "\" may not be used on ").concat(location, ".");
- }
- /**
- * Known directives
- *
- * A GraphQL document is only valid if all `@directives` are known by the
- * schema and legally positioned.
- */
- function KnownDirectives(context) {
- var locationsMap = Object.create(null);
- var schema = context.getSchema();
- var definedDirectives = schema ? schema.getDirectives() : _directives.specifiedDirectives;
- for (var _i2 = 0; _i2 < definedDirectives.length; _i2++) {
- var directive = definedDirectives[_i2];
- locationsMap[directive.name] = directive.locations;
- }
- var astDefinitions = context.getDocument().definitions;
- for (var _i4 = 0; _i4 < astDefinitions.length; _i4++) {
- var def = astDefinitions[_i4];
- if (def.kind === _kinds.Kind.DIRECTIVE_DEFINITION) {
- locationsMap[def.name.value] = def.locations.map(function (name) {
- return name.value;
- });
- }
- }
- return {
- Directive: function Directive(node, key, parent, path, ancestors) {
- var name = node.name.value;
- var locations = locationsMap[name];
- if (!locations) {
- context.reportError(new _GraphQLError.GraphQLError(unknownDirectiveMessage(name), node));
- return;
- }
- var candidateLocation = getDirectiveLocationForASTPath(ancestors);
- if (candidateLocation && locations.indexOf(candidateLocation) === -1) {
- context.reportError(new _GraphQLError.GraphQLError(misplacedDirectiveMessage(name, candidateLocation), node));
- }
- }
- };
- }
- function getDirectiveLocationForASTPath(ancestors) {
- var appliedTo = ancestors[ancestors.length - 1];
- if (!Array.isArray(appliedTo)) {
- switch (appliedTo.kind) {
- case _kinds.Kind.OPERATION_DEFINITION:
- switch (appliedTo.operation) {
- case 'query':
- return _directiveLocation.DirectiveLocation.QUERY;
- case 'mutation':
- return _directiveLocation.DirectiveLocation.MUTATION;
- case 'subscription':
- return _directiveLocation.DirectiveLocation.SUBSCRIPTION;
- }
- break;
- case _kinds.Kind.FIELD:
- return _directiveLocation.DirectiveLocation.FIELD;
- case _kinds.Kind.FRAGMENT_SPREAD:
- return _directiveLocation.DirectiveLocation.FRAGMENT_SPREAD;
- case _kinds.Kind.INLINE_FRAGMENT:
- return _directiveLocation.DirectiveLocation.INLINE_FRAGMENT;
- case _kinds.Kind.FRAGMENT_DEFINITION:
- return _directiveLocation.DirectiveLocation.FRAGMENT_DEFINITION;
- case _kinds.Kind.VARIABLE_DEFINITION:
- return _directiveLocation.DirectiveLocation.VARIABLE_DEFINITION;
- case _kinds.Kind.SCHEMA_DEFINITION:
- case _kinds.Kind.SCHEMA_EXTENSION:
- return _directiveLocation.DirectiveLocation.SCHEMA;
- case _kinds.Kind.SCALAR_TYPE_DEFINITION:
- case _kinds.Kind.SCALAR_TYPE_EXTENSION:
- return _directiveLocation.DirectiveLocation.SCALAR;
- case _kinds.Kind.OBJECT_TYPE_DEFINITION:
- case _kinds.Kind.OBJECT_TYPE_EXTENSION:
- return _directiveLocation.DirectiveLocation.OBJECT;
- case _kinds.Kind.FIELD_DEFINITION:
- return _directiveLocation.DirectiveLocation.FIELD_DEFINITION;
- case _kinds.Kind.INTERFACE_TYPE_DEFINITION:
- case _kinds.Kind.INTERFACE_TYPE_EXTENSION:
- return _directiveLocation.DirectiveLocation.INTERFACE;
- case _kinds.Kind.UNION_TYPE_DEFINITION:
- case _kinds.Kind.UNION_TYPE_EXTENSION:
- return _directiveLocation.DirectiveLocation.UNION;
- case _kinds.Kind.ENUM_TYPE_DEFINITION:
- case _kinds.Kind.ENUM_TYPE_EXTENSION:
- return _directiveLocation.DirectiveLocation.ENUM;
- case _kinds.Kind.ENUM_VALUE_DEFINITION:
- return _directiveLocation.DirectiveLocation.ENUM_VALUE;
- case _kinds.Kind.INPUT_OBJECT_TYPE_DEFINITION:
- case _kinds.Kind.INPUT_OBJECT_TYPE_EXTENSION:
- return _directiveLocation.DirectiveLocation.INPUT_OBJECT;
- case _kinds.Kind.INPUT_VALUE_DEFINITION:
- {
- var parentNode = ancestors[ancestors.length - 3];
- return parentNode.kind === _kinds.Kind.INPUT_OBJECT_TYPE_DEFINITION ? _directiveLocation.DirectiveLocation.INPUT_FIELD_DEFINITION : _directiveLocation.DirectiveLocation.ARGUMENT_DEFINITION;
- }
- }
- }
- }
|