-
-
Notifications
You must be signed in to change notification settings - Fork 796
/
get-directives.ts
207 lines (179 loc) · 5.99 KB
/
get-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
import {
GraphQLDirective,
GraphQLSchema,
SchemaDefinitionNode,
TypeDefinitionNode,
SchemaExtensionNode,
TypeExtensionNode,
GraphQLNamedType,
GraphQLField,
GraphQLInputField,
FieldDefinitionNode,
InputValueDefinitionNode,
GraphQLFieldConfig,
GraphQLInputFieldConfig,
GraphQLSchemaConfig,
GraphQLObjectTypeConfig,
GraphQLInterfaceTypeConfig,
GraphQLUnionTypeConfig,
GraphQLScalarTypeConfig,
GraphQLEnumTypeConfig,
GraphQLInputObjectTypeConfig,
GraphQLEnumValue,
GraphQLEnumValueConfig,
EnumValueDefinitionNode,
getArgumentValues,
} from 'graphql';
export interface DirectiveAnnotation {
name: string;
args?: Record<string, any>;
}
type SchemaOrTypeNode =
| SchemaDefinitionNode
| SchemaExtensionNode
| TypeDefinitionNode
| TypeExtensionNode
| EnumValueDefinitionNode
| FieldDefinitionNode
| InputValueDefinitionNode;
type DirectableGraphQLObject =
| GraphQLSchema
| GraphQLSchemaConfig
| GraphQLNamedType
| GraphQLObjectTypeConfig<any, any>
| GraphQLInterfaceTypeConfig<any, any>
| GraphQLUnionTypeConfig<any, any>
| GraphQLScalarTypeConfig<any, any>
| GraphQLEnumTypeConfig
| GraphQLEnumValue
| GraphQLEnumValueConfig
| GraphQLInputObjectTypeConfig
| GraphQLField<any, any>
| GraphQLInputField
| GraphQLFieldConfig<any, any>
| GraphQLInputFieldConfig;
export function getDirectivesInExtensions(
node: DirectableGraphQLObject,
pathToDirectivesInExtensions = ['directives']
): Array<DirectiveAnnotation> {
return pathToDirectivesInExtensions.reduce(
(acc, pathSegment) => (acc == null ? acc : acc[pathSegment]),
node?.extensions as unknown as Array<DirectiveAnnotation>
);
}
function _getDirectiveInExtensions(
directivesInExtensions: Array<DirectiveAnnotation>,
directiveName: string
): Array<Record<string, any>> | undefined {
const directiveInExtensions = directivesInExtensions.filter(
directiveAnnotation => directiveAnnotation.name === directiveName
);
if (!directiveInExtensions.length) {
return undefined;
}
return directiveInExtensions.map(directive => directive.args ?? {});
}
export function getDirectiveInExtensions(
node: DirectableGraphQLObject,
directiveName: string,
pathToDirectivesInExtensions = ['directives']
): Array<Record<string, any>> | undefined {
const directivesInExtensions = pathToDirectivesInExtensions.reduce(
(acc, pathSegment) => (acc == null ? acc : acc[pathSegment]),
node?.extensions as
| Record<string, Record<string, any> | Array<Record<string, any>>>
| Array<DirectiveAnnotation>
| undefined
);
if (directivesInExtensions === undefined) {
return undefined;
}
if (Array.isArray(directivesInExtensions)) {
return _getDirectiveInExtensions(directivesInExtensions, directiveName);
}
// Support condensed format by converting to longer format
// The condensed format does not preserve ordering of directives when repeatable directives are used.
// See https://github.com/ardatan/graphql-tools/issues/2534
const reformattedDirectivesInExtensions: Array<DirectiveAnnotation> = [];
for (const [name, argsOrArrayOfArgs] of Object.entries(directivesInExtensions)) {
if (Array.isArray(argsOrArrayOfArgs)) {
for (const args of argsOrArrayOfArgs) {
reformattedDirectivesInExtensions.push({ name, args });
}
} else {
reformattedDirectivesInExtensions.push({ name, args: argsOrArrayOfArgs });
}
}
return _getDirectiveInExtensions(reformattedDirectivesInExtensions, directiveName);
}
export function getDirectives(
schema: GraphQLSchema,
node: DirectableGraphQLObject,
pathToDirectivesInExtensions = ['directives']
): Array<DirectiveAnnotation> {
const directivesInExtensions = getDirectivesInExtensions(node, pathToDirectivesInExtensions);
if (directivesInExtensions != null && directivesInExtensions.length > 0) {
return directivesInExtensions;
}
const schemaDirectives: ReadonlyArray<GraphQLDirective> =
schema && schema.getDirectives ? schema.getDirectives() : [];
const schemaDirectiveMap = schemaDirectives.reduce((schemaDirectiveMap, schemaDirective) => {
schemaDirectiveMap[schemaDirective.name] = schemaDirective;
return schemaDirectiveMap;
}, {});
let astNodes: Array<SchemaOrTypeNode> = [];
if (node.astNode) {
astNodes.push(node.astNode);
}
if ('extensionASTNodes' in node && node.extensionASTNodes) {
astNodes = [...astNodes, ...node.extensionASTNodes];
}
const result: Array<DirectiveAnnotation> = [];
for (const astNode of astNodes) {
if (astNode.directives) {
for (const directiveNode of astNode.directives) {
const schemaDirective = schemaDirectiveMap[directiveNode.name.value];
if (schemaDirective) {
result.push({ name: directiveNode.name.value, args: getArgumentValues(schemaDirective, directiveNode) });
}
}
}
}
return result;
}
export function getDirective(
schema: GraphQLSchema,
node: DirectableGraphQLObject,
directiveName: string,
pathToDirectivesInExtensions = ['directives']
): Array<Record<string, any>> | undefined {
const directiveInExtensions = getDirectiveInExtensions(node, directiveName, pathToDirectivesInExtensions);
if (directiveInExtensions != null) {
return directiveInExtensions;
}
const schemaDirective = schema && schema.getDirective ? schema.getDirective(directiveName) : undefined;
if (schemaDirective == null) {
return undefined;
}
let astNodes: Array<SchemaOrTypeNode> = [];
if (node.astNode) {
astNodes.push(node.astNode);
}
if ('extensionASTNodes' in node && node.extensionASTNodes) {
astNodes = [...astNodes, ...node.extensionASTNodes];
}
const result: Array<Record<string, any>> = [];
for (const astNode of astNodes) {
if (astNode.directives) {
for (const directiveNode of astNode.directives) {
if (directiveNode.name.value === directiveName) {
result.push(getArgumentValues(schemaDirective, directiveNode));
}
}
}
}
if (!result.length) {
return undefined;
}
return result;
}