1 | import objectEntries from '../polyfills/objectEntries';
|
2 | import inspect from '../jsutils/inspect';
|
3 | import toObjMap from '../jsutils/toObjMap';
|
4 | import devAssert from '../jsutils/devAssert';
|
5 | import instanceOf from '../jsutils/instanceOf';
|
6 | import defineToJSON from '../jsutils/defineToJSON';
|
7 | import isObjectLike from '../jsutils/isObjectLike';
|
8 | import defineToStringTag from '../jsutils/defineToStringTag';
|
9 | import { DirectiveLocation } from '../language/directiveLocation';
|
10 | import { GraphQLString, GraphQLBoolean } from './scalars';
|
11 | import { argsToArgsConfig, GraphQLNonNull } from './definition';
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | export function isDirective(directive) {
|
18 | return instanceOf(directive, GraphQLDirective);
|
19 | }
|
20 | export 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 |
|
29 |
|
30 |
|
31 |
|
32 | export var GraphQLDirective =
|
33 |
|
34 | function () {
|
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 | }();
|
80 |
|
81 | defineToStringTag(GraphQLDirective);
|
82 | defineToJSON(GraphQLDirective);
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | export 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 |
|
100 |
|
101 |
|
102 | export 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 |
|
115 |
|
116 |
|
117 | export var DEFAULT_DEPRECATION_REASON = 'No longer supported';
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | export 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 |
|
136 |
|
137 |
|
138 | export var specifiedDirectives = Object.freeze([GraphQLIncludeDirective, GraphQLSkipDirective, GraphQLDeprecatedDirective]);
|
139 | export function isSpecifiedDirective(directive) {
|
140 | return isDirective(directive) && specifiedDirectives.some(function (_ref2) {
|
141 | var name = _ref2.name;
|
142 | return name === directive.name;
|
143 | });
|
144 | }
|