Skip to content

Commit

Permalink
Eliminate circular dependency in validation rules (#1263)
Browse files Browse the repository at this point in the history
`validation/validate.js` depends on all of the rules, and all of the
rules depend on `ValidationContext`, which was previously exported by
`validation/validate.js`.

This moves `ValidationContext` out of `validate.js` to resolve this.

Circular dependencies are fine, but they tend to slow down flow, since
it moves more work to the merging phase, and the merging phase isn't as
fast as the inference phase. As a result, this should make flow
marginally faster.

This does have a minor breaking change: `graphql/validatation/validate`
no longer exports `ValidationContext`, but `graphql/validation` still
does. I can add an export to `validate.js` to preserve that behavior,
but it's a minor change to a non-public API, and graphql-js has broken
these things in the past.
  • Loading branch information
bgw authored and leebyron committed Feb 27, 2018
1 parent 499a759 commit 261b99b
Show file tree
Hide file tree
Showing 32 changed files with 257 additions and 238 deletions.
2 changes: 1 addition & 1 deletion src/utilities/isValidLiteralValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { visit, visitWithTypeInfo } from '../language/visitor';
import type { GraphQLInputType } from '../type/definition';
import { GraphQLSchema } from '../type/schema';
import { ValuesOfCorrectType } from '../validation/rules/ValuesOfCorrectType';
import { ValidationContext } from '../validation/validate';
import ValidationContext from '../validation/ValidationContext';

/**
* Utility which determines if a value literal node is valid for an input type.
Expand Down
221 changes: 221 additions & 0 deletions src/validation/ValidationContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/

import type { ObjMap } from '../jsutils/ObjMap';
import { GraphQLError } from '../error';
import { visit, visitWithTypeInfo } from '../language/visitor';
import { Kind } from '../language/kinds';
import type {
DocumentNode,
OperationDefinitionNode,
VariableNode,
SelectionSetNode,
FragmentSpreadNode,
FragmentDefinitionNode,
} from '../language/ast';
import { GraphQLSchema } from '../type/schema';
import type {
GraphQLInputType,
GraphQLOutputType,
GraphQLCompositeType,
GraphQLField,
GraphQLArgument,
} from '../type/definition';
import type { GraphQLDirective } from '../type/directives';
import { TypeInfo } from '../utilities/TypeInfo';

type NodeWithSelectionSet = OperationDefinitionNode | FragmentDefinitionNode;
type VariableUsage = { node: VariableNode, type: ?GraphQLInputType };

/**
* An instance of this class is passed as the "this" context to all validators,
* allowing access to commonly useful contextual information from within a
* validation rule.
*/
export default class ValidationContext {
_schema: GraphQLSchema;
_ast: DocumentNode;
_typeInfo: TypeInfo;
_errors: Array<GraphQLError>;
_fragments: ObjMap<FragmentDefinitionNode>;
_fragmentSpreads: Map<SelectionSetNode, $ReadOnlyArray<FragmentSpreadNode>>;
_recursivelyReferencedFragments: Map<
OperationDefinitionNode,
$ReadOnlyArray<FragmentDefinitionNode>,
>;
_variableUsages: Map<NodeWithSelectionSet, $ReadOnlyArray<VariableUsage>>;
_recursiveVariableUsages: Map<
OperationDefinitionNode,
$ReadOnlyArray<VariableUsage>,
>;

constructor(
schema: GraphQLSchema,
ast: DocumentNode,
typeInfo: TypeInfo,
): void {
this._schema = schema;
this._ast = ast;
this._typeInfo = typeInfo;
this._errors = [];
this._fragmentSpreads = new Map();
this._recursivelyReferencedFragments = new Map();
this._variableUsages = new Map();
this._recursiveVariableUsages = new Map();
}

reportError(error: GraphQLError): void {
this._errors.push(error);
}

getErrors(): $ReadOnlyArray<GraphQLError> {
return this._errors;
}

getSchema(): GraphQLSchema {
return this._schema;
}

getDocument(): DocumentNode {
return this._ast;
}

getFragment(name: string): ?FragmentDefinitionNode {
let fragments = this._fragments;
if (!fragments) {
this._fragments = fragments = this.getDocument().definitions.reduce(
(frags, statement) => {
if (statement.kind === Kind.FRAGMENT_DEFINITION) {
frags[statement.name.value] = statement;
}
return frags;
},
Object.create(null),
);
}
return fragments[name];
}

getFragmentSpreads(
node: SelectionSetNode,
): $ReadOnlyArray<FragmentSpreadNode> {
let spreads = this._fragmentSpreads.get(node);
if (!spreads) {
spreads = [];
const setsToVisit: Array<SelectionSetNode> = [node];
while (setsToVisit.length !== 0) {
const set = setsToVisit.pop();
for (let i = 0; i < set.selections.length; i++) {
const selection = set.selections[i];
if (selection.kind === Kind.FRAGMENT_SPREAD) {
spreads.push(selection);
} else if (selection.selectionSet) {
setsToVisit.push(selection.selectionSet);
}
}
}
this._fragmentSpreads.set(node, spreads);
}
return spreads;
}

getRecursivelyReferencedFragments(
operation: OperationDefinitionNode,
): $ReadOnlyArray<FragmentDefinitionNode> {
let fragments = this._recursivelyReferencedFragments.get(operation);
if (!fragments) {
fragments = [];
const collectedNames = Object.create(null);
const nodesToVisit: Array<SelectionSetNode> = [operation.selectionSet];
while (nodesToVisit.length !== 0) {
const node = nodesToVisit.pop();
const spreads = this.getFragmentSpreads(node);
for (let i = 0; i < spreads.length; i++) {
const fragName = spreads[i].name.value;
if (collectedNames[fragName] !== true) {
collectedNames[fragName] = true;
const fragment = this.getFragment(fragName);
if (fragment) {
fragments.push(fragment);
nodesToVisit.push(fragment.selectionSet);
}
}
}
}
this._recursivelyReferencedFragments.set(operation, fragments);
}
return fragments;
}

getVariableUsages(node: NodeWithSelectionSet): $ReadOnlyArray<VariableUsage> {
let usages = this._variableUsages.get(node);
if (!usages) {
const newUsages = [];
const typeInfo = new TypeInfo(this._schema);
visit(
node,
visitWithTypeInfo(typeInfo, {
VariableDefinition: () => false,
Variable(variable) {
newUsages.push({ node: variable, type: typeInfo.getInputType() });
},
}),
);
usages = newUsages;
this._variableUsages.set(node, usages);
}
return usages;
}

getRecursiveVariableUsages(
operation: OperationDefinitionNode,
): $ReadOnlyArray<VariableUsage> {
let usages = this._recursiveVariableUsages.get(operation);
if (!usages) {
usages = this.getVariableUsages(operation);
const fragments = this.getRecursivelyReferencedFragments(operation);
for (let i = 0; i < fragments.length; i++) {
Array.prototype.push.apply(
usages,
this.getVariableUsages(fragments[i]),
);
}
this._recursiveVariableUsages.set(operation, usages);
}
return usages;
}

getType(): ?GraphQLOutputType {
return this._typeInfo.getType();
}

getParentType(): ?GraphQLCompositeType {
return this._typeInfo.getParentType();
}

getInputType(): ?GraphQLInputType {
return this._typeInfo.getInputType();
}

getParentInputType(): ?GraphQLInputType {
return this._typeInfo.getParentInputType();
}

getFieldDef(): ?GraphQLField<*, *> {
return this._typeInfo.getFieldDef();
}

getDirective(): ?GraphQLDirective {
return this._typeInfo.getDirective();
}

getArgument(): ?GraphQLArgument {
return this._typeInfo.getArgument();
}
}
6 changes: 5 additions & 1 deletion src/validation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
* @flow strict
*/

export { validate, ValidationContext } from './validate';
export { validate } from './validate';

// https://github.com/tc39/proposal-export-default-from
import ValidationContext from './ValidationContext';
export { ValidationContext };

export { specifiedRules } from './specifiedRules';

Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/ExecutableDefinitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow strict
*/

import type { ValidationContext } from '../index';
import type ValidationContext from '../ValidationContext';
import { GraphQLError } from '../../error';
import { Kind } from '../../language/kinds';
import type { ASTVisitor } from '../../language/visitor';
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/FieldsOnCorrectType.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow strict
*/

import type { ValidationContext } from '../index';
import type ValidationContext from '../ValidationContext';
import { GraphQLError } from '../../error';
import suggestionList from '../../jsutils/suggestionList';
import quotedOrList from '../../jsutils/quotedOrList';
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/FragmentsOnCompositeTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow strict
*/

import type { ValidationContext } from '../index';
import type ValidationContext from '../ValidationContext';
import { GraphQLError } from '../../error';
import { print } from '../../language/printer';
import type { ASTVisitor } from '../../language/visitor';
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/KnownArgumentNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow strict
*/

import type { ValidationContext } from '../index';
import type ValidationContext from '../ValidationContext';
import { GraphQLError } from '../../error';
import type { ASTVisitor } from '../../language/visitor';
import suggestionList from '../../jsutils/suggestionList';
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/KnownDirectives.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow strict
*/

import type { ValidationContext } from '../index';
import type ValidationContext from '../ValidationContext';
import { GraphQLError } from '../../error';
import find from '../../jsutils/find';
import { Kind } from '../../language/kinds';
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/KnownFragmentNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow strict
*/

import type { ValidationContext } from '../index';
import type ValidationContext from '../ValidationContext';
import { GraphQLError } from '../../error';
import type { ASTVisitor } from '../../language/visitor';

Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/KnownTypeNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow strict
*/

import type { ValidationContext } from '../index';
import type ValidationContext from '../ValidationContext';
import { GraphQLError } from '../../error';
import suggestionList from '../../jsutils/suggestionList';
import quotedOrList from '../../jsutils/quotedOrList';
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/LoneAnonymousOperation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow strict
*/

import type { ValidationContext } from '../index';
import type ValidationContext from '../ValidationContext';
import { GraphQLError } from '../../error';
import { Kind } from '../../language/kinds';
import type { ASTVisitor } from '../../language/visitor';
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/NoFragmentCycles.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow strict
*/

import type { ValidationContext } from '../index';
import type ValidationContext from '../ValidationContext';
import { GraphQLError } from '../../error';
import type { FragmentDefinitionNode } from '../../language/ast';
import type { ASTVisitor } from '../../language/visitor';
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/NoUndefinedVariables.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow strict
*/

import type { ValidationContext } from '../index';
import type ValidationContext from '../ValidationContext';
import { GraphQLError } from '../../error';
import type { ASTVisitor } from '../../language/visitor';

Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/NoUnusedFragments.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow strict
*/

import type { ValidationContext } from '../index';
import type ValidationContext from '../ValidationContext';
import { GraphQLError } from '../../error';
import type { ASTVisitor } from '../../language/visitor';

Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/NoUnusedVariables.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow strict
*/

import type { ValidationContext } from '../index';
import type ValidationContext from '../ValidationContext';
import { GraphQLError } from '../../error';
import type { ASTVisitor } from '../../language/visitor';

Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/OverlappingFieldsCanBeMerged.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow strict
*/

import type { ValidationContext } from '../index';
import type ValidationContext from '../ValidationContext';
import { GraphQLError } from '../../error';
import find from '../../jsutils/find';
import type { ObjMap } from '../../jsutils/ObjMap';
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/PossibleFragmentSpreads.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow strict
*/

import type { ValidationContext } from '../index';
import type ValidationContext from '../ValidationContext';
import { GraphQLError } from '../../error';
import type { ASTVisitor } from '../../language/visitor';
import { doTypesOverlap } from '../../utilities/typeComparators';
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/ProvidedNonNullArguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow strict
*/

import type { ValidationContext } from '../index';
import type ValidationContext from '../ValidationContext';
import { GraphQLError } from '../../error';
import keyMap from '../../jsutils/keyMap';
import { isNonNullType } from '../../type/definition';
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/ScalarLeafs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow strict
*/

import type { ValidationContext } from '../index';
import type ValidationContext from '../ValidationContext';
import { GraphQLError } from '../../error';
import type { FieldNode } from '../../language/ast';
import { getNamedType, isLeafType } from '../../type/definition';
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/SingleFieldSubscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow strict
*/

import type { ValidationContext } from '../index';
import type ValidationContext from '../ValidationContext';
import { GraphQLError } from '../../error';
import type { OperationDefinitionNode } from '../../language/ast';
import type { ASTVisitor } from '../../language/visitor';
Expand Down
Loading

0 comments on commit 261b99b

Please sign in to comment.