-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathdirectives.ts
293 lines (267 loc) · 8.47 KB
/
directives.ts
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import { devAssert } from '../jsutils/devAssert.js';
import { inspect } from '../jsutils/inspect.js';
import { instanceOf } from '../jsutils/instanceOf.js';
import { isObjectLike } from '../jsutils/isObjectLike.js';
import { keyValMap } from '../jsutils/keyValMap.js';
import type { Maybe } from '../jsutils/Maybe.js';
import type { ObjMap } from '../jsutils/ObjMap.js';
import { toObjMapWithSymbols } from '../jsutils/toObjMap.js';
import type { DirectiveDefinitionNode } from '../language/ast.js';
import { DirectiveLocation } from '../language/directiveLocation.js';
import { assertName } from './assertName.js';
import type {
GraphQLArgumentConfig,
GraphQLFieldNormalizedConfigArgumentMap,
GraphQLSchemaElement,
} from './definition.js';
import { GraphQLArgument, GraphQLNonNull } from './definition.js';
import { GraphQLBoolean, GraphQLInt, GraphQLString } from './scalars.js';
/**
* Test if the given value is a GraphQL directive.
*/
export function isDirective(directive: unknown): directive is GraphQLDirective {
return instanceOf(directive, GraphQLDirective);
}
export function assertDirective(directive: unknown): GraphQLDirective {
if (!isDirective(directive)) {
throw new Error(
`Expected ${inspect(directive)} to be a GraphQL directive.`,
);
}
return directive;
}
/**
* Custom extensions
*
* @remarks
* Use a unique identifier name for your extension, for example the name of
* your library or project. Do not use a shortened identifier as this increases
* the risk of conflicts. We recommend you add at most one extension field,
* an object which can contain all the values you need.
*/
export interface GraphQLDirectiveExtensions {
[attributeName: string | symbol]: unknown;
}
/**
* 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 implements GraphQLSchemaElement {
name: string;
description: Maybe<string>;
locations: ReadonlyArray<DirectiveLocation>;
args: ReadonlyArray<GraphQLArgument>;
isRepeatable: boolean;
extensions: Readonly<GraphQLDirectiveExtensions>;
astNode: Maybe<DirectiveDefinitionNode>;
constructor(config: Readonly<GraphQLDirectiveConfig>) {
this.name = assertName(config.name);
this.description = config.description;
this.locations = config.locations;
this.isRepeatable = config.isRepeatable ?? false;
this.extensions = toObjMapWithSymbols(config.extensions);
this.astNode = config.astNode;
devAssert(
Array.isArray(config.locations),
`@${this.name} locations must be an Array.`,
);
const args = config.args ?? {};
devAssert(
isObjectLike(args) && !Array.isArray(args),
`@${this.name} args must be an object with argument names as keys.`,
);
this.args = Object.entries(args).map(
([argName, argConfig]) => new GraphQLArgument(this, argName, argConfig),
);
}
get [Symbol.toStringTag]() {
return 'GraphQLDirective';
}
toConfig(): GraphQLDirectiveNormalizedConfig {
return {
name: this.name,
description: this.description,
locations: this.locations,
args: keyValMap(
this.args,
(arg) => arg.name,
(arg) => arg.toConfig(),
),
isRepeatable: this.isRepeatable,
extensions: this.extensions,
astNode: this.astNode,
};
}
toString(): string {
return '@' + this.name;
}
toJSON(): string {
return this.toString();
}
}
export interface GraphQLDirectiveConfig {
name: string;
description?: Maybe<string>;
locations: ReadonlyArray<DirectiveLocation>;
args?: Maybe<ObjMap<GraphQLArgumentConfig>>;
isRepeatable?: Maybe<boolean>;
extensions?: Maybe<Readonly<GraphQLDirectiveExtensions>>;
astNode?: Maybe<DirectiveDefinitionNode>;
}
export interface GraphQLDirectiveNormalizedConfig
extends GraphQLDirectiveConfig {
args: GraphQLFieldNormalizedConfigArgumentMap;
isRepeatable: boolean;
extensions: Readonly<GraphQLDirectiveExtensions>;
}
/**
* 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.',
},
},
});
/**
* Used to conditionally defer fragments.
*/
export const GraphQLDeferDirective = new GraphQLDirective({
name: 'defer',
description:
'Directs the executor to defer this fragment when the `if` argument is true or undefined.',
locations: [
DirectiveLocation.FRAGMENT_SPREAD,
DirectiveLocation.INLINE_FRAGMENT,
],
args: {
if: {
type: new GraphQLNonNull(GraphQLBoolean),
description: 'Deferred when true or undefined.',
default: { value: true },
},
label: {
type: GraphQLString,
description: 'Unique name',
},
},
});
/**
* Used to conditionally stream list fields.
*/
export const GraphQLStreamDirective = new GraphQLDirective({
name: 'stream',
description:
'Directs the executor to stream plural fields when the `if` argument is true or undefined.',
locations: [DirectiveLocation.FIELD],
args: {
initialCount: {
default: { value: 0 },
type: new GraphQLNonNull(GraphQLInt),
description: 'Number of items to return immediately',
},
if: {
type: new GraphQLNonNull(GraphQLBoolean),
description: 'Stream when true or undefined.',
default: { value: true },
},
label: {
type: GraphQLString,
description: 'Unique name',
},
},
});
/**
* 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: new GraphQLNonNull(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/).',
default: { value: DEFAULT_DEPRECATION_REASON },
},
},
});
/**
* Used to provide a URL for specifying the behavior of custom scalar definitions.
*/
export const GraphQLSpecifiedByDirective: GraphQLDirective =
new GraphQLDirective({
name: 'specifiedBy',
description: 'Exposes a URL that specifies the behavior of this scalar.',
locations: [DirectiveLocation.SCALAR],
args: {
url: {
type: new GraphQLNonNull(GraphQLString),
description: 'The URL that specifies the behavior of this scalar.',
},
},
});
/**
* Used to indicate an Input Object is a OneOf Input Object.
*/
export const GraphQLOneOfDirective: GraphQLDirective = new GraphQLDirective({
name: 'oneOf',
description:
'Indicates exactly one field must be supplied and this field must not be `null`.',
locations: [DirectiveLocation.INPUT_OBJECT],
args: {},
});
/**
* The full list of specified directives.
*/
export const specifiedDirectives: ReadonlyArray<GraphQLDirective> =
Object.freeze([
GraphQLIncludeDirective,
GraphQLSkipDirective,
GraphQLDeprecatedDirective,
GraphQLSpecifiedByDirective,
GraphQLOneOfDirective,
]);
export function isSpecifiedDirective(directive: GraphQLDirective): boolean {
return specifiedDirectives.some(({ name }) => name === directive.name);
}