forked from graphql/graphql-js
/
directives.js
226 lines (204 loc) · 6.57 KB
/
directives.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import type { ReadOnlyObjMap, ReadOnlyObjMapLike } from '../jsutils/ObjMap';
import { inspect } from '../jsutils/inspect';
import { toObjMap } from '../jsutils/toObjMap';
import { devAssert } from '../jsutils/devAssert';
import { instanceOf } from '../jsutils/instanceOf';
import { isObjectLike } from '../jsutils/isObjectLike';
import type { DirectiveDefinitionNode } from '../language/ast';
import type { DirectiveLocationEnum } from '../language/directiveLocation';
import { DirectiveLocation } from '../language/directiveLocation';
import type {
GraphQLArgument,
GraphQLFieldConfigArgumentMap,
} from './definition';
import { GraphQLString, GraphQLBoolean } from './scalars';
import { argsToArgsConfig, GraphQLNonNull } from './definition';
/**
* Test if the given value is a GraphQL directive.
*/
declare function isDirective(
directive: mixed,
): boolean %checks(directive instanceof GraphQLDirective);
// eslint-disable-next-line no-redeclare
export function isDirective(directive) {
return instanceOf(directive, GraphQLDirective);
}
export function assertDirective(directive: mixed): GraphQLDirective {
if (!isDirective(directive)) {
throw new Error(
`Expected ${inspect(directive)} to be a GraphQL directive.`,
);
}
return directive;
}
/**
* Directives are used by the GraphQL runtime as a way of modifying execution
* behavior. Type system creators will usually not create these directly.
*/
export class GraphQLDirective {
name: string;
description: ?string;
locations: Array<DirectiveLocationEnum>;
args: Array<GraphQLArgument>;
isRepeatable: boolean;
extensions: ?ReadOnlyObjMap<mixed>;
astNode: ?DirectiveDefinitionNode;
constructor(config: $ReadOnly<GraphQLDirectiveConfig>) {
this.name = config.name;
this.description = config.description;
this.locations = config.locations;
this.isRepeatable = config.isRepeatable ?? false;
this.extensions = config.extensions && toObjMap(config.extensions);
this.astNode = config.astNode;
devAssert(config.name, 'Directive must be named.');
devAssert(
Array.isArray(config.locations),
`@${config.name} locations must be an Array.`,
);
const args = config.args ?? {};
devAssert(
isObjectLike(args) && !Array.isArray(args),
`@${config.name} args must be an object with argument names as keys.`,
);
this.args = Object.entries(args).map(([argName, argConfig]) => ({
name: argName,
description: argConfig.description,
type: argConfig.type,
defaultValue: argConfig.defaultValue,
deprecationReason: argConfig.deprecationReason,
extensions: argConfig.extensions && toObjMap(argConfig.extensions),
astNode: argConfig.astNode,
}));
}
toConfig(): GraphQLDirectiveNormalizedConfig {
return {
name: this.name,
description: this.description,
locations: this.locations,
args: argsToArgsConfig(this.args),
isRepeatable: this.isRepeatable,
extensions: this.extensions,
astNode: this.astNode,
};
}
toString(): string {
return '@' + this.name;
}
toJSON(): string {
return this.toString();
}
// $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
get [Symbol.toStringTag]() {
return 'GraphQLDirective';
}
}
export type GraphQLDirectiveConfig = {|
name: string,
description?: ?string,
locations: Array<DirectiveLocationEnum>,
args?: ?GraphQLFieldConfigArgumentMap,
isRepeatable?: ?boolean,
extensions?: ?ReadOnlyObjMapLike<mixed>,
astNode?: ?DirectiveDefinitionNode,
|};
type GraphQLDirectiveNormalizedConfig = {|
...GraphQLDirectiveConfig,
args: GraphQLFieldConfigArgumentMap,
isRepeatable: boolean,
extensions: ?ReadOnlyObjMap<mixed>,
|};
/**
* Used to conditionally include fields or fragments.
*/
export const GraphQLIncludeDirective: GraphQLDirective = new GraphQLDirective({
name: 'include',
description:
'Directs the executor to include this field or fragment only when the `if` argument is true.',
locations: [
DirectiveLocation.FIELD,
DirectiveLocation.FRAGMENT_SPREAD,
DirectiveLocation.INLINE_FRAGMENT,
],
args: {
if: {
type: new GraphQLNonNull(GraphQLBoolean),
description: 'Included when true.',
},
},
});
/**
* Used to conditionally skip (exclude) fields or fragments.
*/
export const GraphQLSkipDirective: GraphQLDirective = new GraphQLDirective({
name: 'skip',
description:
'Directs the executor to skip this field or fragment when the `if` argument is true.',
locations: [
DirectiveLocation.FIELD,
DirectiveLocation.FRAGMENT_SPREAD,
DirectiveLocation.INLINE_FRAGMENT,
],
args: {
if: {
type: new GraphQLNonNull(GraphQLBoolean),
description: 'Skipped when true.',
},
},
});
/**
* Constant string used for default reason for a deprecation.
*/
export const DEFAULT_DEPRECATION_REASON = 'No longer supported';
/**
* Used to declare element of a GraphQL schema as deprecated.
*/
export const GraphQLDeprecatedDirective: GraphQLDirective = new GraphQLDirective(
{
name: 'deprecated',
description: 'Marks an element of a GraphQL schema as no longer supported.',
locations: [
DirectiveLocation.FIELD_DEFINITION,
DirectiveLocation.ARGUMENT_DEFINITION,
DirectiveLocation.INPUT_FIELD_DEFINITION,
DirectiveLocation.ENUM_VALUE,
],
args: {
reason: {
type: GraphQLString,
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/).',
defaultValue: DEFAULT_DEPRECATION_REASON,
},
},
},
);
/**
* Used to provide a URL for specifying the behaviour of custom scalar definitions.
*/
export const GraphQLSpecifiedByDirective: GraphQLDirective = new GraphQLDirective(
{
name: 'specifiedBy',
description: 'Exposes a URL that specifies the behaviour of this scalar.',
locations: [DirectiveLocation.SCALAR],
args: {
url: {
type: new GraphQLNonNull(GraphQLString),
description: 'The URL that specifies the behaviour of this scalar.',
},
},
},
);
/**
* The full list of specified directives.
*/
export const specifiedDirectives: $ReadOnlyArray<GraphQLDirective> = Object.freeze(
[
GraphQLIncludeDirective,
GraphQLSkipDirective,
GraphQLDeprecatedDirective,
GraphQLSpecifiedByDirective,
],
);
export function isSpecifiedDirective(directive: GraphQLDirective): boolean {
return specifiedDirectives.some(({ name }) => name === directive.name);
}