UNPKG

8.06 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.isDirective = isDirective;
7exports.assertDirective = assertDirective;
8exports.isSpecifiedDirective = isSpecifiedDirective;
9exports.specifiedDirectives = exports.GraphQLSpecifiedByDirective = exports.GraphQLDeprecatedDirective = exports.DEFAULT_DEPRECATION_REASON = exports.GraphQLSkipDirective = exports.GraphQLIncludeDirective = exports.GraphQLDirective = void 0;
10
11var _objectEntries = _interopRequireDefault(require("../polyfills/objectEntries.js"));
12
13var _symbols = require("../polyfills/symbols.js");
14
15var _inspect = _interopRequireDefault(require("../jsutils/inspect.js"));
16
17var _toObjMap = _interopRequireDefault(require("../jsutils/toObjMap.js"));
18
19var _devAssert = _interopRequireDefault(require("../jsutils/devAssert.js"));
20
21var _instanceOf = _interopRequireDefault(require("../jsutils/instanceOf.js"));
22
23var _isObjectLike = _interopRequireDefault(require("../jsutils/isObjectLike.js"));
24
25var _defineInspect = _interopRequireDefault(require("../jsutils/defineInspect.js"));
26
27var _directiveLocation = require("../language/directiveLocation.js");
28
29var _scalars = require("./scalars.js");
30
31var _definition = require("./definition.js");
32
33function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
34
35function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
36
37function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
38
39// eslint-disable-next-line no-redeclare
40function isDirective(directive) {
41 return (0, _instanceOf.default)(directive, GraphQLDirective);
42}
43
44function assertDirective(directive) {
45 if (!isDirective(directive)) {
46 throw new Error("Expected ".concat((0, _inspect.default)(directive), " to be a GraphQL directive."));
47 }
48
49 return directive;
50}
51/**
52 * Directives are used by the GraphQL runtime as a way of modifying execution
53 * behavior. Type system creators will usually not create these directly.
54 */
55
56
57var GraphQLDirective = /*#__PURE__*/function () {
58 function GraphQLDirective(config) {
59 var _config$isRepeatable, _config$args;
60
61 this.name = config.name;
62 this.description = config.description;
63 this.locations = config.locations;
64 this.isRepeatable = (_config$isRepeatable = config.isRepeatable) !== null && _config$isRepeatable !== void 0 ? _config$isRepeatable : false;
65 this.extensions = config.extensions && (0, _toObjMap.default)(config.extensions);
66 this.astNode = config.astNode;
67 config.name || (0, _devAssert.default)(0, 'Directive must be named.');
68 Array.isArray(config.locations) || (0, _devAssert.default)(0, "@".concat(config.name, " locations must be an Array."));
69 var args = (_config$args = config.args) !== null && _config$args !== void 0 ? _config$args : {};
70 (0, _isObjectLike.default)(args) && !Array.isArray(args) || (0, _devAssert.default)(0, "@".concat(config.name, " args must be an object with argument names as keys."));
71 this.args = (0, _objectEntries.default)(args).map(function (_ref) {
72 var argName = _ref[0],
73 argConfig = _ref[1];
74 return {
75 name: argName,
76 description: argConfig.description,
77 type: argConfig.type,
78 defaultValue: argConfig.defaultValue,
79 deprecationReason: argConfig.deprecationReason,
80 extensions: argConfig.extensions && (0, _toObjMap.default)(argConfig.extensions),
81 astNode: argConfig.astNode
82 };
83 });
84 }
85
86 var _proto = GraphQLDirective.prototype;
87
88 _proto.toConfig = function toConfig() {
89 return {
90 name: this.name,
91 description: this.description,
92 locations: this.locations,
93 args: (0, _definition.argsToArgsConfig)(this.args),
94 isRepeatable: this.isRepeatable,
95 extensions: this.extensions,
96 astNode: this.astNode
97 };
98 };
99
100 _proto.toString = function toString() {
101 return '@' + this.name;
102 };
103
104 _proto.toJSON = function toJSON() {
105 return this.toString();
106 } // $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
107 ;
108
109 _createClass(GraphQLDirective, [{
110 key: _symbols.SYMBOL_TO_STRING_TAG,
111 get: function get() {
112 return 'GraphQLDirective';
113 }
114 }]);
115
116 return GraphQLDirective;
117}(); // Print a simplified form when appearing in `inspect` and `util.inspect`.
118
119
120exports.GraphQLDirective = GraphQLDirective;
121(0, _defineInspect.default)(GraphQLDirective);
122
123/**
124 * Used to conditionally include fields or fragments.
125 */
126var GraphQLIncludeDirective = new GraphQLDirective({
127 name: 'include',
128 description: 'Directs the executor to include this field or fragment only 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: new _definition.GraphQLNonNull(_scalars.GraphQLBoolean),
133 description: 'Included when true.'
134 }
135 }
136});
137/**
138 * Used to conditionally skip (exclude) fields or fragments.
139 */
140
141exports.GraphQLIncludeDirective = GraphQLIncludeDirective;
142var GraphQLSkipDirective = new GraphQLDirective({
143 name: 'skip',
144 description: 'Directs the executor to skip this field or fragment when the `if` argument is true.',
145 locations: [_directiveLocation.DirectiveLocation.FIELD, _directiveLocation.DirectiveLocation.FRAGMENT_SPREAD, _directiveLocation.DirectiveLocation.INLINE_FRAGMENT],
146 args: {
147 if: {
148 type: new _definition.GraphQLNonNull(_scalars.GraphQLBoolean),
149 description: 'Skipped when true.'
150 }
151 }
152});
153/**
154 * Constant string used for default reason for a deprecation.
155 */
156
157exports.GraphQLSkipDirective = GraphQLSkipDirective;
158var DEFAULT_DEPRECATION_REASON = 'No longer supported';
159/**
160 * Used to declare element of a GraphQL schema as deprecated.
161 */
162
163exports.DEFAULT_DEPRECATION_REASON = DEFAULT_DEPRECATION_REASON;
164var GraphQLDeprecatedDirective = new GraphQLDirective({
165 name: 'deprecated',
166 description: 'Marks an element of a GraphQL schema as no longer supported.',
167 locations: [_directiveLocation.DirectiveLocation.FIELD_DEFINITION, _directiveLocation.DirectiveLocation.ARGUMENT_DEFINITION, _directiveLocation.DirectiveLocation.INPUT_FIELD_DEFINITION, _directiveLocation.DirectiveLocation.ENUM_VALUE],
168 args: {
169 reason: {
170 type: _scalars.GraphQLString,
171 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/).',
172 defaultValue: DEFAULT_DEPRECATION_REASON
173 }
174 }
175});
176/**
177 * Used to provide a URL for specifying the behaviour of custom scalar definitions.
178 */
179
180exports.GraphQLDeprecatedDirective = GraphQLDeprecatedDirective;
181var GraphQLSpecifiedByDirective = new GraphQLDirective({
182 name: 'specifiedBy',
183 description: 'Exposes a URL that specifies the behaviour of this scalar.',
184 locations: [_directiveLocation.DirectiveLocation.SCALAR],
185 args: {
186 url: {
187 type: new _definition.GraphQLNonNull(_scalars.GraphQLString),
188 description: 'The URL that specifies the behaviour of this scalar.'
189 }
190 }
191});
192/**
193 * The full list of specified directives.
194 */
195
196exports.GraphQLSpecifiedByDirective = GraphQLSpecifiedByDirective;
197var specifiedDirectives = Object.freeze([GraphQLIncludeDirective, GraphQLSkipDirective, GraphQLDeprecatedDirective, GraphQLSpecifiedByDirective]);
198exports.specifiedDirectives = specifiedDirectives;
199
200function isSpecifiedDirective(directive) {
201 return specifiedDirectives.some(function (_ref2) {
202 var name = _ref2.name;
203 return name === directive.name;
204 });
205}