From 57619c91bcd72e047f047241f2eb393da442f410 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Fri, 27 Jul 2018 06:50:15 +0300 Subject: [PATCH] Replace '.getDirectives' with generic method --- src/type/definition.js | 140 ----------------------------------------- src/type/validate.js | 31 +++++---- 2 files changed, 18 insertions(+), 153 deletions(-) diff --git a/src/type/definition.js b/src/type/definition.js index 376e899a80..d588a565d1 100644 --- a/src/type/definition.js +++ b/src/type/definition.js @@ -35,7 +35,6 @@ import type { OperationDefinitionNode, FieldNode, FragmentDefinitionNode, - DirectiveNode, ValueNode, } from '../language/ast'; import type { GraphQLSchema } from './schema'; @@ -542,8 +541,6 @@ export class GraphQLScalarType { astNode: ?ScalarTypeDefinitionNode; extensionASTNodes: ?$ReadOnlyArray; - _directives: ?$ReadOnlyArray; - constructor(config: GraphQLScalarTypeConfig<*, *>): void { this.name = config.name; this.description = config.description; @@ -569,28 +566,6 @@ export class GraphQLScalarType { } } - getDirectives(): $ReadOnlyArray { - if (this._directives) { - return this._directives; - } - - const directives = []; - if (this.astNode && this.astNode.directives) { - directives.push(...this.astNode.directives); - } - const extensionASTNodes = this.extensionASTNodes; - if (extensionASTNodes) { - for (let i = 0; i < extensionASTNodes.length; i++) { - const extensionNode = extensionASTNodes[i]; - if (extensionNode.directives) { - directives.push(...extensionNode.directives); - } - } - } - this._directives = directives; - return directives; - } - toString(): string { return this.name; } @@ -666,7 +641,6 @@ export class GraphQLObjectType { _fields: Thunk>; _interfaces: Thunk>; - _directives: ?$ReadOnlyArray; constructor(config: GraphQLObjectTypeConfig<*, *>): void { this.name = config.name; @@ -685,28 +659,6 @@ export class GraphQLObjectType { } } - getDirectives(): $ReadOnlyArray { - if (this._directives) { - return this._directives; - } - - const directives = []; - if (this.astNode && this.astNode.directives) { - directives.push(...this.astNode.directives); - } - const extensionASTNodes = this.extensionASTNodes; - if (extensionASTNodes) { - for (let i = 0; i < extensionASTNodes.length; i++) { - const extensionNode = extensionASTNodes[i]; - if (extensionNode.directives) { - directives.push(...extensionNode.directives); - } - } - } - this._directives = directives; - return directives; - } - getFields(): GraphQLFieldMap<*, *> { if (typeof this._fields === 'function') { this._fields = this._fields(); @@ -942,7 +894,6 @@ export class GraphQLInterfaceType { resolveType: ?GraphQLTypeResolver<*, *>; _fields: Thunk>; - _directives: ?$ReadOnlyArray; constructor(config: GraphQLInterfaceTypeConfig<*, *>): void { this.name = config.name; @@ -960,28 +911,6 @@ export class GraphQLInterfaceType { } } - getDirectives(): $ReadOnlyArray { - if (this._directives) { - return this._directives; - } - - const directives = []; - if (this.astNode && this.astNode.directives) { - directives.push(...this.astNode.directives); - } - const extensionASTNodes = this.extensionASTNodes; - if (extensionASTNodes) { - for (let i = 0; i < extensionASTNodes.length; i++) { - const extensionNode = extensionASTNodes[i]; - if (extensionNode.directives) { - directives.push(...extensionNode.directives); - } - } - } - this._directives = directives; - return directives; - } - getFields(): GraphQLFieldMap<*, *> { if (typeof this._fields === 'function') { this._fields = this._fields(); @@ -1043,7 +972,6 @@ export class GraphQLUnionType { resolveType: ?GraphQLTypeResolver<*, *>; _types: Thunk>; - _directives: ?$ReadOnlyArray; constructor(config: GraphQLUnionTypeConfig<*, *>): void { this.name = config.name; @@ -1061,28 +989,6 @@ export class GraphQLUnionType { } } - getDirectives(): $ReadOnlyArray { - if (this._directives) { - return this._directives; - } - - const directives = []; - if (this.astNode && this.astNode.directives) { - directives.push(...this.astNode.directives); - } - const extensionASTNodes = this.extensionASTNodes; - if (extensionASTNodes) { - for (let i = 0; i < extensionASTNodes.length; i++) { - const extensionNode = extensionASTNodes[i]; - if (extensionNode.directives) { - directives.push(...extensionNode.directives); - } - } - } - this._directives = directives; - return directives; - } - getTypes(): Array { if (typeof this._types === 'function') { this._types = this._types(); @@ -1152,7 +1058,6 @@ export class GraphQLEnumType /* */ { astNode: ?EnumTypeDefinitionNode; extensionASTNodes: ?$ReadOnlyArray; - _directives: ?$ReadOnlyArray; _values: Array */>; _valueLookup: Map; _nameLookup: ObjMap; @@ -1171,28 +1076,6 @@ export class GraphQLEnumType /* */ { invariant(typeof config.name === 'string', 'Must provide name.'); } - getDirectives(): $ReadOnlyArray { - if (this._directives) { - return this._directives; - } - - const directives = []; - if (this.astNode && this.astNode.directives) { - directives.push(...this.astNode.directives); - } - const extensionASTNodes = this.extensionASTNodes; - if (extensionASTNodes) { - for (let i = 0; i < extensionASTNodes.length; i++) { - const extensionNode = extensionASTNodes[i]; - if (extensionNode.directives) { - directives.push(...extensionNode.directives); - } - } - } - this._directives = directives; - return directives; - } - getValues(): Array */> { return this._values; } @@ -1321,7 +1204,6 @@ export class GraphQLInputObjectType { astNode: ?InputObjectTypeDefinitionNode; extensionASTNodes: ?$ReadOnlyArray; - _directives: ?$ReadOnlyArray; _fields: Thunk; constructor(config: GraphQLInputObjectTypeConfig): void { @@ -1333,28 +1215,6 @@ export class GraphQLInputObjectType { invariant(typeof config.name === 'string', 'Must provide name.'); } - getDirectives(): $ReadOnlyArray { - if (this._directives) { - return this._directives; - } - - const directives = []; - if (this.astNode && this.astNode.directives) { - directives.push(...this.astNode.directives); - } - const extensionASTNodes = this.extensionASTNodes; - if (extensionASTNodes) { - for (let i = 0; i < extensionASTNodes.length; i++) { - const extensionNode = extensionASTNodes[i]; - if (extensionNode.directives) { - directives.push(...extensionNode.directives); - } - } - } - this._directives = directives; - return directives; - } - getFields(): GraphQLInputFieldMap { if (typeof this._fields === 'function') { this._fields = this._fields(); diff --git a/src/type/validate.js b/src/type/validate.js index c5161fc8eb..5884e3cabc 100644 --- a/src/type/validate.js +++ b/src/type/validate.js @@ -19,6 +19,7 @@ import type { import { DirectiveLocation } from '../language/directiveLocation'; import type { DirectiveLocationEnum } from '../language/directiveLocation'; import type { + GraphQLNamedType, GraphQLEnumType, GraphQLInputObjectType, GraphQLInterfaceType, @@ -78,13 +79,11 @@ export function validateSchema( validateDirectiveDefinitions(context); // Validate directives that are used on the schema - if (schema.astNode && schema.astNode.directives) { - validateDirectivesAtLocation( - context, - schema.astNode.directives, - DirectiveLocation.SCHEMA, - ); - } + validateDirectivesAtLocation( + context, + getDirectives(schema), + DirectiveLocation.SCHEMA, + ); validateTypes(context); @@ -284,7 +283,7 @@ function validateTypes(context: SchemaValidationContext): void { // Ensure directives are valid validateDirectivesAtLocation( context, - type.getDirectives(), + getDirectives(type), DirectiveLocation.OBJECT, ); } else if (isInterfaceType(type)) { @@ -294,7 +293,7 @@ function validateTypes(context: SchemaValidationContext): void { // Ensure directives are valid validateDirectivesAtLocation( context, - type.getDirectives(), + getDirectives(type), DirectiveLocation.INTERFACE, ); } else if (isUnionType(type)) { @@ -304,7 +303,7 @@ function validateTypes(context: SchemaValidationContext): void { // Ensure directives are valid validateDirectivesAtLocation( context, - type.getDirectives(), + getDirectives(type), DirectiveLocation.UNION, ); } else if (isEnumType(type)) { @@ -314,7 +313,7 @@ function validateTypes(context: SchemaValidationContext): void { // Ensure directives are valid validateDirectivesAtLocation( context, - type.getDirectives(), + getDirectives(type), DirectiveLocation.ENUM, ); } else if (isInputObjectType(type)) { @@ -324,14 +323,14 @@ function validateTypes(context: SchemaValidationContext): void { // Ensure directives are valid validateDirectivesAtLocation( context, - type.getDirectives(), + getDirectives(type), DirectiveLocation.INPUT_OBJECT, ); } else if (isScalarType(type)) { // Ensure directives are valid validateDirectivesAtLocation( context, - type.getDirectives(), + getDirectives(type), DirectiveLocation.SCALAR, ); } @@ -735,6 +734,12 @@ function getAllSubNodes( return result; } +function getDirectives( + object: GraphQLSchema | GraphQLNamedType, +): $ReadOnlyArray { + return getAllSubNodes(object, node => node.directives); +} + function getImplementsInterfaceNode( type: GraphQLObjectType, iface: GraphQLInterfaceType,