UNPKG

5.02 kBJavaScriptView Raw
1import objectEntries from '../polyfills/objectEntries';
2import inspect from '../jsutils/inspect';
3import toObjMap from '../jsutils/toObjMap';
4import devAssert from '../jsutils/devAssert';
5import instanceOf from '../jsutils/instanceOf';
6import defineToJSON from '../jsutils/defineToJSON';
7import isObjectLike from '../jsutils/isObjectLike';
8import defineToStringTag from '../jsutils/defineToStringTag';
9import { DirectiveLocation } from '../language/directiveLocation';
10import { GraphQLString, GraphQLBoolean } from './scalars';
11import { argsToArgsConfig, GraphQLNonNull } from './definition';
12/**
13 * Test if the given value is a GraphQL directive.
14 */
15
16// eslint-disable-next-line no-redeclare
17export function isDirective(directive) {
18 return instanceOf(directive, GraphQLDirective);
19}
20export function assertDirective(directive) {
21 if (!isDirective(directive)) {
22 throw new Error("Expected ".concat(inspect(directive), " to be a GraphQL directive."));
23 }
24
25 return directive;
26}
27/**
28 * Directives are used by the GraphQL runtime as a way of modifying execution
29 * behavior. Type system creators will usually not create these directly.
30 */
31
32export var GraphQLDirective =
33/*#__PURE__*/
34function () {
35 function GraphQLDirective(config) {
36 this.name = config.name;
37 this.description = config.description;
38 this.locations = config.locations;
39 this.isRepeatable = config.isRepeatable != null && config.isRepeatable;
40 this.extensions = config.extensions && toObjMap(config.extensions);
41 this.astNode = config.astNode;
42 config.name || devAssert(0, 'Directive must be named.');
43 Array.isArray(config.locations) || devAssert(0, "@".concat(config.name, " locations must be an Array."));
44 var args = config.args || {};
45 isObjectLike(args) && !Array.isArray(args) || devAssert(0, "@".concat(config.name, " args must be an object with argument names as keys."));
46 this.args = objectEntries(args).map(function (_ref) {
47 var argName = _ref[0],
48 arg = _ref[1];
49 return {
50 name: argName,
51 description: arg.description === undefined ? null : arg.description,
52 type: arg.type,
53 defaultValue: arg.defaultValue,
54 extensions: arg.extensions && toObjMap(arg.extensions),
55 astNode: arg.astNode
56 };
57 });
58 }
59
60 var _proto = GraphQLDirective.prototype;
61
62 _proto.toString = function toString() {
63 return '@' + this.name;
64 };
65
66 _proto.toConfig = function toConfig() {
67 return {
68 name: this.name,
69 description: this.description,
70 locations: this.locations,
71 args: argsToArgsConfig(this.args),
72 isRepeatable: this.isRepeatable,
73 extensions: this.extensions,
74 astNode: this.astNode
75 };
76 };
77
78 return GraphQLDirective;
79}(); // Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
80
81defineToStringTag(GraphQLDirective);
82defineToJSON(GraphQLDirective);
83
84/**
85 * Used to conditionally include fields or fragments.
86 */
87export var GraphQLIncludeDirective = new GraphQLDirective({
88 name: 'include',
89 description: 'Directs the executor to include this field or fragment only when the `if` argument is true.',
90 locations: [DirectiveLocation.FIELD, DirectiveLocation.FRAGMENT_SPREAD, DirectiveLocation.INLINE_FRAGMENT],
91 args: {
92 if: {
93 type: GraphQLNonNull(GraphQLBoolean),
94 description: 'Included when true.'
95 }
96 }
97});
98/**
99 * Used to conditionally skip (exclude) fields or fragments.
100 */
101
102export var GraphQLSkipDirective = new GraphQLDirective({
103 name: 'skip',
104 description: 'Directs the executor to skip this field or fragment when the `if` argument is true.',
105 locations: [DirectiveLocation.FIELD, DirectiveLocation.FRAGMENT_SPREAD, DirectiveLocation.INLINE_FRAGMENT],
106 args: {
107 if: {
108 type: GraphQLNonNull(GraphQLBoolean),
109 description: 'Skipped when true.'
110 }
111 }
112});
113/**
114 * Constant string used for default reason for a deprecation.
115 */
116
117export var DEFAULT_DEPRECATION_REASON = 'No longer supported';
118/**
119 * Used to declare element of a GraphQL schema as deprecated.
120 */
121
122export var GraphQLDeprecatedDirective = new GraphQLDirective({
123 name: 'deprecated',
124 description: 'Marks an element of a GraphQL schema as no longer supported.',
125 locations: [DirectiveLocation.FIELD_DEFINITION, DirectiveLocation.ENUM_VALUE],
126 args: {
127 reason: {
128 type: GraphQLString,
129 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/).',
130 defaultValue: DEFAULT_DEPRECATION_REASON
131 }
132 }
133});
134/**
135 * The full list of specified directives.
136 */
137
138export var specifiedDirectives = Object.freeze([GraphQLIncludeDirective, GraphQLSkipDirective, GraphQLDeprecatedDirective]);
139export function isSpecifiedDirective(directive) {
140 return isDirective(directive) && specifiedDirectives.some(function (_ref2) {
141 var name = _ref2.name;
142 return name === directive.name;
143 });
144}