1 | import inspect from "../../jsutils/inspect.mjs";
|
2 | import invariant from "../../jsutils/invariant.mjs";
|
3 | import { GraphQLError } from "../../error/GraphQLError.mjs";
|
4 | import { Kind } from "../../language/kinds.mjs";
|
5 | import { DirectiveLocation } from "../../language/directiveLocation.mjs";
|
6 | import { specifiedDirectives } from "../../type/directives.mjs";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | export function KnownDirectivesRule(context) {
|
15 | var locationsMap = Object.create(null);
|
16 | var schema = context.getSchema();
|
17 | var definedDirectives = schema ? schema.getDirectives() : specifiedDirectives;
|
18 |
|
19 | for (var _i2 = 0; _i2 < definedDirectives.length; _i2++) {
|
20 | var directive = definedDirectives[_i2];
|
21 | locationsMap[directive.name] = directive.locations;
|
22 | }
|
23 |
|
24 | var astDefinitions = context.getDocument().definitions;
|
25 |
|
26 | for (var _i4 = 0; _i4 < astDefinitions.length; _i4++) {
|
27 | var def = astDefinitions[_i4];
|
28 |
|
29 | if (def.kind === Kind.DIRECTIVE_DEFINITION) {
|
30 | locationsMap[def.name.value] = def.locations.map(function (name) {
|
31 | return name.value;
|
32 | });
|
33 | }
|
34 | }
|
35 |
|
36 | return {
|
37 | Directive: function Directive(node, _key, _parent, _path, ancestors) {
|
38 | var name = node.name.value;
|
39 | var locations = locationsMap[name];
|
40 |
|
41 | if (!locations) {
|
42 | context.reportError(new GraphQLError("Unknown directive \"@".concat(name, "\"."), node));
|
43 | return;
|
44 | }
|
45 |
|
46 | var candidateLocation = getDirectiveLocationForASTPath(ancestors);
|
47 |
|
48 | if (candidateLocation && locations.indexOf(candidateLocation) === -1) {
|
49 | context.reportError(new GraphQLError("Directive \"@".concat(name, "\" may not be used on ").concat(candidateLocation, "."), node));
|
50 | }
|
51 | }
|
52 | };
|
53 | }
|
54 |
|
55 | function getDirectiveLocationForASTPath(ancestors) {
|
56 | var appliedTo = ancestors[ancestors.length - 1];
|
57 | !Array.isArray(appliedTo) || invariant(0);
|
58 |
|
59 | switch (appliedTo.kind) {
|
60 | case Kind.OPERATION_DEFINITION:
|
61 | return getDirectiveLocationForOperation(appliedTo.operation);
|
62 |
|
63 | case Kind.FIELD:
|
64 | return DirectiveLocation.FIELD;
|
65 |
|
66 | case Kind.FRAGMENT_SPREAD:
|
67 | return DirectiveLocation.FRAGMENT_SPREAD;
|
68 |
|
69 | case Kind.INLINE_FRAGMENT:
|
70 | return DirectiveLocation.INLINE_FRAGMENT;
|
71 |
|
72 | case Kind.FRAGMENT_DEFINITION:
|
73 | return DirectiveLocation.FRAGMENT_DEFINITION;
|
74 |
|
75 | case Kind.VARIABLE_DEFINITION:
|
76 | return DirectiveLocation.VARIABLE_DEFINITION;
|
77 |
|
78 | case Kind.SCHEMA_DEFINITION:
|
79 | case Kind.SCHEMA_EXTENSION:
|
80 | return DirectiveLocation.SCHEMA;
|
81 |
|
82 | case Kind.SCALAR_TYPE_DEFINITION:
|
83 | case Kind.SCALAR_TYPE_EXTENSION:
|
84 | return DirectiveLocation.SCALAR;
|
85 |
|
86 | case Kind.OBJECT_TYPE_DEFINITION:
|
87 | case Kind.OBJECT_TYPE_EXTENSION:
|
88 | return DirectiveLocation.OBJECT;
|
89 |
|
90 | case Kind.FIELD_DEFINITION:
|
91 | return DirectiveLocation.FIELD_DEFINITION;
|
92 |
|
93 | case Kind.INTERFACE_TYPE_DEFINITION:
|
94 | case Kind.INTERFACE_TYPE_EXTENSION:
|
95 | return DirectiveLocation.INTERFACE;
|
96 |
|
97 | case Kind.UNION_TYPE_DEFINITION:
|
98 | case Kind.UNION_TYPE_EXTENSION:
|
99 | return DirectiveLocation.UNION;
|
100 |
|
101 | case Kind.ENUM_TYPE_DEFINITION:
|
102 | case Kind.ENUM_TYPE_EXTENSION:
|
103 | return DirectiveLocation.ENUM;
|
104 |
|
105 | case Kind.ENUM_VALUE_DEFINITION:
|
106 | return DirectiveLocation.ENUM_VALUE;
|
107 |
|
108 | case Kind.INPUT_OBJECT_TYPE_DEFINITION:
|
109 | case Kind.INPUT_OBJECT_TYPE_EXTENSION:
|
110 | return DirectiveLocation.INPUT_OBJECT;
|
111 |
|
112 | case Kind.INPUT_VALUE_DEFINITION:
|
113 | {
|
114 | var parentNode = ancestors[ancestors.length - 3];
|
115 | return parentNode.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION ? DirectiveLocation.INPUT_FIELD_DEFINITION : DirectiveLocation.ARGUMENT_DEFINITION;
|
116 | }
|
117 | }
|
118 | }
|
119 |
|
120 | function getDirectiveLocationForOperation(operation) {
|
121 | switch (operation) {
|
122 | case 'query':
|
123 | return DirectiveLocation.QUERY;
|
124 |
|
125 | case 'mutation':
|
126 | return DirectiveLocation.MUTATION;
|
127 |
|
128 | case 'subscription':
|
129 | return DirectiveLocation.SUBSCRIPTION;
|
130 | }
|
131 |
|
132 |
|
133 | false || invariant(0, 'Unexpected operation: ' + inspect(operation));
|
134 | }
|