171 lines
5.1 KiB
JavaScript
171 lines
5.1 KiB
JavaScript
import { GraphQLError } from '../../error/GraphQLError';
|
|
import { Kind } from '../../language/kinds';
|
|
import { DirectiveLocation } from '../../language/directiveLocation';
|
|
import { specifiedDirectives } from '../../type/directives';
|
|
export function unknownDirectiveMessage(directiveName) {
|
|
return "Unknown directive \"".concat(directiveName, "\".");
|
|
}
|
|
export 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.
|
|
*/
|
|
|
|
export function KnownDirectives(context) {
|
|
var locationsMap = Object.create(null);
|
|
var schema = context.getSchema();
|
|
var definedDirectives = schema ? schema.getDirectives() : specifiedDirectives;
|
|
var _iteratorNormalCompletion = true;
|
|
var _didIteratorError = false;
|
|
var _iteratorError = undefined;
|
|
|
|
try {
|
|
for (var _iterator = definedDirectives[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
var directive = _step.value;
|
|
locationsMap[directive.name] = directive.locations;
|
|
}
|
|
} catch (err) {
|
|
_didIteratorError = true;
|
|
_iteratorError = err;
|
|
} finally {
|
|
try {
|
|
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
_iterator.return();
|
|
}
|
|
} finally {
|
|
if (_didIteratorError) {
|
|
throw _iteratorError;
|
|
}
|
|
}
|
|
}
|
|
|
|
var astDefinitions = context.getDocument().definitions;
|
|
var _iteratorNormalCompletion2 = true;
|
|
var _didIteratorError2 = false;
|
|
var _iteratorError2 = undefined;
|
|
|
|
try {
|
|
for (var _iterator2 = astDefinitions[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
|
|
var def = _step2.value;
|
|
|
|
if (def.kind === Kind.DIRECTIVE_DEFINITION) {
|
|
locationsMap[def.name.value] = def.locations.map(function (name) {
|
|
return name.value;
|
|
});
|
|
}
|
|
}
|
|
} catch (err) {
|
|
_didIteratorError2 = true;
|
|
_iteratorError2 = err;
|
|
} finally {
|
|
try {
|
|
if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
|
|
_iterator2.return();
|
|
}
|
|
} finally {
|
|
if (_didIteratorError2) {
|
|
throw _iteratorError2;
|
|
}
|
|
}
|
|
}
|
|
|
|
return {
|
|
Directive: function Directive(node, key, parent, path, ancestors) {
|
|
var name = node.name.value;
|
|
var locations = locationsMap[name];
|
|
|
|
if (!locations) {
|
|
context.reportError(new GraphQLError(unknownDirectiveMessage(name), node));
|
|
return;
|
|
}
|
|
|
|
var candidateLocation = getDirectiveLocationForASTPath(ancestors);
|
|
|
|
if (candidateLocation && locations.indexOf(candidateLocation) === -1) {
|
|
context.reportError(new GraphQLError(misplacedDirectiveMessage(name, candidateLocation), node));
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
function getDirectiveLocationForASTPath(ancestors) {
|
|
var appliedTo = ancestors[ancestors.length - 1];
|
|
|
|
if (!Array.isArray(appliedTo)) {
|
|
switch (appliedTo.kind) {
|
|
case Kind.OPERATION_DEFINITION:
|
|
switch (appliedTo.operation) {
|
|
case 'query':
|
|
return DirectiveLocation.QUERY;
|
|
|
|
case 'mutation':
|
|
return DirectiveLocation.MUTATION;
|
|
|
|
case 'subscription':
|
|
return DirectiveLocation.SUBSCRIPTION;
|
|
}
|
|
|
|
break;
|
|
|
|
case Kind.FIELD:
|
|
return DirectiveLocation.FIELD;
|
|
|
|
case Kind.FRAGMENT_SPREAD:
|
|
return DirectiveLocation.FRAGMENT_SPREAD;
|
|
|
|
case Kind.INLINE_FRAGMENT:
|
|
return DirectiveLocation.INLINE_FRAGMENT;
|
|
|
|
case Kind.FRAGMENT_DEFINITION:
|
|
return DirectiveLocation.FRAGMENT_DEFINITION;
|
|
|
|
case Kind.VARIABLE_DEFINITION:
|
|
return DirectiveLocation.VARIABLE_DEFINITION;
|
|
|
|
case Kind.SCHEMA_DEFINITION:
|
|
case Kind.SCHEMA_EXTENSION:
|
|
return DirectiveLocation.SCHEMA;
|
|
|
|
case Kind.SCALAR_TYPE_DEFINITION:
|
|
case Kind.SCALAR_TYPE_EXTENSION:
|
|
return DirectiveLocation.SCALAR;
|
|
|
|
case Kind.OBJECT_TYPE_DEFINITION:
|
|
case Kind.OBJECT_TYPE_EXTENSION:
|
|
return DirectiveLocation.OBJECT;
|
|
|
|
case Kind.FIELD_DEFINITION:
|
|
return DirectiveLocation.FIELD_DEFINITION;
|
|
|
|
case Kind.INTERFACE_TYPE_DEFINITION:
|
|
case Kind.INTERFACE_TYPE_EXTENSION:
|
|
return DirectiveLocation.INTERFACE;
|
|
|
|
case Kind.UNION_TYPE_DEFINITION:
|
|
case Kind.UNION_TYPE_EXTENSION:
|
|
return DirectiveLocation.UNION;
|
|
|
|
case Kind.ENUM_TYPE_DEFINITION:
|
|
case Kind.ENUM_TYPE_EXTENSION:
|
|
return DirectiveLocation.ENUM;
|
|
|
|
case Kind.ENUM_VALUE_DEFINITION:
|
|
return DirectiveLocation.ENUM_VALUE;
|
|
|
|
case Kind.INPUT_OBJECT_TYPE_DEFINITION:
|
|
case Kind.INPUT_OBJECT_TYPE_EXTENSION:
|
|
return DirectiveLocation.INPUT_OBJECT;
|
|
|
|
case Kind.INPUT_VALUE_DEFINITION:
|
|
{
|
|
var parentNode = ancestors[ancestors.length - 3];
|
|
return parentNode.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION ? DirectiveLocation.INPUT_FIELD_DEFINITION : DirectiveLocation.ARGUMENT_DEFINITION;
|
|
}
|
|
}
|
|
}
|
|
}
|