Skip to content

Commit 57619c9

Browse files
committed
Replace '.getDirectives' with generic method
1 parent 89710c8 commit 57619c9

File tree

2 files changed

+18
-153
lines changed

2 files changed

+18
-153
lines changed

src/type/definition.js

Lines changed: 0 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import type {
3535
OperationDefinitionNode,
3636
FieldNode,
3737
FragmentDefinitionNode,
38-
DirectiveNode,
3938
ValueNode,
4039
} from '../language/ast';
4140
import type { GraphQLSchema } from './schema';
@@ -542,8 +541,6 @@ export class GraphQLScalarType {
542541
astNode: ?ScalarTypeDefinitionNode;
543542
extensionASTNodes: ?$ReadOnlyArray<ScalarTypeExtensionNode>;
544543

545-
_directives: ?$ReadOnlyArray<DirectiveNode>;
546-
547544
constructor(config: GraphQLScalarTypeConfig<*, *>): void {
548545
this.name = config.name;
549546
this.description = config.description;
@@ -569,28 +566,6 @@ export class GraphQLScalarType {
569566
}
570567
}
571568

572-
getDirectives(): $ReadOnlyArray<DirectiveNode> {
573-
if (this._directives) {
574-
return this._directives;
575-
}
576-
577-
const directives = [];
578-
if (this.astNode && this.astNode.directives) {
579-
directives.push(...this.astNode.directives);
580-
}
581-
const extensionASTNodes = this.extensionASTNodes;
582-
if (extensionASTNodes) {
583-
for (let i = 0; i < extensionASTNodes.length; i++) {
584-
const extensionNode = extensionASTNodes[i];
585-
if (extensionNode.directives) {
586-
directives.push(...extensionNode.directives);
587-
}
588-
}
589-
}
590-
this._directives = directives;
591-
return directives;
592-
}
593-
594569
toString(): string {
595570
return this.name;
596571
}
@@ -666,7 +641,6 @@ export class GraphQLObjectType {
666641

667642
_fields: Thunk<GraphQLFieldMap<*, *>>;
668643
_interfaces: Thunk<Array<GraphQLInterfaceType>>;
669-
_directives: ?$ReadOnlyArray<DirectiveNode>;
670644

671645
constructor(config: GraphQLObjectTypeConfig<*, *>): void {
672646
this.name = config.name;
@@ -685,28 +659,6 @@ export class GraphQLObjectType {
685659
}
686660
}
687661

688-
getDirectives(): $ReadOnlyArray<DirectiveNode> {
689-
if (this._directives) {
690-
return this._directives;
691-
}
692-
693-
const directives = [];
694-
if (this.astNode && this.astNode.directives) {
695-
directives.push(...this.astNode.directives);
696-
}
697-
const extensionASTNodes = this.extensionASTNodes;
698-
if (extensionASTNodes) {
699-
for (let i = 0; i < extensionASTNodes.length; i++) {
700-
const extensionNode = extensionASTNodes[i];
701-
if (extensionNode.directives) {
702-
directives.push(...extensionNode.directives);
703-
}
704-
}
705-
}
706-
this._directives = directives;
707-
return directives;
708-
}
709-
710662
getFields(): GraphQLFieldMap<*, *> {
711663
if (typeof this._fields === 'function') {
712664
this._fields = this._fields();
@@ -942,7 +894,6 @@ export class GraphQLInterfaceType {
942894
resolveType: ?GraphQLTypeResolver<*, *>;
943895

944896
_fields: Thunk<GraphQLFieldMap<*, *>>;
945-
_directives: ?$ReadOnlyArray<DirectiveNode>;
946897

947898
constructor(config: GraphQLInterfaceTypeConfig<*, *>): void {
948899
this.name = config.name;
@@ -960,28 +911,6 @@ export class GraphQLInterfaceType {
960911
}
961912
}
962913

963-
getDirectives(): $ReadOnlyArray<DirectiveNode> {
964-
if (this._directives) {
965-
return this._directives;
966-
}
967-
968-
const directives = [];
969-
if (this.astNode && this.astNode.directives) {
970-
directives.push(...this.astNode.directives);
971-
}
972-
const extensionASTNodes = this.extensionASTNodes;
973-
if (extensionASTNodes) {
974-
for (let i = 0; i < extensionASTNodes.length; i++) {
975-
const extensionNode = extensionASTNodes[i];
976-
if (extensionNode.directives) {
977-
directives.push(...extensionNode.directives);
978-
}
979-
}
980-
}
981-
this._directives = directives;
982-
return directives;
983-
}
984-
985914
getFields(): GraphQLFieldMap<*, *> {
986915
if (typeof this._fields === 'function') {
987916
this._fields = this._fields();
@@ -1043,7 +972,6 @@ export class GraphQLUnionType {
1043972
resolveType: ?GraphQLTypeResolver<*, *>;
1044973

1045974
_types: Thunk<Array<GraphQLObjectType>>;
1046-
_directives: ?$ReadOnlyArray<DirectiveNode>;
1047975

1048976
constructor(config: GraphQLUnionTypeConfig<*, *>): void {
1049977
this.name = config.name;
@@ -1061,28 +989,6 @@ export class GraphQLUnionType {
1061989
}
1062990
}
1063991

1064-
getDirectives(): $ReadOnlyArray<DirectiveNode> {
1065-
if (this._directives) {
1066-
return this._directives;
1067-
}
1068-
1069-
const directives = [];
1070-
if (this.astNode && this.astNode.directives) {
1071-
directives.push(...this.astNode.directives);
1072-
}
1073-
const extensionASTNodes = this.extensionASTNodes;
1074-
if (extensionASTNodes) {
1075-
for (let i = 0; i < extensionASTNodes.length; i++) {
1076-
const extensionNode = extensionASTNodes[i];
1077-
if (extensionNode.directives) {
1078-
directives.push(...extensionNode.directives);
1079-
}
1080-
}
1081-
}
1082-
this._directives = directives;
1083-
return directives;
1084-
}
1085-
1086992
getTypes(): Array<GraphQLObjectType> {
1087993
if (typeof this._types === 'function') {
1088994
this._types = this._types();
@@ -1152,7 +1058,6 @@ export class GraphQLEnumType /* <T> */ {
11521058
astNode: ?EnumTypeDefinitionNode;
11531059
extensionASTNodes: ?$ReadOnlyArray<EnumTypeExtensionNode>;
11541060

1155-
_directives: ?$ReadOnlyArray<DirectiveNode>;
11561061
_values: Array<GraphQLEnumValue /* <T> */>;
11571062
_valueLookup: Map<any /* T */, GraphQLEnumValue>;
11581063
_nameLookup: ObjMap<GraphQLEnumValue>;
@@ -1171,28 +1076,6 @@ export class GraphQLEnumType /* <T> */ {
11711076
invariant(typeof config.name === 'string', 'Must provide name.');
11721077
}
11731078

1174-
getDirectives(): $ReadOnlyArray<DirectiveNode> {
1175-
if (this._directives) {
1176-
return this._directives;
1177-
}
1178-
1179-
const directives = [];
1180-
if (this.astNode && this.astNode.directives) {
1181-
directives.push(...this.astNode.directives);
1182-
}
1183-
const extensionASTNodes = this.extensionASTNodes;
1184-
if (extensionASTNodes) {
1185-
for (let i = 0; i < extensionASTNodes.length; i++) {
1186-
const extensionNode = extensionASTNodes[i];
1187-
if (extensionNode.directives) {
1188-
directives.push(...extensionNode.directives);
1189-
}
1190-
}
1191-
}
1192-
this._directives = directives;
1193-
return directives;
1194-
}
1195-
11961079
getValues(): Array<GraphQLEnumValue /* <T> */> {
11971080
return this._values;
11981081
}
@@ -1321,7 +1204,6 @@ export class GraphQLInputObjectType {
13211204
astNode: ?InputObjectTypeDefinitionNode;
13221205
extensionASTNodes: ?$ReadOnlyArray<InputObjectTypeExtensionNode>;
13231206

1324-
_directives: ?$ReadOnlyArray<DirectiveNode>;
13251207
_fields: Thunk<GraphQLInputFieldMap>;
13261208

13271209
constructor(config: GraphQLInputObjectTypeConfig): void {
@@ -1333,28 +1215,6 @@ export class GraphQLInputObjectType {
13331215
invariant(typeof config.name === 'string', 'Must provide name.');
13341216
}
13351217

1336-
getDirectives(): $ReadOnlyArray<DirectiveNode> {
1337-
if (this._directives) {
1338-
return this._directives;
1339-
}
1340-
1341-
const directives = [];
1342-
if (this.astNode && this.astNode.directives) {
1343-
directives.push(...this.astNode.directives);
1344-
}
1345-
const extensionASTNodes = this.extensionASTNodes;
1346-
if (extensionASTNodes) {
1347-
for (let i = 0; i < extensionASTNodes.length; i++) {
1348-
const extensionNode = extensionASTNodes[i];
1349-
if (extensionNode.directives) {
1350-
directives.push(...extensionNode.directives);
1351-
}
1352-
}
1353-
}
1354-
this._directives = directives;
1355-
return directives;
1356-
}
1357-
13581218
getFields(): GraphQLInputFieldMap {
13591219
if (typeof this._fields === 'function') {
13601220
this._fields = this._fields();

src/type/validate.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919
import { DirectiveLocation } from '../language/directiveLocation';
2020
import type { DirectiveLocationEnum } from '../language/directiveLocation';
2121
import type {
22+
GraphQLNamedType,
2223
GraphQLEnumType,
2324
GraphQLInputObjectType,
2425
GraphQLInterfaceType,
@@ -78,13 +79,11 @@ export function validateSchema(
7879
validateDirectiveDefinitions(context);
7980

8081
// Validate directives that are used on the schema
81-
if (schema.astNode && schema.astNode.directives) {
82-
validateDirectivesAtLocation(
83-
context,
84-
schema.astNode.directives,
85-
DirectiveLocation.SCHEMA,
86-
);
87-
}
82+
validateDirectivesAtLocation(
83+
context,
84+
getDirectives(schema),
85+
DirectiveLocation.SCHEMA,
86+
);
8887

8988
validateTypes(context);
9089

@@ -284,7 +283,7 @@ function validateTypes(context: SchemaValidationContext): void {
284283
// Ensure directives are valid
285284
validateDirectivesAtLocation(
286285
context,
287-
type.getDirectives(),
286+
getDirectives(type),
288287
DirectiveLocation.OBJECT,
289288
);
290289
} else if (isInterfaceType(type)) {
@@ -294,7 +293,7 @@ function validateTypes(context: SchemaValidationContext): void {
294293
// Ensure directives are valid
295294
validateDirectivesAtLocation(
296295
context,
297-
type.getDirectives(),
296+
getDirectives(type),
298297
DirectiveLocation.INTERFACE,
299298
);
300299
} else if (isUnionType(type)) {
@@ -304,7 +303,7 @@ function validateTypes(context: SchemaValidationContext): void {
304303
// Ensure directives are valid
305304
validateDirectivesAtLocation(
306305
context,
307-
type.getDirectives(),
306+
getDirectives(type),
308307
DirectiveLocation.UNION,
309308
);
310309
} else if (isEnumType(type)) {
@@ -314,7 +313,7 @@ function validateTypes(context: SchemaValidationContext): void {
314313
// Ensure directives are valid
315314
validateDirectivesAtLocation(
316315
context,
317-
type.getDirectives(),
316+
getDirectives(type),
318317
DirectiveLocation.ENUM,
319318
);
320319
} else if (isInputObjectType(type)) {
@@ -324,14 +323,14 @@ function validateTypes(context: SchemaValidationContext): void {
324323
// Ensure directives are valid
325324
validateDirectivesAtLocation(
326325
context,
327-
type.getDirectives(),
326+
getDirectives(type),
328327
DirectiveLocation.INPUT_OBJECT,
329328
);
330329
} else if (isScalarType(type)) {
331330
// Ensure directives are valid
332331
validateDirectivesAtLocation(
333332
context,
334-
type.getDirectives(),
333+
getDirectives(type),
335334
DirectiveLocation.SCALAR,
336335
);
337336
}
@@ -735,6 +734,12 @@ function getAllSubNodes<T: ASTNode, K: ASTNode, L: ASTNode>(
735734
return result;
736735
}
737736

737+
function getDirectives(
738+
object: GraphQLSchema | GraphQLNamedType,
739+
): $ReadOnlyArray<DirectiveNode> {
740+
return getAllSubNodes(object, node => node.directives);
741+
}
742+
738743
function getImplementsInterfaceNode(
739744
type: GraphQLObjectType,
740745
iface: GraphQLInterfaceType,

0 commit comments

Comments
 (0)