Skip to content

Replace '.getDirectives' with generic method #1436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 0 additions & 140 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import type {
OperationDefinitionNode,
FieldNode,
FragmentDefinitionNode,
DirectiveNode,
ValueNode,
} from '../language/ast';
import type { GraphQLSchema } from './schema';
Expand Down Expand Up @@ -542,8 +541,6 @@ export class GraphQLScalarType {
astNode: ?ScalarTypeDefinitionNode;
extensionASTNodes: ?$ReadOnlyArray<ScalarTypeExtensionNode>;

_directives: ?$ReadOnlyArray<DirectiveNode>;

constructor(config: GraphQLScalarTypeConfig<*, *>): void {
this.name = config.name;
this.description = config.description;
Expand All @@ -569,28 +566,6 @@ export class GraphQLScalarType {
}
}

getDirectives(): $ReadOnlyArray<DirectiveNode> {
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;
}
Expand Down Expand Up @@ -666,7 +641,6 @@ export class GraphQLObjectType {

_fields: Thunk<GraphQLFieldMap<*, *>>;
_interfaces: Thunk<Array<GraphQLInterfaceType>>;
_directives: ?$ReadOnlyArray<DirectiveNode>;

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

getDirectives(): $ReadOnlyArray<DirectiveNode> {
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();
Expand Down Expand Up @@ -942,7 +894,6 @@ export class GraphQLInterfaceType {
resolveType: ?GraphQLTypeResolver<*, *>;

_fields: Thunk<GraphQLFieldMap<*, *>>;
_directives: ?$ReadOnlyArray<DirectiveNode>;

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

getDirectives(): $ReadOnlyArray<DirectiveNode> {
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();
Expand Down Expand Up @@ -1043,7 +972,6 @@ export class GraphQLUnionType {
resolveType: ?GraphQLTypeResolver<*, *>;

_types: Thunk<Array<GraphQLObjectType>>;
_directives: ?$ReadOnlyArray<DirectiveNode>;

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

getDirectives(): $ReadOnlyArray<DirectiveNode> {
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<GraphQLObjectType> {
if (typeof this._types === 'function') {
this._types = this._types();
Expand Down Expand Up @@ -1152,7 +1058,6 @@ export class GraphQLEnumType /* <T> */ {
astNode: ?EnumTypeDefinitionNode;
extensionASTNodes: ?$ReadOnlyArray<EnumTypeExtensionNode>;

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

getDirectives(): $ReadOnlyArray<DirectiveNode> {
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<GraphQLEnumValue /* <T> */> {
return this._values;
}
Expand Down Expand Up @@ -1321,7 +1204,6 @@ export class GraphQLInputObjectType {
astNode: ?InputObjectTypeDefinitionNode;
extensionASTNodes: ?$ReadOnlyArray<InputObjectTypeExtensionNode>;

_directives: ?$ReadOnlyArray<DirectiveNode>;
_fields: Thunk<GraphQLInputFieldMap>;

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

getDirectives(): $ReadOnlyArray<DirectiveNode> {
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();
Expand Down
31 changes: 18 additions & 13 deletions src/type/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
import { DirectiveLocation } from '../language/directiveLocation';
import type { DirectiveLocationEnum } from '../language/directiveLocation';
import type {
GraphQLNamedType,
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLInterfaceType,
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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,
);
}
Expand Down Expand Up @@ -735,6 +734,12 @@ function getAllSubNodes<T: ASTNode, K: ASTNode, L: ASTNode>(
return result;
}

function getDirectives(
object: GraphQLSchema | GraphQLNamedType,
): $ReadOnlyArray<DirectiveNode> {
return getAllSubNodes(object, node => node.directives);
}

function getImplementsInterfaceNode(
type: GraphQLObjectType,
iface: GraphQLInterfaceType,
Expand Down