1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.KnownDirectivesRule = KnownDirectivesRule;
var _inspect = _interopRequireDefault(require("../../jsutils/inspect.js"));
var _invariant = _interopRequireDefault(require("../../jsutils/invariant.js"));
var _GraphQLError = require("../../error/GraphQLError.js");
var _kinds = require("../../language/kinds.js");
var _directiveLocation = require("../../language/directiveLocation.js");
var _directives = require("../../type/directives.js");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Known directives
*
* A GraphQL document is only valid if all `@directives` are known by the
* schema and legally positioned.
*/
function KnownDirectivesRule(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("Unknown directive \"@".concat(name, "\"."), node));
return;
}
var candidateLocation = getDirectiveLocationForASTPath(ancestors);
if (candidateLocation && locations.indexOf(candidateLocation) === -1) {
context.reportError(new _GraphQLError.GraphQLError("Directive \"@".concat(name, "\" may not be used on ").concat(candidateLocation, "."), node));
}
}
};
}
function getDirectiveLocationForASTPath(ancestors) {
var appliedTo = ancestors[ancestors.length - 1];
!Array.isArray(appliedTo) || (0, _invariant.default)(0);
switch (appliedTo.kind) {
case _kinds.Kind.OPERATION_DEFINITION:
return getDirectiveLocationForOperation(appliedTo.operation);
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;
}
}
}
function getDirectiveLocationForOperation(operation) {
switch (operation) {
case 'query':
return _directiveLocation.DirectiveLocation.QUERY;
case 'mutation':
return _directiveLocation.DirectiveLocation.MUTATION;
case 'subscription':
return _directiveLocation.DirectiveLocation.SUBSCRIPTION;
} // istanbul ignore next (Not reachable. All possible types have been considered)
false || (0, _invariant.default)(0, 'Unexpected operation: ' + (0, _inspect.default)(operation));
}
|