Skip to content

Commit b3c3832

Browse files
committed
Add ability to getDirectives() on a type
1 parent 5abe152 commit b3c3832

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

src/type/definition.js

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

545+
_directives: ?$ReadOnlyArray<DirectiveNode>;
546+
544547
constructor(config: GraphQLScalarTypeConfig<*, *>): void {
545548
this.name = config.name;
546549
this.description = config.description;
@@ -566,6 +569,28 @@ export class GraphQLScalarType {
566569
}
567570
}
568571

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+
569594
toString(): string {
570595
return this.name;
571596
}
@@ -641,6 +666,7 @@ export class GraphQLObjectType {
641666

642667
_fields: Thunk<GraphQLFieldMap<*, *>>;
643668
_interfaces: Thunk<Array<GraphQLInterfaceType>>;
669+
_directives: ?$ReadOnlyArray<DirectiveNode>;
644670

645671
constructor(config: GraphQLObjectTypeConfig<*, *>): void {
646672
this.name = config.name;
@@ -659,6 +685,28 @@ export class GraphQLObjectType {
659685
}
660686
}
661687

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+
662710
getFields(): GraphQLFieldMap<*, *> {
663711
if (typeof this._fields === 'function') {
664712
this._fields = this._fields();
@@ -894,6 +942,7 @@ export class GraphQLInterfaceType {
894942
resolveType: ?GraphQLTypeResolver<*, *>;
895943

896944
_fields: Thunk<GraphQLFieldMap<*, *>>;
945+
_directives: ?$ReadOnlyArray<DirectiveNode>;
897946

898947
constructor(config: GraphQLInterfaceTypeConfig<*, *>): void {
899948
this.name = config.name;
@@ -911,6 +960,28 @@ export class GraphQLInterfaceType {
911960
}
912961
}
913962

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+
914985
getFields(): GraphQLFieldMap<*, *> {
915986
if (typeof this._fields === 'function') {
916987
this._fields = this._fields();
@@ -972,6 +1043,7 @@ export class GraphQLUnionType {
9721043
resolveType: ?GraphQLTypeResolver<*, *>;
9731044

9741045
_types: Thunk<Array<GraphQLObjectType>>;
1046+
_directives: ?$ReadOnlyArray<DirectiveNode>;
9751047

9761048
constructor(config: GraphQLUnionTypeConfig<*, *>): void {
9771049
this.name = config.name;
@@ -989,6 +1061,28 @@ export class GraphQLUnionType {
9891061
}
9901062
}
9911063

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+
9921086
getTypes(): Array<GraphQLObjectType> {
9931087
if (typeof this._types === 'function') {
9941088
this._types = this._types();
@@ -1058,6 +1152,7 @@ export class GraphQLEnumType /* <T> */ {
10581152
astNode: ?EnumTypeDefinitionNode;
10591153
extensionASTNodes: ?$ReadOnlyArray<EnumTypeExtensionNode>;
10601154

1155+
_directives: ?$ReadOnlyArray<DirectiveNode>;
10611156
_values: Array<GraphQLEnumValue /* <T> */>;
10621157
_valueLookup: Map<any /* T */, GraphQLEnumValue>;
10631158
_nameLookup: ObjMap<GraphQLEnumValue>;
@@ -1076,6 +1171,28 @@ export class GraphQLEnumType /* <T> */ {
10761171
invariant(typeof config.name === 'string', 'Must provide name.');
10771172
}
10781173

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+
10791196
getValues(): Array<GraphQLEnumValue /* <T> */> {
10801197
return this._values;
10811198
}
@@ -1204,6 +1321,7 @@ export class GraphQLInputObjectType {
12041321
astNode: ?InputObjectTypeDefinitionNode;
12051322
extensionASTNodes: ?$ReadOnlyArray<InputObjectTypeExtensionNode>;
12061323

1324+
_directives: ?$ReadOnlyArray<DirectiveNode>;
12071325
_fields: Thunk<GraphQLInputFieldMap>;
12081326

12091327
constructor(config: GraphQLInputObjectTypeConfig): void {
@@ -1215,6 +1333,28 @@ export class GraphQLInputObjectType {
12151333
invariant(typeof config.name === 'string', 'Must provide name.');
12161334
}
12171335

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+
12181358
getFields(): GraphQLInputFieldMap {
12191359
if (typeof this._fields === 'function') {
12201360
this._fields = this._fields();

0 commit comments

Comments
 (0)