UNPKG

6.32 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.GraphQLDeprecatedDirective = exports.DEFAULT_DEPRECATION_REASON = exports.GraphQLSkipDirective = exports.GraphQLIncludeDirective = exports.GraphQLDirective = void 0;
10
11var _objectEntries = _interopRequireDefault(require("../polyfills/objectEntries"));
12
13var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
14
15var _toObjMap = _interopRequireDefault(require("../jsutils/toObjMap"));
16
17var _devAssert = _interopRequireDefault(require("../jsutils/devAssert"));
18
19var _instanceOf = _interopRequireDefault(require("../jsutils/instanceOf"));
20
21var _defineToJSON = _interopRequireDefault(require("../jsutils/defineToJSON"));
22
23var _isObjectLike = _interopRequireDefault(require("../jsutils/isObjectLike"));
24
25var _defineToStringTag = _interopRequireDefault(require("../jsutils/defineToStringTag"));
26
27var _directiveLocation = require("../language/directiveLocation");
28
29var _scalars = require("./scalars");
30
31var _definition = require("./definition");
32
33function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
34
35// eslint-disable-next-line no-redeclare
36function isDirective(directive) {
37 return (0, _instanceOf.default)(directive, GraphQLDirective);
38}
39
40function 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 * Directives are used by the GraphQL runtime as a way of modifying execution
49 * behavior. Type system creators will usually not create these directly.
50 */
51
52
53var GraphQLDirective =
54/*#__PURE__*/
55function () {
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}(); // Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
101
102
103exports.GraphQLDirective = GraphQLDirective;
104(0, _defineToStringTag.default)(GraphQLDirective);
105(0, _defineToJSON.default)(GraphQLDirective);
106
107/**
108 * Used to conditionally include fields or fragments.
109 */
110var 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 * Used to conditionally skip (exclude) fields or fragments.
123 */
124
125exports.GraphQLIncludeDirective = GraphQLIncludeDirective;
126var 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 * Constant string used for default reason for a deprecation.
139 */
140
141exports.GraphQLSkipDirective = GraphQLSkipDirective;
142var DEFAULT_DEPRECATION_REASON = 'No longer supported';
143/**
144 * Used to declare element of a GraphQL schema as deprecated.
145 */
146
147exports.DEFAULT_DEPRECATION_REASON = DEFAULT_DEPRECATION_REASON;
148var 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 * The full list of specified directives.
162 */
163
164exports.GraphQLDeprecatedDirective = GraphQLDeprecatedDirective;
165var specifiedDirectives = Object.freeze([GraphQLIncludeDirective, GraphQLSkipDirective, GraphQLDeprecatedDirective]);
166exports.specifiedDirectives = specifiedDirectives;
167
168function isSpecifiedDirective(directive) {
169 return isDirective(directive) && specifiedDirectives.some(function (_ref2) {
170 var name = _ref2.name;
171 return name === directive.name;
172 });
173}