1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.isDirective = isDirective;
|
7 | exports.assertDirective = assertDirective;
|
8 | exports.isSpecifiedDirective = isSpecifiedDirective;
|
9 | exports.specifiedDirectives = exports.GraphQLDeprecatedDirective = exports.DEFAULT_DEPRECATION_REASON = exports.GraphQLSkipDirective = exports.GraphQLIncludeDirective = exports.GraphQLDirective = void 0;
|
10 |
|
11 | var _objectEntries = _interopRequireDefault(require("../polyfills/objectEntries"));
|
12 |
|
13 | var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
|
14 |
|
15 | var _toObjMap = _interopRequireDefault(require("../jsutils/toObjMap"));
|
16 |
|
17 | var _devAssert = _interopRequireDefault(require("../jsutils/devAssert"));
|
18 |
|
19 | var _instanceOf = _interopRequireDefault(require("../jsutils/instanceOf"));
|
20 |
|
21 | var _defineToJSON = _interopRequireDefault(require("../jsutils/defineToJSON"));
|
22 |
|
23 | var _isObjectLike = _interopRequireDefault(require("../jsutils/isObjectLike"));
|
24 |
|
25 | var _defineToStringTag = _interopRequireDefault(require("../jsutils/defineToStringTag"));
|
26 |
|
27 | var _directiveLocation = require("../language/directiveLocation");
|
28 |
|
29 | var _scalars = require("./scalars");
|
30 |
|
31 | var _definition = require("./definition");
|
32 |
|
33 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
34 |
|
35 |
|
36 | function isDirective(directive) {
|
37 | return (0, _instanceOf.default)(directive, GraphQLDirective);
|
38 | }
|
39 |
|
40 | function assertDirective(directive) {
|
41 | if (!isDirective(directive)) {
|
42 | throw new Error("Expected ".concat((0, _inspect.default)(directive), " to be a GraphQL directive."));
|
43 | }
|
44 |
|
45 | return directive;
|
46 | }
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | var GraphQLDirective =
|
54 |
|
55 | function () {
|
56 | function GraphQLDirective(config) {
|
57 | this.name = config.name;
|
58 | this.description = config.description;
|
59 | this.locations = config.locations;
|
60 | this.isRepeatable = config.isRepeatable != null && config.isRepeatable;
|
61 | this.extensions = config.extensions && (0, _toObjMap.default)(config.extensions);
|
62 | this.astNode = config.astNode;
|
63 | config.name || (0, _devAssert.default)(0, 'Directive must be named.');
|
64 | Array.isArray(config.locations) || (0, _devAssert.default)(0, "@".concat(config.name, " locations must be an Array."));
|
65 | var args = config.args || {};
|
66 | (0, _isObjectLike.default)(args) && !Array.isArray(args) || (0, _devAssert.default)(0, "@".concat(config.name, " args must be an object with argument names as keys."));
|
67 | this.args = (0, _objectEntries.default)(args).map(function (_ref) {
|
68 | var argName = _ref[0],
|
69 | arg = _ref[1];
|
70 | return {
|
71 | name: argName,
|
72 | description: arg.description === undefined ? null : arg.description,
|
73 | type: arg.type,
|
74 | defaultValue: arg.defaultValue,
|
75 | extensions: arg.extensions && (0, _toObjMap.default)(arg.extensions),
|
76 | astNode: arg.astNode
|
77 | };
|
78 | });
|
79 | }
|
80 |
|
81 | var _proto = GraphQLDirective.prototype;
|
82 |
|
83 | _proto.toString = function toString() {
|
84 | return '@' + this.name;
|
85 | };
|
86 |
|
87 | _proto.toConfig = function toConfig() {
|
88 | return {
|
89 | name: this.name,
|
90 | description: this.description,
|
91 | locations: this.locations,
|
92 | args: (0, _definition.argsToArgsConfig)(this.args),
|
93 | isRepeatable: this.isRepeatable,
|
94 | extensions: this.extensions,
|
95 | astNode: this.astNode
|
96 | };
|
97 | };
|
98 |
|
99 | return GraphQLDirective;
|
100 | }();
|
101 |
|
102 |
|
103 | exports.GraphQLDirective = GraphQLDirective;
|
104 | (0, _defineToStringTag.default)(GraphQLDirective);
|
105 | (0, _defineToJSON.default)(GraphQLDirective);
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | var GraphQLIncludeDirective = new GraphQLDirective({
|
111 | name: 'include',
|
112 | description: 'Directs the executor to include this field or fragment only when the `if` argument is true.',
|
113 | locations: [_directiveLocation.DirectiveLocation.FIELD, _directiveLocation.DirectiveLocation.FRAGMENT_SPREAD, _directiveLocation.DirectiveLocation.INLINE_FRAGMENT],
|
114 | args: {
|
115 | if: {
|
116 | type: (0, _definition.GraphQLNonNull)(_scalars.GraphQLBoolean),
|
117 | description: 'Included when true.'
|
118 | }
|
119 | }
|
120 | });
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | exports.GraphQLIncludeDirective = GraphQLIncludeDirective;
|
126 | var GraphQLSkipDirective = new GraphQLDirective({
|
127 | name: 'skip',
|
128 | description: 'Directs the executor to skip this field or fragment when the `if` argument is true.',
|
129 | locations: [_directiveLocation.DirectiveLocation.FIELD, _directiveLocation.DirectiveLocation.FRAGMENT_SPREAD, _directiveLocation.DirectiveLocation.INLINE_FRAGMENT],
|
130 | args: {
|
131 | if: {
|
132 | type: (0, _definition.GraphQLNonNull)(_scalars.GraphQLBoolean),
|
133 | description: 'Skipped when true.'
|
134 | }
|
135 | }
|
136 | });
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | exports.GraphQLSkipDirective = GraphQLSkipDirective;
|
142 | var DEFAULT_DEPRECATION_REASON = 'No longer supported';
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | exports.DEFAULT_DEPRECATION_REASON = DEFAULT_DEPRECATION_REASON;
|
148 | var GraphQLDeprecatedDirective = new GraphQLDirective({
|
149 | name: 'deprecated',
|
150 | description: 'Marks an element of a GraphQL schema as no longer supported.',
|
151 | locations: [_directiveLocation.DirectiveLocation.FIELD_DEFINITION, _directiveLocation.DirectiveLocation.ENUM_VALUE],
|
152 | args: {
|
153 | reason: {
|
154 | type: _scalars.GraphQLString,
|
155 | description: 'Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted using the Markdown syntax (as specified by [CommonMark](https://commonmark.org/).',
|
156 | defaultValue: DEFAULT_DEPRECATION_REASON
|
157 | }
|
158 | }
|
159 | });
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | exports.GraphQLDeprecatedDirective = GraphQLDeprecatedDirective;
|
165 | var specifiedDirectives = Object.freeze([GraphQLIncludeDirective, GraphQLSkipDirective, GraphQLDeprecatedDirective]);
|
166 | exports.specifiedDirectives = specifiedDirectives;
|
167 |
|
168 | function isSpecifiedDirective(directive) {
|
169 | return isDirective(directive) && specifiedDirectives.some(function (_ref2) {
|
170 | var name = _ref2.name;
|
171 | return name === directive.name;
|
172 | });
|
173 | }
|