UNPKG

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