forked from graphql/graphql-js
-
Notifications
You must be signed in to change notification settings - Fork 5
/
directives.js
195 lines (174 loc) · 5.38 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
// @flow strict
import objectEntries from '../polyfills/objectEntries';
import inspect from '../jsutils/inspect';
import devAssert from '../jsutils/devAssert';
import instanceOf from '../jsutils/instanceOf';
import defineToJSON from '../jsutils/defineToJSON';
import isObjectLike from '../jsutils/isObjectLike';
import defineToStringTag from '../jsutils/defineToStringTag';
import { type DirectiveDefinitionNode } from '../language/ast';
import {
DirectiveLocation,
type DirectiveLocationEnum,
} from '../language/directiveLocation';
import { GraphQLString, GraphQLBoolean } from './scalars';
import {
type GraphQLFieldConfigArgumentMap,
type GraphQLArgument,
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>;
isRepeatable: boolean;
args: Array<GraphQLArgument>;
astNode: ?DirectiveDefinitionNode;
constructor(config: GraphQLDirectiveConfig): void {
this.name = config.name;
this.description = config.description;
this.locations = config.locations;
this.isRepeatable = config.isRepeatable != null && config.isRepeatable;
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 = objectEntries(args).map(([argName, arg]) => ({
name: argName,
description: arg.description === undefined ? null : arg.description,
type: arg.type,
defaultValue: arg.defaultValue,
astNode: arg.astNode,
}));
}
toString(): string {
return '@' + this.name;
}
toConfig(): {|
...GraphQLDirectiveConfig,
args: GraphQLFieldConfigArgumentMap,
isRepeatable: boolean,
|} {
return {
name: this.name,
description: this.description,
locations: this.locations,
args: argsToArgsConfig(this.args),
isRepeatable: this.isRepeatable,
astNode: this.astNode,
};
}
}
// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
defineToStringTag(GraphQLDirective);
defineToJSON(GraphQLDirective);
export type GraphQLDirectiveConfig = {|
name: string,
description?: ?string,
locations: Array<DirectiveLocationEnum>,
args?: ?GraphQLFieldConfigArgumentMap,
isRepeatable?: ?boolean,
astNode?: ?DirectiveDefinitionNode,
|};
/**
* Used to conditionally include fields or fragments.
*/
export const GraphQLIncludeDirective = 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: GraphQLNonNull(GraphQLBoolean),
description: 'Included when true.',
},
},
});
/**
* Used to conditionally skip (exclude) fields or fragments.
*/
export const GraphQLSkipDirective = 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: 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 = new GraphQLDirective({
name: 'deprecated',
description: 'Marks an element of a GraphQL schema as no longer supported.',
locations: [DirectiveLocation.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,
},
},
});
/**
* The full list of specified directives.
*/
export const specifiedDirectives = Object.freeze([
GraphQLIncludeDirective,
GraphQLSkipDirective,
GraphQLDeprecatedDirective,
]);
export function isSpecifiedDirective(directive: mixed): boolean %checks {
return (
isDirective(directive) &&
specifiedDirectives.some(({ name }) => name === directive.name)
);
}