From e49df4980396ed40d9b43bad738c53e001d8f0e5 Mon Sep 17 00:00:00 2001 From: Lee Byron Date: Wed, 2 Nov 2016 21:56:11 -0700 Subject: [PATCH] Rename AST nodes to `*Node` to disambiguate types. --- src/error/GraphQLError.js | 4 +- src/error/__tests__/GraphQLError-test.js | 12 +- src/execution/__tests__/executor-test.js | 6 +- src/execution/execute.js | 151 ++++---- src/execution/values.js | 56 +-- src/index.js | 94 ++--- src/language/ast.js | 366 +++++++++--------- src/language/index.js | 92 ++--- src/language/parser.js | 196 +++++----- src/type/definition.js | 26 +- src/type/index.js | 1 - src/utilities/TypeInfo.js | 11 +- src/utilities/astFromValue.js | 50 +-- src/utilities/buildASTSchema.js | 139 +++---- src/utilities/concatAST.js | 4 +- src/utilities/extendSchema.js | 173 +++++---- src/utilities/getOperationAST.js | 6 +- src/utilities/isValidLiteralValue.js | 46 ++- src/utilities/separateOperations.js | 10 +- src/utilities/typeFromAST.js | 16 +- src/utilities/valueFromAST.js | 58 +-- .../rules/ArgumentsOfCorrectType.js | 10 +- .../rules/DefaultValuesOfCorrectType.js | 6 +- src/validation/rules/FieldsOnCorrectType.js | 4 +- src/validation/rules/NoFragmentCycles.js | 4 +- src/validation/rules/NoUndefinedVariables.js | 4 +- .../rules/OverlappingFieldsCanBeMerged.js | 102 ++--- .../rules/ProvidedNonNullArguments.js | 28 +- src/validation/rules/ScalarLeafs.js | 4 +- src/validation/rules/UniqueVariableNames.js | 4 +- .../rules/VariablesAreInputTypes.js | 4 +- .../rules/VariablesInAllowedPosition.js | 4 +- src/validation/validate.js | 54 +-- 33 files changed, 880 insertions(+), 865 deletions(-) diff --git a/src/error/GraphQLError.js b/src/error/GraphQLError.js index b81b31e67f..b5f250cdff 100644 --- a/src/error/GraphQLError.js +++ b/src/error/GraphQLError.js @@ -9,7 +9,7 @@ */ import { getLocation } from '../language'; -import type { Node } from '../language/ast'; +import type { ASTNode } from '../language/ast'; import type { Source } from '../language/source'; /** @@ -50,7 +50,7 @@ declare class GraphQLError extends Error { /** * An array of GraphQL AST Nodes corresponding to this error. */ - nodes: Array | void; + nodes: Array | void; /** * The source GraphQL document corresponding to this error. diff --git a/src/error/__tests__/GraphQLError-test.js b/src/error/__tests__/GraphQLError-test.js index 7b22492242..4ccd166fee 100644 --- a/src/error/__tests__/GraphQLError-test.js +++ b/src/error/__tests__/GraphQLError-test.js @@ -64,9 +64,9 @@ describe('GraphQLError', () => { field }`); const ast = parse(source); - const fieldAST = ast.definitions[0].selectionSet.selections[0]; - const e = new GraphQLError('msg', [ fieldAST ]); - expect(e.nodes).to.deep.equal([ fieldAST ]); + const fieldNode = ast.definitions[0].selectionSet.selections[0]; + const e = new GraphQLError('msg', [ fieldNode ]); + expect(e.nodes).to.deep.equal([ fieldNode ]); expect(e.source).to.equal(source); expect(e.positions).to.deep.equal([ 8 ]); expect(e.locations).to.deep.equal([ { line: 2, column: 7 } ]); @@ -77,9 +77,9 @@ describe('GraphQLError', () => { field }`); const ast = parse(source); - const operationAST = ast.definitions[0]; - const e = new GraphQLError('msg', [ operationAST ]); - expect(e.nodes).to.deep.equal([ operationAST ]); + const operationNode = ast.definitions[0]; + const e = new GraphQLError('msg', [ operationNode ]); + expect(e.nodes).to.deep.equal([ operationNode ]); expect(e.source).to.equal(source); expect(e.positions).to.deep.equal([ 0 ]); expect(e.locations).to.deep.equal([ { line: 1, column: 1 } ]); diff --git a/src/execution/__tests__/executor-test.js b/src/execution/__tests__/executor-test.js index 017cb333b1..797a4f3e8b 100644 --- a/src/execution/__tests__/executor-test.js +++ b/src/execution/__tests__/executor-test.js @@ -208,7 +208,7 @@ describe('Execute: Handles basic execution tasks', () => { expect(Object.keys(info)).to.deep.equal([ 'fieldName', - 'fieldASTs', + 'fieldNodes', 'returnType', 'parentType', 'path', @@ -219,8 +219,8 @@ describe('Execute: Handles basic execution tasks', () => { 'variableValues', ]); expect(info.fieldName).to.equal('test'); - expect(info.fieldASTs).to.have.lengthOf(1); - expect(info.fieldASTs[0]).to.equal( + expect(info.fieldNodes).to.have.lengthOf(1); + expect(info.fieldNodes[0]).to.equal( ast.definitions[0].selectionSet.selections[0] ); expect(info.returnType).to.equal(GraphQLString); diff --git a/src/execution/execute.js b/src/execution/execute.js index 36d2db9eca..48bcf5dcad 100644 --- a/src/execution/execute.js +++ b/src/execution/execute.js @@ -39,20 +39,20 @@ import { GraphQLSchema } from '../type/schema'; import { SchemaMetaFieldDef, TypeMetaFieldDef, - TypeNameMetaFieldDef + TypeNameMetaFieldDef, } from '../type/introspection'; import { GraphQLIncludeDirective, - GraphQLSkipDirective + GraphQLSkipDirective, } from '../type/directives'; import type { - Directive, - Document, - OperationDefinition, - SelectionSet, - Field, - InlineFragment, - FragmentDefinition + DirectiveNode, + DocumentNode, + OperationDefinitionNode, + SelectionSetNode, + FieldNode, + InlineFragmentNode, + FragmentDefinitionNode, } from '../language/ast'; @@ -84,10 +84,10 @@ import type { */ type ExecutionContext = { schema: GraphQLSchema; - fragments: {[key: string]: FragmentDefinition}; + fragments: {[key: string]: FragmentDefinitionNode}; rootValue: mixed; contextValue: mixed; - operation: OperationDefinition; + operation: OperationDefinitionNode; variableValues: {[key: string]: mixed}; errors: Array; }; @@ -113,7 +113,7 @@ export type ExecutionResult = { */ export function execute( schema: GraphQLSchema, - documentAST: Document, + document: DocumentNode, rootValue?: mixed, contextValue?: mixed, variableValues?: ?{[key: string]: mixed}, @@ -138,7 +138,7 @@ export function execute( // this will throw an error. const context = buildExecutionContext( schema, - documentAST, + document, rootValue, contextValue, variableValues, @@ -176,16 +176,17 @@ export function execute( */ function buildExecutionContext( schema: GraphQLSchema, - documentAST: Document, + document: DocumentNode, rootValue: mixed, contextValue: mixed, rawVariableValues: ?{[key: string]: mixed}, operationName: ?string ): ExecutionContext { const errors: Array = []; - let operation: ?OperationDefinition; - const fragments: {[name: string]: FragmentDefinition} = Object.create(null); - documentAST.definitions.forEach(definition => { + let operation: ?OperationDefinitionNode; + const fragments: {[name: string]: FragmentDefinitionNode} = + Object.create(null); + document.definitions.forEach(definition => { switch (definition.kind) { case Kind.OPERATION_DEFINITION: if (!operationName && operation) { @@ -236,7 +237,7 @@ function buildExecutionContext( */ function executeOperation( exeContext: ExecutionContext, - operation: OperationDefinition, + operation: OperationDefinitionNode, rootValue: mixed ): {[key: string]: mixed} { const type = getOperationRootType(exeContext.schema, operation); @@ -261,7 +262,7 @@ function executeOperation( */ function getOperationRootType( schema: GraphQLSchema, - operation: OperationDefinition + operation: OperationDefinitionNode ): GraphQLObjectType { switch (operation.operation) { case 'query': @@ -301,17 +302,17 @@ function executeFieldsSerially( parentType: GraphQLObjectType, sourceValue: mixed, path: Array, - fields: {[key: string]: Array} + fields: {[key: string]: Array} ): Promise<{[key: string]: mixed}> { return Object.keys(fields).reduce( (prevPromise, responseName) => prevPromise.then(results => { - const fieldASTs = fields[responseName]; + const fieldNodes = fields[responseName]; const fieldPath = path.concat([ responseName ]); const result = resolveField( exeContext, parentType, sourceValue, - fieldASTs, + fieldNodes, fieldPath ); if (result === undefined) { @@ -339,19 +340,19 @@ function executeFields( parentType: GraphQLObjectType, sourceValue: mixed, path: Array, - fields: {[key: string]: Array} + fields: {[key: string]: Array} ): {[key: string]: mixed} { let containsPromise = false; const finalResults = Object.keys(fields).reduce( (results, responseName) => { - const fieldASTs = fields[responseName]; + const fieldNodes = fields[responseName]; const fieldPath = path.concat([ responseName ]); const result = resolveField( exeContext, parentType, sourceValue, - fieldASTs, + fieldNodes, fieldPath ); if (result === undefined) { @@ -389,10 +390,10 @@ function executeFields( function collectFields( exeContext: ExecutionContext, runtimeType: GraphQLObjectType, - selectionSet: SelectionSet, - fields: {[key: string]: Array}, + selectionSet: SelectionSetNode, + fields: {[key: string]: Array}, visitedFragmentNames: {[key: string]: boolean} -): {[key: string]: Array} { +): {[key: string]: Array} { for (let i = 0; i < selectionSet.selections.length; i++) { const selection = selectionSet.selections[i]; switch (selection.kind) { @@ -450,16 +451,16 @@ function collectFields( */ function shouldIncludeNode( exeContext: ExecutionContext, - directives: ?Array + directives: ?Array ): boolean { - const skipAST = directives && find( + const skipNode = directives && find( directives, directive => directive.name.value === GraphQLSkipDirective.name ); - if (skipAST) { + if (skipNode) { const { if: skipIf } = getArgumentValues( GraphQLSkipDirective, - skipAST, + skipNode, exeContext.variableValues ); if (skipIf === true) { @@ -467,14 +468,14 @@ function shouldIncludeNode( } } - const includeAST = directives && find( + const includeNode = directives && find( directives, directive => directive.name.value === GraphQLIncludeDirective.name ); - if (includeAST) { + if (includeNode) { const { if: includeIf } = getArgumentValues( GraphQLIncludeDirective, - includeAST, + includeNode, exeContext.variableValues ); if (includeIf === false) { @@ -490,14 +491,14 @@ function shouldIncludeNode( */ function doesFragmentConditionMatch( exeContext: ExecutionContext, - fragment: FragmentDefinition | InlineFragment, + fragment: FragmentDefinitionNode | InlineFragmentNode, type: GraphQLObjectType ): boolean { - const typeConditionAST = fragment.typeCondition; - if (!typeConditionAST) { + const typeConditionNode = fragment.typeCondition; + if (!typeConditionNode) { return true; } - const conditionalType = typeFromAST(exeContext.schema, typeConditionAST); + const conditionalType = typeFromAST(exeContext.schema, typeConditionNode); if (conditionalType === type) { return true; } @@ -531,7 +532,7 @@ function promiseForObject( /** * Implements the logic to compute the key of a given field's entry */ -function getFieldEntryKey(node: Field): string { +function getFieldEntryKey(node: FieldNode): string { return node.alias ? node.alias.value : node.name.value; } @@ -545,11 +546,11 @@ function resolveField( exeContext: ExecutionContext, parentType: GraphQLObjectType, source: mixed, - fieldASTs: Array, + fieldNodes: Array, path: Array ): mixed { - const fieldAST = fieldASTs[0]; - const fieldName = fieldAST.name.value; + const fieldNode = fieldNodes[0]; + const fieldName = fieldNode.name.value; const fieldDef = getFieldDef(exeContext.schema, parentType, fieldName); if (!fieldDef) { @@ -568,7 +569,7 @@ function resolveField( // information about the current execution state. const info: GraphQLResolveInfo = { fieldName, - fieldASTs, + fieldNodes, returnType, parentType, path, @@ -584,7 +585,7 @@ function resolveField( const result = resolveOrError( exeContext, fieldDef, - fieldAST, + fieldNode, resolveFn, source, context, @@ -594,7 +595,7 @@ function resolveField( return completeValueCatchingError( exeContext, returnType, - fieldASTs, + fieldNodes, info, path, result @@ -606,7 +607,7 @@ function resolveField( function resolveOrError( exeContext: ExecutionContext, fieldDef: GraphQLField, - fieldAST: Field, + fieldNode: FieldNode, resolveFn: GraphQLFieldResolveFn<*>, source: mixed, context: mixed, @@ -618,7 +619,7 @@ function resolveOrError( // TODO: find a way to memoize, in case this field is within a List type. const args = getArgumentValues( fieldDef, - fieldAST, + fieldNode, exeContext.variableValues ); @@ -635,7 +636,7 @@ function resolveOrError( function completeValueCatchingError( exeContext: ExecutionContext, returnType: GraphQLType, - fieldASTs: Array, + fieldNodes: Array, info: GraphQLResolveInfo, path: Array, result: mixed @@ -646,7 +647,7 @@ function completeValueCatchingError( return completeValueWithLocatedError( exeContext, returnType, - fieldASTs, + fieldNodes, info, path, result @@ -659,7 +660,7 @@ function completeValueCatchingError( const completed = completeValueWithLocatedError( exeContext, returnType, - fieldASTs, + fieldNodes, info, path, result @@ -688,7 +689,7 @@ function completeValueCatchingError( function completeValueWithLocatedError( exeContext: ExecutionContext, returnType: GraphQLType, - fieldASTs: Array, + fieldNodes: Array, info: GraphQLResolveInfo, path: Array, result: mixed @@ -697,7 +698,7 @@ function completeValueWithLocatedError( const completed = completeValue( exeContext, returnType, - fieldASTs, + fieldNodes, info, path, result @@ -705,12 +706,12 @@ function completeValueWithLocatedError( if (isThenable(completed)) { return ((completed: any): Promise<*>).then( undefined, - error => Promise.reject(locatedError(error, fieldASTs, path)) + error => Promise.reject(locatedError(error, fieldNodes, path)) ); } return completed; } catch (error) { - throw locatedError(error, fieldASTs, path); + throw locatedError(error, fieldNodes, path); } } @@ -738,7 +739,7 @@ function completeValueWithLocatedError( function completeValue( exeContext: ExecutionContext, returnType: GraphQLType, - fieldASTs: Array, + fieldNodes: Array, info: GraphQLResolveInfo, path: Array, result: mixed @@ -749,7 +750,7 @@ function completeValue( resolved => completeValue( exeContext, returnType, - fieldASTs, + fieldNodes, info, path, resolved @@ -768,7 +769,7 @@ function completeValue( const completed = completeValue( exeContext, returnType.ofType, - fieldASTs, + fieldNodes, info, path, result @@ -792,7 +793,7 @@ function completeValue( return completeListValue( exeContext, returnType, - fieldASTs, + fieldNodes, info, path, result @@ -813,7 +814,7 @@ function completeValue( return completeAbstractValue( exeContext, returnType, - fieldASTs, + fieldNodes, info, path, result @@ -825,7 +826,7 @@ function completeValue( return completeObjectValue( exeContext, returnType, - fieldASTs, + fieldNodes, info, path, result @@ -845,7 +846,7 @@ function completeValue( function completeListValue( exeContext: ExecutionContext, returnType: GraphQLList<*>, - fieldASTs: Array, + fieldNodes: Array, info: GraphQLResolveInfo, path: Array, result: mixed @@ -868,7 +869,7 @@ function completeListValue( const completedItem = completeValueCatchingError( exeContext, itemType, - fieldASTs, + fieldNodes, info, fieldPath, item @@ -909,7 +910,7 @@ function completeLeafValue( function completeAbstractValue( exeContext: ExecutionContext, returnType: GraphQLAbstractType, - fieldASTs: Array, + fieldNodes: Array, info: GraphQLResolveInfo, path: Array, result: mixed @@ -928,7 +929,7 @@ function completeAbstractValue( `Abstract type ${returnType.name} must resolve to an Object type at ` + `runtime for field ${info.parentType.name}.${info.fieldName} with ` + `value "${String(result)}", received "${String(runtimeType)}".`, - fieldASTs + fieldNodes ); } @@ -936,14 +937,14 @@ function completeAbstractValue( throw new GraphQLError( `Runtime Object type "${runtimeType.name}" is not a possible type ` + `for "${returnType.name}".`, - fieldASTs + fieldNodes ); } return completeObjectValue( exeContext, runtimeType, - fieldASTs, + fieldNodes, info, path, result @@ -956,7 +957,7 @@ function completeAbstractValue( function completeObjectValue( exeContext: ExecutionContext, returnType: GraphQLObjectType, - fieldASTs: Array, + fieldNodes: Array, info: GraphQLResolveInfo, path: Array, result: mixed @@ -968,27 +969,27 @@ function completeObjectValue( !returnType.isTypeOf(result, exeContext.contextValue, info)) { throw new GraphQLError( `Expected value of type "${returnType.name}" but got: ${String(result)}.`, - fieldASTs + fieldNodes ); } // Collect sub-fields to execute to complete this value. - let subFieldASTs = Object.create(null); + let subFieldNodes = Object.create(null); const visitedFragmentNames = Object.create(null); - for (let i = 0; i < fieldASTs.length; i++) { - const selectionSet = fieldASTs[i].selectionSet; + for (let i = 0; i < fieldNodes.length; i++) { + const selectionSet = fieldNodes[i].selectionSet; if (selectionSet) { - subFieldASTs = collectFields( + subFieldNodes = collectFields( exeContext, returnType, selectionSet, - subFieldASTs, + subFieldNodes, visitedFragmentNames ); } } - return executeFields(exeContext, returnType, result, path, subFieldASTs); + return executeFields(exeContext, returnType, result, path, subFieldNodes); } /** diff --git a/src/execution/values.js b/src/execution/values.js index e4c29471f5..90d2484312 100644 --- a/src/execution/values.js +++ b/src/execution/values.js @@ -36,10 +36,10 @@ import type { import type { GraphQLDirective } from '../type/directives'; import type { GraphQLSchema } from '../type/schema'; import type { - Field, - Directive, - Variable, - VariableDefinition, + FieldNode, + DirectiveNode, + VariableNode, + VariableDefinitionNode, } from '../language/ast'; @@ -50,26 +50,26 @@ import type { */ export function getVariableValues( schema: GraphQLSchema, - definitionASTs: Array, + varDefNodes: Array, inputs: { [key: string]: mixed } ): { [key: string]: mixed } { const coercedValues = Object.create(null); - for (let i = 0; i < definitionASTs.length; i++) { - const definitionAST = definitionASTs[i]; - const varName = definitionAST.variable.name.value; - let varType = typeFromAST(schema, definitionAST.type); + for (let i = 0; i < varDefNodes.length; i++) { + const varDefNode = varDefNodes[i]; + const varName = varDefNode.variable.name.value; + let varType = typeFromAST(schema, varDefNode.type); if (!isInputType(varType)) { throw new GraphQLError( `Variable "$${varName}" expected value of type ` + - `"${print(definitionAST.type)}" which cannot be used as an input type.`, - [ definitionAST.type ] + `"${print(varDefNode.type)}" which cannot be used as an input type.`, + [ varDefNode.type ] ); } varType = ((varType: any): GraphQLInputType); const value = inputs[varName]; if (isInvalid(value)) { - const defaultValue = definitionAST.defaultValue; + const defaultValue = varDefNode.defaultValue; if (defaultValue) { coercedValues[varName] = valueFromAST(defaultValue, varType); } @@ -77,7 +77,7 @@ export function getVariableValues( throw new GraphQLError( `Variable "$${varName}" of required type ` + `"${String(varType)}" was not provided.`, - [ definitionAST ] + [ varDefNode ] ); } } else { @@ -87,7 +87,7 @@ export function getVariableValues( throw new GraphQLError( `Variable "$${varName}" got invalid value ` + `${JSON.stringify(value)}.${message}`, - [ definitionAST ] + [ varDefNode ] ); } @@ -105,23 +105,23 @@ export function getVariableValues( */ export function getArgumentValues( def: GraphQLField | GraphQLDirective, - node: Field | Directive, + node: FieldNode | DirectiveNode, variableValues?: ?{ [key: string]: mixed } ): { [key: string]: mixed } { const argDefs = def.args; - const argASTs = node.arguments; - if (!argDefs || !argASTs) { + const argNodes = node.arguments; + if (!argDefs || !argNodes) { return {}; } const coercedValues = Object.create(null); - const argASTMap = keyMap(argASTs, arg => arg.name.value); + const argNodeMap = keyMap(argNodes, arg => arg.name.value); for (let i = 0; i < argDefs.length; i++) { const argDef = argDefs[i]; const name = argDef.name; const argType = argDef.type; - const argumentAST = argASTMap[name]; + const argumentNode = argNodeMap[name]; const defaultValue = argDef.defaultValue; - if (!argumentAST) { + if (!argumentNode) { if (!isInvalid(defaultValue)) { coercedValues[name] = defaultValue; } else if (argType instanceof GraphQLNonNull) { @@ -131,8 +131,8 @@ export function getArgumentValues( [ node ] ); } - } else if (argumentAST.value.kind === Kind.VARIABLE) { - const variableName = (argumentAST.value: Variable).name.value; + } else if (argumentNode.value.kind === Kind.VARIABLE) { + const variableName = (argumentNode.value: VariableNode).name.value; if (variableValues && !isInvalid(variableValues[variableName])) { // Note: this does not check that this variable value is correct. // This assumes that this query has been validated and the variable @@ -145,18 +145,18 @@ export function getArgumentValues( `Argument "${name}" of required type "${String(argType)}" was ` + `provided the variable "$${variableName}" which was not provided ` + 'a runtime value.', - [ argumentAST.value ] + [ argumentNode.value ] ); } } else { - const valueAST = argumentAST.value; - const coercedValue = valueFromAST(valueAST, argType, variableValues); + const valueNode = argumentNode.value; + const coercedValue = valueFromAST(valueNode, argType, variableValues); if (isInvalid(coercedValue)) { - const errors = isValidLiteralValue(argType, valueAST); + const errors = isValidLiteralValue(argType, valueNode); const message = errors ? '\n' + errors.join('\n') : ''; throw new GraphQLError( - `Argument "${name}" got invalid value ${print(valueAST)}.${message}`, - [ argumentAST.value ] + `Argument "${name}" got invalid value ${print(valueNode)}.${message}`, + [ argumentNode.value ] ); } coercedValues[name] = coercedValue; diff --git a/src/index.js b/src/index.js index 82f5faa37a..52f66d8780 100644 --- a/src/index.js +++ b/src/index.js @@ -172,51 +172,51 @@ export type { // AST nodes Location, Token, - Node, - Name, - Document, - Definition, - OperationDefinition, - OperationType, - VariableDefinition, - Variable, - SelectionSet, - Selection, - Field, - Argument, - FragmentSpread, - InlineFragment, - FragmentDefinition, - Value, - IntValue, - FloatValue, - StringValue, - BooleanValue, - NullValue, - EnumValue, - ListValue, - ObjectValue, - ObjectField, - Directive, - Type, - NamedType, - ListType, - NonNullType, - TypeSystemDefinition, - SchemaDefinition, - OperationTypeDefinition, - TypeDefinition, - ScalarTypeDefinition, - ObjectTypeDefinition, - FieldDefinition, - InputValueDefinition, - InterfaceTypeDefinition, - UnionTypeDefinition, - EnumTypeDefinition, - EnumValueDefinition, - InputObjectTypeDefinition, - TypeExtensionDefinition, - DirectiveDefinition, + ASTNode, + NameNode, + DocumentNode, + DefinitionNode, + OperationDefinitionNode, + OperationTypeNode, + VariableDefinitionNode, + VariableNode, + SelectionSetNode, + SelectionNode, + FieldNode, + ArgumentNode, + FragmentSpreadNode, + InlineFragmentNode, + FragmentDefinitionNode, + ValueNode, + IntValueNode, + FloatValueNode, + StringValueNode, + BooleanValueNode, + NullValueNode, + EnumValueNode, + ListValueNode, + ObjectValueNode, + ObjectFieldNode, + DirectiveNode, + TypeNode, + NamedTypeNode, + ListTypeNode, + NonNullTypeNode, + TypeSystemDefinitionNode, + SchemaDefinitionNode, + OperationTypeDefinitionNode, + TypeDefinitionNode, + ScalarTypeDefinitionNode, + ObjectTypeDefinitionNode, + FieldDefinitionNode, + InputValueDefinitionNode, + InterfaceTypeDefinitionNode, + UnionTypeDefinitionNode, + EnumTypeDefinitionNode, + EnumValueDefinitionNode, + InputObjectTypeDefinitionNode, + TypeExtensionDefinitionNode, + DirectiveDefinitionNode, } from './language'; @@ -228,7 +228,7 @@ export { export type { ExecutionResult, -} from './execute'; +} from './execution'; // Validate GraphQL queries. @@ -331,4 +331,4 @@ export type { IntrospectionType, IntrospectionTypeRef, IntrospectionUnionType, -} from './introspectionQuery'; +} from './utilities'; diff --git a/src/language/ast.js b/src/language/ast.js index 81d52ae87a..3639ec9588 100644 --- a/src/language/ast.js +++ b/src/language/ast.js @@ -110,48 +110,48 @@ export type Token = { /** * The list of all possible AST node types. */ -export type Node = - | Name - | Document - | OperationDefinition - | VariableDefinition - | Variable - | SelectionSet - | Field - | Argument - | FragmentSpread - | InlineFragment - | FragmentDefinition - | IntValue - | FloatValue - | StringValue - | BooleanValue - | NullValue - | EnumValue - | ListValue - | ObjectValue - | ObjectField - | Directive - | NamedType - | ListType - | NonNullType - | SchemaDefinition - | OperationTypeDefinition - | ScalarTypeDefinition - | ObjectTypeDefinition - | FieldDefinition - | InputValueDefinition - | InterfaceTypeDefinition - | UnionTypeDefinition - | EnumTypeDefinition - | EnumValueDefinition - | InputObjectTypeDefinition - | TypeExtensionDefinition - | DirectiveDefinition; +export type ASTNode = + | NameNode + | DocumentNode + | OperationDefinitionNode + | VariableDefinitionNode + | VariableNode + | SelectionSetNode + | FieldNode + | ArgumentNode + | FragmentSpreadNode + | InlineFragmentNode + | FragmentDefinitionNode + | IntValueNode + | FloatValueNode + | StringValueNode + | BooleanValueNode + | NullValueNode + | EnumValueNode + | ListValueNode + | ObjectValueNode + | ObjectFieldNode + | DirectiveNode + | NamedTypeNode + | ListTypeNode + | NonNullTypeNode + | SchemaDefinitionNode + | OperationTypeDefinitionNode + | ScalarTypeDefinitionNode + | ObjectTypeDefinitionNode + | FieldDefinitionNode + | InputValueDefinitionNode + | InterfaceTypeDefinitionNode + | UnionTypeDefinitionNode + | EnumTypeDefinitionNode + | EnumValueDefinitionNode + | InputObjectTypeDefinitionNode + | TypeExtensionDefinitionNode + | DirectiveDefinitionNode; // Name -export type Name = { +export type NameNode = { kind: 'Name'; loc?: Location; value: string; @@ -159,316 +159,316 @@ export type Name = { // Document -export type Document = { +export type DocumentNode = { kind: 'Document'; loc?: Location; - definitions: Array; + definitions: Array; }; -export type Definition = - | OperationDefinition - | FragmentDefinition - | TypeSystemDefinition; // experimental non-spec addition. +export type DefinitionNode = + | OperationDefinitionNode + | FragmentDefinitionNode + | TypeSystemDefinitionNode; // experimental non-spec addition. -export type OperationDefinition = { +export type OperationDefinitionNode = { kind: 'OperationDefinition'; loc?: Location; - operation: OperationType; - name?: ?Name; - variableDefinitions?: ?Array; - directives?: ?Array; - selectionSet: SelectionSet; + operation: OperationTypeNode; + name?: ?NameNode; + variableDefinitions?: ?Array; + directives?: ?Array; + selectionSet: SelectionSetNode; }; // Note: subscription is an experimental non-spec addition. -export type OperationType = 'query' | 'mutation' | 'subscription'; +export type OperationTypeNode = 'query' | 'mutation' | 'subscription'; -export type VariableDefinition = { +export type VariableDefinitionNode = { kind: 'VariableDefinition'; loc?: Location; - variable: Variable; - type: Type; - defaultValue?: ?Value; + variable: VariableNode; + type: TypeNode; + defaultValue?: ?ValueNode; }; -export type Variable = { +export type VariableNode = { kind: 'Variable'; loc?: Location; - name: Name; + name: NameNode; }; -export type SelectionSet = { +export type SelectionSetNode = { kind: 'SelectionSet'; loc?: Location; - selections: Array; + selections: Array; }; -export type Selection = - | Field - | FragmentSpread - | InlineFragment; +export type SelectionNode = + | FieldNode + | FragmentSpreadNode + | InlineFragmentNode; -export type Field = { +export type FieldNode = { kind: 'Field'; loc?: Location; - alias?: ?Name; - name: Name; - arguments?: ?Array; - directives?: ?Array; - selectionSet?: ?SelectionSet; + alias?: ?NameNode; + name: NameNode; + arguments?: ?Array; + directives?: ?Array; + selectionSet?: ?SelectionSetNode; }; -export type Argument = { +export type ArgumentNode = { kind: 'Argument'; loc?: Location; - name: Name; - value: Value; + name: NameNode; + value: ValueNode; }; // Fragments -export type FragmentSpread = { +export type FragmentSpreadNode = { kind: 'FragmentSpread'; loc?: Location; - name: Name; - directives?: ?Array; + name: NameNode; + directives?: ?Array; }; -export type InlineFragment = { +export type InlineFragmentNode = { kind: 'InlineFragment'; loc?: Location; - typeCondition?: ?NamedType; - directives?: ?Array; - selectionSet: SelectionSet; + typeCondition?: ?NamedTypeNode; + directives?: ?Array; + selectionSet: SelectionSetNode; }; -export type FragmentDefinition = { +export type FragmentDefinitionNode = { kind: 'FragmentDefinition'; loc?: Location; - name: Name; - typeCondition: NamedType; - directives?: ?Array; - selectionSet: SelectionSet; + name: NameNode; + typeCondition: NamedTypeNode; + directives?: ?Array; + selectionSet: SelectionSetNode; }; // Values -export type Value = - | Variable - | IntValue - | FloatValue - | StringValue - | BooleanValue - | NullValue - | EnumValue - | ListValue - | ObjectValue; - -export type IntValue = { +export type ValueNode = + | VariableNode + | IntValueNode + | FloatValueNode + | StringValueNode + | BooleanValueNode + | NullValueNode + | EnumValueNode + | ListValueNode + | ObjectValueNode; + +export type IntValueNode = { kind: 'IntValue'; loc?: Location; value: string; }; -export type FloatValue = { +export type FloatValueNode = { kind: 'FloatValue'; loc?: Location; value: string; }; -export type StringValue = { +export type StringValueNode = { kind: 'StringValue'; loc?: Location; value: string; }; -export type BooleanValue = { +export type BooleanValueNode = { kind: 'BooleanValue'; loc?: Location; value: boolean; }; -export type NullValue = { +export type NullValueNode = { kind: 'NullValue'; loc?: Location; }; -export type EnumValue = { +export type EnumValueNode = { kind: 'EnumValue'; loc?: Location; value: string; }; -export type ListValue = { +export type ListValueNode = { kind: 'ListValue'; loc?: Location; - values: Array; + values: Array; }; -export type ObjectValue = { +export type ObjectValueNode = { kind: 'ObjectValue'; loc?: Location; - fields: Array; + fields: Array; }; -export type ObjectField = { +export type ObjectFieldNode = { kind: 'ObjectField'; loc?: Location; - name: Name; - value: Value; + name: NameNode; + value: ValueNode; }; // Directives -export type Directive = { +export type DirectiveNode = { kind: 'Directive'; loc?: Location; - name: Name; - arguments?: ?Array; + name: NameNode; + arguments?: ?Array; }; // Type Reference -export type Type = - | NamedType - | ListType - | NonNullType; +export type TypeNode = + | NamedTypeNode + | ListTypeNode + | NonNullTypeNode; -export type NamedType = { +export type NamedTypeNode = { kind: 'NamedType'; loc?: Location; - name: Name; + name: NameNode; }; -export type ListType = { +export type ListTypeNode = { kind: 'ListType'; loc?: Location; - type: Type; + type: TypeNode; }; -export type NonNullType = { +export type NonNullTypeNode = { kind: 'NonNullType'; loc?: Location; - type: NamedType | ListType; + type: NamedTypeNode | ListTypeNode; }; // Type System Definition -export type TypeSystemDefinition = - | SchemaDefinition - | TypeDefinition - | TypeExtensionDefinition - | DirectiveDefinition; +export type TypeSystemDefinitionNode = + | SchemaDefinitionNode + | TypeDefinitionNode + | TypeExtensionDefinitionNode + | DirectiveDefinitionNode; -export type SchemaDefinition = { +export type SchemaDefinitionNode = { kind: 'SchemaDefinition'; loc?: Location; - directives: Array; - operationTypes: Array; + directives: Array; + operationTypes: Array; }; -export type OperationTypeDefinition = { +export type OperationTypeDefinitionNode = { kind: 'OperationTypeDefinition'; loc?: Location; - operation: OperationType; - type: NamedType; + operation: OperationTypeNode; + type: NamedTypeNode; }; -export type TypeDefinition = - | ScalarTypeDefinition - | ObjectTypeDefinition - | InterfaceTypeDefinition - | UnionTypeDefinition - | EnumTypeDefinition - | InputObjectTypeDefinition; +export type TypeDefinitionNode = + | ScalarTypeDefinitionNode + | ObjectTypeDefinitionNode + | InterfaceTypeDefinitionNode + | UnionTypeDefinitionNode + | EnumTypeDefinitionNode + | InputObjectTypeDefinitionNode; -export type ScalarTypeDefinition = { +export type ScalarTypeDefinitionNode = { kind: 'ScalarTypeDefinition'; loc?: Location; - name: Name; - directives?: ?Array; + name: NameNode; + directives?: ?Array; }; -export type ObjectTypeDefinition = { +export type ObjectTypeDefinitionNode = { kind: 'ObjectTypeDefinition'; loc?: Location; - name: Name; - interfaces?: ?Array; - directives?: ?Array; - fields: Array; + name: NameNode; + interfaces?: ?Array; + directives?: ?Array; + fields: Array; }; -export type FieldDefinition = { +export type FieldDefinitionNode = { kind: 'FieldDefinition'; loc?: Location; - name: Name; - arguments: Array; - type: Type; - directives?: ?Array; + name: NameNode; + arguments: Array; + type: TypeNode; + directives?: ?Array; }; -export type InputValueDefinition = { +export type InputValueDefinitionNode = { kind: 'InputValueDefinition'; loc?: Location; - name: Name; - type: Type; - defaultValue?: ?Value; - directives?: ?Array; + name: NameNode; + type: TypeNode; + defaultValue?: ?ValueNode; + directives?: ?Array; }; -export type InterfaceTypeDefinition = { +export type InterfaceTypeDefinitionNode = { kind: 'InterfaceTypeDefinition'; loc?: Location; - name: Name; - directives?: ?Array; - fields: Array; + name: NameNode; + directives?: ?Array; + fields: Array; }; -export type UnionTypeDefinition = { +export type UnionTypeDefinitionNode = { kind: 'UnionTypeDefinition'; loc?: Location; - name: Name; - directives?: ?Array; - types: Array; + name: NameNode; + directives?: ?Array; + types: Array; }; -export type EnumTypeDefinition = { +export type EnumTypeDefinitionNode = { kind: 'EnumTypeDefinition'; loc?: Location; - name: Name; - directives?: ?Array; - values: Array; + name: NameNode; + directives?: ?Array; + values: Array; }; -export type EnumValueDefinition = { +export type EnumValueDefinitionNode = { kind: 'EnumValueDefinition'; loc?: Location; - name: Name; - directives?: ?Array; + name: NameNode; + directives?: ?Array; }; -export type InputObjectTypeDefinition = { +export type InputObjectTypeDefinitionNode = { kind: 'InputObjectTypeDefinition'; loc?: Location; - name: Name; - directives?: ?Array; - fields: Array; + name: NameNode; + directives?: ?Array; + fields: Array; }; -export type TypeExtensionDefinition = { +export type TypeExtensionDefinitionNode = { kind: 'TypeExtensionDefinition'; loc?: Location; - definition: ObjectTypeDefinition; + definition: ObjectTypeDefinitionNode; }; -export type DirectiveDefinition = { +export type DirectiveDefinitionNode = { kind: 'DirectiveDefinition'; loc?: Location; - name: Name; - arguments?: ?Array; - locations: Array; + name: NameNode; + arguments?: ?Array; + locations: Array; }; diff --git a/src/language/index.js b/src/language/index.js index dd246171e2..a82bc142a1 100644 --- a/src/language/index.js +++ b/src/language/index.js @@ -23,49 +23,51 @@ export type { ParseOptions } from './parser'; export type { Location, Token, - Node, - Name, - Document, - Definition, - OperationDefinition, - OperationType, - VariableDefinition, - Variable, - SelectionSet, - Selection, - Field, - Argument, - FragmentSpread, - InlineFragment, - FragmentDefinition, - Value, - IntValue, - FloatValue, - StringValue, - BooleanValue, - NullValue, - EnumValue, - ListValue, - ObjectValue, - ObjectField, - Directive, - Type, - NamedType, - ListType, - NonNullType, - TypeSystemDefinition, - SchemaDefinition, - OperationTypeDefinition, - TypeDefinition, - ScalarTypeDefinition, - ObjectTypeDefinition, - FieldDefinition, - InputValueDefinition, - InterfaceTypeDefinition, - UnionTypeDefinition, - EnumTypeDefinition, - EnumValueDefinition, - InputObjectTypeDefinition, - TypeExtensionDefinition, - DirectiveDefinition, + ASTNode, + + // Each kind of AST node + NameNode, + DocumentNode, + DefinitionNode, + OperationDefinitionNode, + OperationTypeNode, + VariableDefinitionNode, + VariableNode, + SelectionSetNode, + SelectionNode, + FieldNode, + ArgumentNode, + FragmentSpreadNode, + InlineFragmentNode, + FragmentDefinitionNode, + ValueNode, + IntValueNode, + FloatValueNode, + StringValueNode, + BooleanValueNode, + NullValueNode, + EnumValueNode, + ListValueNode, + ObjectValueNode, + ObjectFieldNode, + DirectiveNode, + TypeNode, + NamedTypeNode, + ListTypeNode, + NonNullTypeNode, + TypeSystemDefinitionNode, + SchemaDefinitionNode, + OperationTypeDefinitionNode, + TypeDefinitionNode, + ScalarTypeDefinitionNode, + ObjectTypeDefinitionNode, + FieldDefinitionNode, + InputValueDefinitionNode, + InterfaceTypeDefinitionNode, + UnionTypeDefinitionNode, + EnumTypeDefinitionNode, + EnumValueDefinitionNode, + InputObjectTypeDefinitionNode, + TypeExtensionDefinitionNode, + DirectiveDefinitionNode, } from './ast'; diff --git a/src/language/parser.js b/src/language/parser.js index af4ee1c715..b649eb50da 100644 --- a/src/language/parser.js +++ b/src/language/parser.js @@ -21,53 +21,53 @@ import type { Location, Token, - Name, - Variable, - - Document, - Definition, - OperationDefinition, - OperationType, - VariableDefinition, - SelectionSet, - Selection, - Field, - Argument, - - FragmentSpread, - InlineFragment, - FragmentDefinition, - - Value, - ListValue, - ObjectValue, - ObjectField, - - Directive, - - Type, - NamedType, - ListType, - NonNullType, - - TypeSystemDefinition, - - SchemaDefinition, - OperationTypeDefinition, - - ScalarTypeDefinition, - ObjectTypeDefinition, - FieldDefinition, - InputValueDefinition, - InterfaceTypeDefinition, - UnionTypeDefinition, - EnumTypeDefinition, - EnumValueDefinition, - InputObjectTypeDefinition, - - TypeExtensionDefinition, - - DirectiveDefinition, + NameNode, + VariableNode, + + DocumentNode, + DefinitionNode, + OperationDefinitionNode, + OperationTypeNode, + VariableDefinitionNode, + SelectionSetNode, + SelectionNode, + FieldNode, + ArgumentNode, + + FragmentSpreadNode, + InlineFragmentNode, + FragmentDefinitionNode, + + ValueNode, + ListValueNode, + ObjectValueNode, + ObjectFieldNode, + + DirectiveNode, + + TypeNode, + NamedTypeNode, + ListTypeNode, + NonNullTypeNode, + + TypeSystemDefinitionNode, + + SchemaDefinitionNode, + OperationTypeDefinitionNode, + + ScalarTypeDefinitionNode, + ObjectTypeDefinitionNode, + FieldDefinitionNode, + InputValueDefinitionNode, + InterfaceTypeDefinitionNode, + UnionTypeDefinitionNode, + EnumTypeDefinitionNode, + EnumValueDefinitionNode, + InputObjectTypeDefinitionNode, + + TypeExtensionDefinitionNode, + + DirectiveDefinitionNode, } from './ast'; import { @@ -139,7 +139,7 @@ export type ParseOptions = { export function parse( source: string | Source, options?: ParseOptions -): Document { +): DocumentNode { const sourceObj = typeof source === 'string' ? new Source(source) : source; const lexer = createLexer(sourceObj, options || {}); return parseDocument(lexer); @@ -158,7 +158,7 @@ export function parse( export function parseValue( source: string | Source, options?: ParseOptions -): Value { +): ValueNode { const sourceObj = typeof source === 'string' ? new Source(source) : source; const lexer = createLexer(sourceObj, options || {}); expect(lexer, TokenKind.SOF); @@ -180,7 +180,7 @@ export function parseValue( export function parseType( source: string | Source, options?: ParseOptions -): Type { +): TypeNode { const sourceObj = typeof source === 'string' ? new Source(source) : source; const lexer = createLexer(sourceObj, options || {}); expect(lexer, TokenKind.SOF); @@ -192,7 +192,7 @@ export function parseType( /** * Converts a name lex token into a name parse node. */ -function parseName(lexer: Lexer<*>): Name { +function parseName(lexer: Lexer<*>): NameNode { const token = expect(lexer, TokenKind.NAME); return { kind: NAME, @@ -206,7 +206,7 @@ function parseName(lexer: Lexer<*>): Name { /** * Document : Definition+ */ -function parseDocument(lexer: Lexer<*>): Document { +function parseDocument(lexer: Lexer<*>): DocumentNode { const start = lexer.token; expect(lexer, TokenKind.SOF); const definitions = []; @@ -227,7 +227,7 @@ function parseDocument(lexer: Lexer<*>): Document { * - FragmentDefinition * - TypeSystemDefinition */ -function parseDefinition(lexer: Lexer<*>): Definition { +function parseDefinition(lexer: Lexer<*>): DefinitionNode { if (peek(lexer, TokenKind.BRACE_L)) { return parseOperationDefinition(lexer); } @@ -266,7 +266,7 @@ function parseDefinition(lexer: Lexer<*>): Definition { * - SelectionSet * - OperationType Name? VariableDefinitions? Directives? SelectionSet */ -function parseOperationDefinition(lexer: Lexer<*>): OperationDefinition { +function parseOperationDefinition(lexer: Lexer<*>): OperationDefinitionNode { const start = lexer.token; if (peek(lexer, TokenKind.BRACE_L)) { return { @@ -298,7 +298,7 @@ function parseOperationDefinition(lexer: Lexer<*>): OperationDefinition { /** * OperationType : one of query mutation subscription */ -function parseOperationType(lexer: Lexer<*>): OperationType { +function parseOperationType(lexer: Lexer<*>): OperationTypeNode { const operationToken = expect(lexer, TokenKind.NAME); switch (operationToken.value) { case 'query': return 'query'; @@ -313,7 +313,9 @@ function parseOperationType(lexer: Lexer<*>): OperationType { /** * VariableDefinitions : ( VariableDefinition+ ) */ -function parseVariableDefinitions(lexer: Lexer<*>): Array { +function parseVariableDefinitions( + lexer: Lexer<*> +): Array { return peek(lexer, TokenKind.PAREN_L) ? many( lexer, @@ -327,7 +329,7 @@ function parseVariableDefinitions(lexer: Lexer<*>): Array { /** * VariableDefinition : Variable : Type DefaultValue? */ -function parseVariableDefinition(lexer: Lexer<*>): VariableDefinition { +function parseVariableDefinition(lexer: Lexer<*>): VariableDefinitionNode { const start = lexer.token; return { kind: VARIABLE_DEFINITION, @@ -342,7 +344,7 @@ function parseVariableDefinition(lexer: Lexer<*>): VariableDefinition { /** * Variable : $ Name */ -function parseVariable(lexer: Lexer<*>): Variable { +function parseVariable(lexer: Lexer<*>): VariableNode { const start = lexer.token; expect(lexer, TokenKind.DOLLAR); return { @@ -355,7 +357,7 @@ function parseVariable(lexer: Lexer<*>): Variable { /** * SelectionSet : { Selection+ } */ -function parseSelectionSet(lexer: Lexer<*>): SelectionSet { +function parseSelectionSet(lexer: Lexer<*>): SelectionSetNode { const start = lexer.token; return { kind: SELECTION_SET, @@ -371,7 +373,7 @@ function parseSelectionSet(lexer: Lexer<*>): SelectionSet { * - FragmentSpread * - InlineFragment */ -function parseSelection(lexer: Lexer<*>): Selection { +function parseSelection(lexer: Lexer<*>): SelectionNode { return peek(lexer, TokenKind.SPREAD) ? parseFragment(lexer) : parseField(lexer); @@ -382,7 +384,7 @@ function parseSelection(lexer: Lexer<*>): Selection { * * Alias : Name : */ -function parseField(lexer: Lexer<*>): Field { +function parseField(lexer: Lexer<*>): FieldNode { const start = lexer.token; const nameOrAlias = parseName(lexer); @@ -411,7 +413,7 @@ function parseField(lexer: Lexer<*>): Field { /** * Arguments : ( Argument+ ) */ -function parseArguments(lexer: Lexer<*>): Array { +function parseArguments(lexer: Lexer<*>): Array { return peek(lexer, TokenKind.PAREN_L) ? many(lexer, TokenKind.PAREN_L, parseArgument, TokenKind.PAREN_R) : []; @@ -420,7 +422,7 @@ function parseArguments(lexer: Lexer<*>): Array { /** * Argument : Name : Value */ -function parseArgument(lexer: Lexer<*>): Argument { +function parseArgument(lexer: Lexer<*>): ArgumentNode { const start = lexer.token; return { kind: ARGUMENT, @@ -440,7 +442,9 @@ function parseArgument(lexer: Lexer<*>): Argument { * * InlineFragment : ... TypeCondition? Directives? SelectionSet */ -function parseFragment(lexer: Lexer<*>): FragmentSpread | InlineFragment { +function parseFragment( + lexer: Lexer<*> +): FragmentSpreadNode | InlineFragmentNode { const start = lexer.token; expect(lexer, TokenKind.SPREAD); if (peek(lexer, TokenKind.NAME) && lexer.token.value !== 'on') { @@ -471,7 +475,7 @@ function parseFragment(lexer: Lexer<*>): FragmentSpread | InlineFragment { * * TypeCondition : NamedType */ -function parseFragmentDefinition(lexer: Lexer<*>): FragmentDefinition { +function parseFragmentDefinition(lexer: Lexer<*>): FragmentDefinitionNode { const start = lexer.token; expectKeyword(lexer, 'fragment'); return { @@ -487,7 +491,7 @@ function parseFragmentDefinition(lexer: Lexer<*>): FragmentDefinition { /** * FragmentName : Name but not `on` */ -function parseFragmentName(lexer: Lexer<*>): Name { +function parseFragmentName(lexer: Lexer<*>): NameNode { if (lexer.token.value === 'on') { throw unexpected(lexer); } @@ -515,7 +519,7 @@ function parseFragmentName(lexer: Lexer<*>): Name { * * EnumValue : Name but not `true`, `false` or `null` */ -function parseValueLiteral(lexer: Lexer<*>, isConst: boolean): Value { +function parseValueLiteral(lexer: Lexer<*>, isConst: boolean): ValueNode { const token = lexer.token; switch (token.kind) { case TokenKind.BRACKET_L: @@ -573,11 +577,11 @@ function parseValueLiteral(lexer: Lexer<*>, isConst: boolean): Value { throw unexpected(lexer); } -export function parseConstValue(lexer: Lexer<*>): Value { +export function parseConstValue(lexer: Lexer<*>): ValueNode { return parseValueLiteral(lexer, true); } -function parseValueValue(lexer: Lexer<*>): Value { +function parseValueValue(lexer: Lexer<*>): ValueNode { return parseValueLiteral(lexer, false); } @@ -586,7 +590,7 @@ function parseValueValue(lexer: Lexer<*>): Value { * - [ ] * - [ Value[?Const]+ ] */ -function parseList(lexer: Lexer<*>, isConst: boolean): ListValue { +function parseList(lexer: Lexer<*>, isConst: boolean): ListValueNode { const start = lexer.token; const item = isConst ? parseConstValue : parseValueValue; return { @@ -601,7 +605,7 @@ function parseList(lexer: Lexer<*>, isConst: boolean): ListValue { * - { } * - { ObjectField[?Const]+ } */ -function parseObject(lexer: Lexer<*>, isConst: boolean): ObjectValue { +function parseObject(lexer: Lexer<*>, isConst: boolean): ObjectValueNode { const start = lexer.token; expect(lexer, TokenKind.BRACE_L); const fields = []; @@ -618,7 +622,7 @@ function parseObject(lexer: Lexer<*>, isConst: boolean): ObjectValue { /** * ObjectField[Const] : Name : Value[?Const] */ -function parseObjectField(lexer: Lexer<*>, isConst: boolean): ObjectField { +function parseObjectField(lexer: Lexer<*>, isConst: boolean): ObjectFieldNode { const start = lexer.token; return { kind: OBJECT_FIELD, @@ -635,7 +639,7 @@ function parseObjectField(lexer: Lexer<*>, isConst: boolean): ObjectField { /** * Directives : Directive+ */ -function parseDirectives(lexer: Lexer<*>): Array { +function parseDirectives(lexer: Lexer<*>): Array { const directives = []; while (peek(lexer, TokenKind.AT)) { directives.push(parseDirective(lexer)); @@ -646,7 +650,7 @@ function parseDirectives(lexer: Lexer<*>): Array { /** * Directive : @ Name Arguments? */ -function parseDirective(lexer: Lexer<*>): Directive { +function parseDirective(lexer: Lexer<*>): DirectiveNode { const start = lexer.token; expect(lexer, TokenKind.AT); return { @@ -666,7 +670,7 @@ function parseDirective(lexer: Lexer<*>): Directive { * - ListType * - NonNullType */ -export function parseTypeReference(lexer: Lexer<*>): Type { +export function parseTypeReference(lexer: Lexer<*>): TypeNode { const start = lexer.token; let type; if (skip(lexer, TokenKind.BRACKET_L)) { @@ -676,7 +680,7 @@ export function parseTypeReference(lexer: Lexer<*>): Type { kind: LIST_TYPE, type, loc: loc(lexer, start) - }: ListType); + }: ListTypeNode); } else { type = parseNamedType(lexer); } @@ -685,7 +689,7 @@ export function parseTypeReference(lexer: Lexer<*>): Type { kind: NON_NULL_TYPE, type, loc: loc(lexer, start) - }: NonNullType); + }: NonNullTypeNode); } return type; } @@ -693,7 +697,7 @@ export function parseTypeReference(lexer: Lexer<*>): Type { /** * NamedType : Name */ -export function parseNamedType(lexer: Lexer<*>): NamedType { +export function parseNamedType(lexer: Lexer<*>): NamedTypeNode { const start = lexer.token; return { kind: NAMED_TYPE, @@ -720,7 +724,7 @@ export function parseNamedType(lexer: Lexer<*>): NamedType { * - EnumTypeDefinition * - InputObjectTypeDefinition */ -function parseTypeSystemDefinition(lexer: Lexer<*>): TypeSystemDefinition { +function parseTypeSystemDefinition(lexer: Lexer<*>): TypeSystemDefinitionNode { if (peek(lexer, TokenKind.NAME)) { switch (lexer.token.value) { case 'schema': return parseSchemaDefinition(lexer); @@ -743,7 +747,7 @@ function parseTypeSystemDefinition(lexer: Lexer<*>): TypeSystemDefinition { * * OperationTypeDefinition : OperationType : NamedType */ -function parseSchemaDefinition(lexer: Lexer<*>): SchemaDefinition { +function parseSchemaDefinition(lexer: Lexer<*>): SchemaDefinitionNode { const start = lexer.token; expectKeyword(lexer, 'schema'); const directives = parseDirectives(lexer); @@ -763,7 +767,7 @@ function parseSchemaDefinition(lexer: Lexer<*>): SchemaDefinition { function parseOperationTypeDefinition( lexer: Lexer<*> -): OperationTypeDefinition { +): OperationTypeDefinitionNode { const start = lexer.token; const operation = parseOperationType(lexer); expect(lexer, TokenKind.COLON); @@ -779,7 +783,7 @@ function parseOperationTypeDefinition( /** * ScalarTypeDefinition : scalar Name Directives? */ -function parseScalarTypeDefinition(lexer: Lexer<*>): ScalarTypeDefinition { +function parseScalarTypeDefinition(lexer: Lexer<*>): ScalarTypeDefinitionNode { const start = lexer.token; expectKeyword(lexer, 'scalar'); const name = parseName(lexer); @@ -796,7 +800,7 @@ function parseScalarTypeDefinition(lexer: Lexer<*>): ScalarTypeDefinition { * ObjectTypeDefinition : * - type Name ImplementsInterfaces? Directives? { FieldDefinition+ } */ -function parseObjectTypeDefinition(lexer: Lexer<*>): ObjectTypeDefinition { +function parseObjectTypeDefinition(lexer: Lexer<*>): ObjectTypeDefinitionNode { const start = lexer.token; expectKeyword(lexer, 'type'); const name = parseName(lexer); @@ -821,7 +825,7 @@ function parseObjectTypeDefinition(lexer: Lexer<*>): ObjectTypeDefinition { /** * ImplementsInterfaces : implements NamedType+ */ -function parseImplementsInterfaces(lexer: Lexer<*>): Array { +function parseImplementsInterfaces(lexer: Lexer<*>): Array { const types = []; if (lexer.token.value === 'implements') { lexer.advance(); @@ -835,7 +839,7 @@ function parseImplementsInterfaces(lexer: Lexer<*>): Array { /** * FieldDefinition : Name ArgumentsDefinition? : Type Directives? */ -function parseFieldDefinition(lexer: Lexer<*>): FieldDefinition { +function parseFieldDefinition(lexer: Lexer<*>): FieldDefinitionNode { const start = lexer.token; const name = parseName(lexer); const args = parseArgumentDefs(lexer); @@ -855,7 +859,7 @@ function parseFieldDefinition(lexer: Lexer<*>): FieldDefinition { /** * ArgumentsDefinition : ( InputValueDefinition+ ) */ -function parseArgumentDefs(lexer: Lexer<*>): Array { +function parseArgumentDefs(lexer: Lexer<*>): Array { if (!peek(lexer, TokenKind.PAREN_L)) { return []; } @@ -865,7 +869,7 @@ function parseArgumentDefs(lexer: Lexer<*>): Array { /** * InputValueDefinition : Name : Type DefaultValue? Directives? */ -function parseInputValueDef(lexer: Lexer<*>): InputValueDefinition { +function parseInputValueDef(lexer: Lexer<*>): InputValueDefinitionNode { const start = lexer.token; const name = parseName(lexer); expect(lexer, TokenKind.COLON); @@ -890,7 +894,7 @@ function parseInputValueDef(lexer: Lexer<*>): InputValueDefinition { */ function parseInterfaceTypeDefinition( lexer: Lexer<*> -): InterfaceTypeDefinition { +): InterfaceTypeDefinitionNode { const start = lexer.token; expectKeyword(lexer, 'interface'); const name = parseName(lexer); @@ -913,7 +917,7 @@ function parseInterfaceTypeDefinition( /** * UnionTypeDefinition : union Name Directives? = UnionMembers */ -function parseUnionTypeDefinition(lexer: Lexer<*>): UnionTypeDefinition { +function parseUnionTypeDefinition(lexer: Lexer<*>): UnionTypeDefinitionNode { const start = lexer.token; expectKeyword(lexer, 'union'); const name = parseName(lexer); @@ -934,7 +938,7 @@ function parseUnionTypeDefinition(lexer: Lexer<*>): UnionTypeDefinition { * - NamedType * - UnionMembers | NamedType */ -function parseUnionMembers(lexer: Lexer<*>): Array { +function parseUnionMembers(lexer: Lexer<*>): Array { const members = []; do { members.push(parseNamedType(lexer)); @@ -945,7 +949,7 @@ function parseUnionMembers(lexer: Lexer<*>): Array { /** * EnumTypeDefinition : enum Name Directives? { EnumValueDefinition+ } */ -function parseEnumTypeDefinition(lexer: Lexer<*>): EnumTypeDefinition { +function parseEnumTypeDefinition(lexer: Lexer<*>): EnumTypeDefinitionNode { const start = lexer.token; expectKeyword(lexer, 'enum'); const name = parseName(lexer); @@ -970,7 +974,7 @@ function parseEnumTypeDefinition(lexer: Lexer<*>): EnumTypeDefinition { * * EnumValue : Name */ -function parseEnumValueDefinition(lexer: Lexer<*>): EnumValueDefinition { +function parseEnumValueDefinition(lexer: Lexer<*>): EnumValueDefinitionNode { const start = lexer.token; const name = parseName(lexer); const directives = parseDirectives(lexer); @@ -987,7 +991,7 @@ function parseEnumValueDefinition(lexer: Lexer<*>): EnumValueDefinition { */ function parseInputObjectTypeDefinition( lexer: Lexer<*> -): InputObjectTypeDefinition { +): InputObjectTypeDefinitionNode { const start = lexer.token; expectKeyword(lexer, 'input'); const name = parseName(lexer); @@ -1012,7 +1016,7 @@ function parseInputObjectTypeDefinition( */ function parseTypeExtensionDefinition( lexer: Lexer<*> -): TypeExtensionDefinition { +): TypeExtensionDefinitionNode { const start = lexer.token; expectKeyword(lexer, 'extend'); const definition = parseObjectTypeDefinition(lexer); @@ -1027,7 +1031,7 @@ function parseTypeExtensionDefinition( * DirectiveDefinition : * - directive @ Name ArgumentsDefinition? on DirectiveLocations */ -function parseDirectiveDefinition(lexer: Lexer<*>): DirectiveDefinition { +function parseDirectiveDefinition(lexer: Lexer<*>): DirectiveDefinitionNode { const start = lexer.token; expectKeyword(lexer, 'directive'); expect(lexer, TokenKind.AT); @@ -1049,7 +1053,7 @@ function parseDirectiveDefinition(lexer: Lexer<*>): DirectiveDefinition { * - Name * - DirectiveLocations | Name */ -function parseDirectiveLocations(lexer: Lexer<*>): Array { +function parseDirectiveLocations(lexer: Lexer<*>): Array { const locations = []; do { locations.push(parseName(lexer)); diff --git a/src/type/definition.js b/src/type/definition.js index 3feaedd0cb..0dc258e93e 100644 --- a/src/type/definition.js +++ b/src/type/definition.js @@ -13,10 +13,10 @@ import isNullish from '../jsutils/isNullish'; import { ENUM } from '../language/kinds'; import { assertValidName } from '../utilities/assertValidName'; import type { - OperationDefinition, - Field, - FragmentDefinition, - Value, + OperationDefinitionNode, + FieldNode, + FragmentDefinitionNode, + ValueNode, } from '../language/ast'; import type { GraphQLSchema } from './schema'; @@ -258,9 +258,9 @@ export class GraphQLScalarType { } // Parses an externally provided literal value to use as an input. - parseLiteral(valueAST: Value): mixed { + parseLiteral(valueNode: ValueNode): mixed { const parser = this._scalarConfig.parseLiteral; - return parser ? parser(valueAST) : null; + return parser ? parser(valueNode) : null; } toString(): string { @@ -273,7 +273,7 @@ export type GraphQLScalarTypeConfig = { description?: ?string; serialize: (value: mixed) => ?TExternal; parseValue?: (value: mixed) => ?TInternal; - parseLiteral?: (valueAST: Value) => ?TInternal; + parseLiteral?: (valueNode: ValueNode) => ?TInternal; }; @@ -488,14 +488,14 @@ export type GraphQLFieldResolveFn = ( export type GraphQLResolveInfo = { fieldName: string; - fieldASTs: Array; + fieldNodes: Array; returnType: GraphQLOutputType; parentType: GraphQLCompositeType; path: Array; schema: GraphQLSchema; - fragments: { [fragmentName: string]: FragmentDefinition }; + fragments: { [fragmentName: string]: FragmentDefinitionNode }; rootValue: mixed; - operation: OperationDefinition; + operation: OperationDefinitionNode; variableValues: { [variableName: string]: mixed }; }; @@ -768,9 +768,9 @@ export class GraphQLEnumType/* */ { } } - parseLiteral(valueAST: Value): ?any/* T */ { - if (valueAST.kind === ENUM) { - const enumValue = this._getNameLookup()[valueAST.value]; + parseLiteral(valueNode: ValueNode): ?any/* T */ { + if (valueNode.kind === ENUM) { + const enumValue = this._getNameLookup()[valueNode.value]; if (enumValue) { return enumValue.value; } diff --git a/src/type/index.js b/src/type/index.js index d151d99945..7becdcfd9c 100644 --- a/src/type/index.js +++ b/src/type/index.js @@ -118,4 +118,3 @@ export type { GraphQLTypeResolveFn, GraphQLUnionTypeConfig, } from './definition'; - diff --git a/src/utilities/TypeInfo.js b/src/utilities/TypeInfo.js index 7f83d6841f..af4befc06d 100644 --- a/src/utilities/TypeInfo.js +++ b/src/utilities/TypeInfo.js @@ -9,7 +9,6 @@ */ import * as Kind from '../language/kinds'; -import type { Field } from '../language/ast'; import { isCompositeType, getNullableType, @@ -35,7 +34,7 @@ import { TypeNameMetaFieldDef } from '../type/introspection'; import type { GraphQLSchema } from '../type/schema'; -import type { Node } from '../language/ast'; +import type { ASTNode, FieldNode } from '../language/ast'; import { typeFromAST } from './typeFromAST'; import find from '../jsutils/find'; @@ -105,7 +104,7 @@ export class TypeInfo { } // Flow does not yet handle this case. - enter(node: any/* Node */) { + enter(node: any/* ASTNode */) { const schema = this._schema; switch (node.kind) { case Kind.SELECTION_SET: @@ -186,7 +185,7 @@ export class TypeInfo { } } - leave(node: Node) { + leave(node: ASTNode) { switch (node.kind) { case Kind.SELECTION_SET: this._parentTypeStack.pop(); @@ -226,9 +225,9 @@ export class TypeInfo { function getFieldDef( schema: GraphQLSchema, parentType: GraphQLType, - fieldAST: Field + fieldNode: FieldNode ): ?GraphQLField { - const name = fieldAST.name.value; + const name = fieldNode.name.value; if (name === SchemaMetaFieldDef.name && schema.getQueryType() === parentType) { return SchemaMetaFieldDef; diff --git a/src/utilities/astFromValue.js b/src/utilities/astFromValue.js index b3aa2d98d1..2960e0f964 100644 --- a/src/utilities/astFromValue.js +++ b/src/utilities/astFromValue.js @@ -14,15 +14,15 @@ import invariant from '../jsutils/invariant'; import isNullish from '../jsutils/isNullish'; import isInvalid from '../jsutils/isInvalid'; import type { - Value, - IntValue, - FloatValue, - StringValue, - BooleanValue, - NullValue, - EnumValue, - ListValue, - ObjectValue, + ValueNode, + IntValueNode, + FloatValueNode, + StringValueNode, + BooleanValueNode, + NullValueNode, + EnumValueNode, + ListValueNode, + ObjectValueNode, } from '../language/ast'; import { NAME, @@ -67,7 +67,7 @@ import { GraphQLID } from '../type/scalars'; export function astFromValue( value: mixed, type: GraphQLInputType -): ?Value { +): ?ValueNode { // Ensure flow knows that we treat function params as const. const _value = value; @@ -81,7 +81,7 @@ export function astFromValue( // only explicit null, not undefined, NaN if (_value === null) { - return ({ kind: NULL }: NullValue); + return ({ kind: NULL }: NullValueNode); } // undefined, NaN @@ -94,14 +94,14 @@ export function astFromValue( if (type instanceof GraphQLList) { const itemType = type.ofType; if (isCollection(_value)) { - const valuesASTs = []; + const valuesNodes = []; forEach((_value: any), item => { - const itemAST = astFromValue(item, itemType); - if (itemAST) { - valuesASTs.push(itemAST); + const itemNode = astFromValue(item, itemType); + if (itemNode) { + valuesNodes.push(itemNode); } }); - return ({ kind: LIST, values: valuesASTs }: ListValue); + return ({ kind: LIST, values: valuesNodes }: ListValueNode); } return astFromValue(_value, itemType); } @@ -113,19 +113,19 @@ export function astFromValue( return null; } const fields = type.getFields(); - const fieldASTs = []; + const fieldNodes = []; Object.keys(fields).forEach(fieldName => { const fieldType = fields[fieldName].type; const fieldValue = astFromValue(_value[fieldName], fieldType); if (fieldValue) { - fieldASTs.push({ + fieldNodes.push({ kind: OBJECT_FIELD, name: { kind: NAME, value: fieldName }, value: fieldValue }); } }); - return ({ kind: OBJECT, fields: fieldASTs }: ObjectValue); + return ({ kind: OBJECT, fields: fieldNodes }: ObjectValueNode); } invariant( @@ -142,26 +142,26 @@ export function astFromValue( // Others serialize based on their corresponding JavaScript scalar types. if (typeof serialized === 'boolean') { - return ({ kind: BOOLEAN, value: serialized }: BooleanValue); + return ({ kind: BOOLEAN, value: serialized }: BooleanValueNode); } // JavaScript numbers can be Int or Float values. if (typeof serialized === 'number') { const stringNum = String(serialized); return /^[0-9]+$/.test(stringNum) ? - ({ kind: INT, value: stringNum }: IntValue) : - ({ kind: FLOAT, value: stringNum }: FloatValue); + ({ kind: INT, value: stringNum }: IntValueNode) : + ({ kind: FLOAT, value: stringNum }: FloatValueNode); } if (typeof serialized === 'string') { // Enum types use Enum literals. if (type instanceof GraphQLEnumType) { - return ({ kind: ENUM, value: serialized }: EnumValue); + return ({ kind: ENUM, value: serialized }: EnumValueNode); } // ID types can use Int literals. if (type === GraphQLID && /^[0-9]+$/.test(serialized)) { - return ({ kind: INT, value: serialized }: IntValue); + return ({ kind: INT, value: serialized }: IntValueNode); } // Use JSON stringify, which uses the same string encoding as GraphQL, @@ -169,7 +169,7 @@ export function astFromValue( return ({ kind: STRING, value: JSON.stringify(serialized).slice(1, -1) - }: StringValue); + }: StringValueNode); } throw new TypeError('Cannot convert value to AST: ' + String(serialized)); diff --git a/src/utilities/buildASTSchema.js b/src/utilities/buildASTSchema.js index 3f2de8b3fe..6245919cdd 100644 --- a/src/utilities/buildASTSchema.js +++ b/src/utilities/buildASTSchema.js @@ -33,20 +33,20 @@ import { import type { Location, - Document, - Directive, - Type, - NamedType, - SchemaDefinition, - TypeDefinition, - ScalarTypeDefinition, - ObjectTypeDefinition, - InputValueDefinition, - InterfaceTypeDefinition, - UnionTypeDefinition, - EnumTypeDefinition, - InputObjectTypeDefinition, - DirectiveDefinition, + DocumentNode, + DirectiveNode, + TypeNode, + NamedTypeNode, + SchemaDefinitionNode, + TypeDefinitionNode, + ScalarTypeDefinitionNode, + ObjectTypeDefinitionNode, + InputValueDefinitionNode, + InterfaceTypeDefinitionNode, + UnionTypeDefinitionNode, + EnumTypeDefinitionNode, + InputObjectTypeDefinitionNode, + DirectiveDefinitionNode, } from '../language/ast'; import { GraphQLSchema } from '../type/schema'; @@ -104,21 +104,21 @@ import { function buildWrappedType( innerType: GraphQLType, - inputTypeAST: Type + inputTypeNode: TypeNode ): GraphQLType { - if (inputTypeAST.kind === LIST_TYPE) { - return new GraphQLList(buildWrappedType(innerType, inputTypeAST.type)); + if (inputTypeNode.kind === LIST_TYPE) { + return new GraphQLList(buildWrappedType(innerType, inputTypeNode.type)); } - if (inputTypeAST.kind === NON_NULL_TYPE) { - const wrappedType = buildWrappedType(innerType, inputTypeAST.type); + if (inputTypeNode.kind === NON_NULL_TYPE) { + const wrappedType = buildWrappedType(innerType, inputTypeNode.type); invariant(!(wrappedType instanceof GraphQLNonNull), 'No nesting nonnull.'); return new GraphQLNonNull(wrappedType); } return innerType; } -function getNamedTypeAST(typeAST: Type): NamedType { - let namedType = typeAST; +function getNamedTypeNode(typeNode: TypeNode): NamedTypeNode { + let namedType = typeNode; while (namedType.kind === LIST_TYPE || namedType.kind === NON_NULL_TYPE) { namedType = namedType.type; } @@ -135,16 +135,16 @@ function getNamedTypeAST(typeAST: Type): NamedType { * Given that AST it constructs a GraphQLSchema. The resulting schema * has no resolve methods, so execution will use default resolvers. */ -export function buildASTSchema(ast: Document): GraphQLSchema { +export function buildASTSchema(ast: DocumentNode): GraphQLSchema { if (!ast || ast.kind !== DOCUMENT) { throw new Error('Must provide a document ast.'); } - let schemaDef: ?SchemaDefinition; + let schemaDef: ?SchemaDefinitionNode; - const typeDefs: Array = []; - const astMap: {[name: string]: TypeDefinition} = Object.create(null); - const directiveDefs: Array = []; + const typeDefs: Array = []; + const nodeMap: {[name: string]: TypeDefinitionNode} = Object.create(null); + const directiveDefs: Array = []; for (let i = 0; i < ast.definitions.length; i++) { const d = ast.definitions[i]; switch (d.kind) { @@ -161,7 +161,7 @@ export function buildASTSchema(ast: Document): GraphQLSchema { case UNION_TYPE_DEFINITION: case INPUT_OBJECT_TYPE_DEFINITION: typeDefs.push(d); - astMap[d.name.value] = d; + nodeMap[d.name.value] = d; break; case DIRECTIVE_DEFINITION: directiveDefs.push(d); @@ -179,7 +179,7 @@ export function buildASTSchema(ast: Document): GraphQLSchema { if (queryTypeName) { throw new Error('Must provide only one query type in schema.'); } - if (!astMap[typeName]) { + if (!nodeMap[typeName]) { throw new Error( `Specified query type "${typeName}" not found in document.` ); @@ -189,7 +189,7 @@ export function buildASTSchema(ast: Document): GraphQLSchema { if (mutationTypeName) { throw new Error('Must provide only one mutation type in schema.'); } - if (!astMap[typeName]) { + if (!nodeMap[typeName]) { throw new Error( `Specified mutation type "${typeName}" not found in document.` ); @@ -199,7 +199,7 @@ export function buildASTSchema(ast: Document): GraphQLSchema { if (subscriptionTypeName) { throw new Error('Must provide only one subscription type in schema.'); } - if (!astMap[typeName]) { + if (!nodeMap[typeName]) { throw new Error( `Specified subscription type "${typeName}" not found in document.` ); @@ -208,13 +208,13 @@ export function buildASTSchema(ast: Document): GraphQLSchema { } }); } else { - if (astMap.Query) { + if (nodeMap.Query) { queryTypeName = 'Query'; } - if (astMap.Mutation) { + if (nodeMap.Mutation) { mutationTypeName = 'Mutation'; } - if (astMap.Subscription) { + if (nodeMap.Subscription) { subscriptionTypeName = 'Subscription'; } } @@ -259,27 +259,32 @@ export function buildASTSchema(ast: Document): GraphQLSchema { } return new GraphQLSchema({ - query: getObjectType(astMap[queryTypeName]), - mutation: mutationTypeName ? getObjectType(astMap[mutationTypeName]) : null, - subscription: - subscriptionTypeName ? getObjectType(astMap[subscriptionTypeName]) : null, + query: getObjectType(nodeMap[queryTypeName]), + mutation: mutationTypeName ? + getObjectType(nodeMap[mutationTypeName]) : + null, + subscription: subscriptionTypeName ? + getObjectType(nodeMap[subscriptionTypeName]) : + null, types, directives, }); - function getDirective(directiveAST: DirectiveDefinition): GraphQLDirective { + function getDirective( + directiveNode: DirectiveDefinitionNode + ): GraphQLDirective { return new GraphQLDirective({ - name: directiveAST.name.value, - description: getDescription(directiveAST), - locations: directiveAST.locations.map( + name: directiveNode.name.value, + description: getDescription(directiveNode), + locations: directiveNode.locations.map( node => ((node.value: any): DirectiveLocationEnum) ), - args: directiveAST.arguments && makeInputValues(directiveAST.arguments), + args: directiveNode.arguments && makeInputValues(directiveNode.arguments), }); } - function getObjectType(typeAST: TypeDefinition): GraphQLObjectType { - const type = typeDefNamed(typeAST.name.value); + function getObjectType(typeNode: TypeDefinitionNode): GraphQLObjectType { + const type = typeDefNamed(typeNode.name.value); invariant( type instanceof GraphQLObjectType, 'AST must provide object type.' @@ -287,32 +292,32 @@ export function buildASTSchema(ast: Document): GraphQLSchema { return (type: any); } - function produceType(typeAST: Type): GraphQLType { - const typeName = getNamedTypeAST(typeAST).name.value; + function produceType(typeNode: TypeNode): GraphQLType { + const typeName = getNamedTypeNode(typeNode).name.value; const typeDef = typeDefNamed(typeName); - return buildWrappedType(typeDef, typeAST); + return buildWrappedType(typeDef, typeNode); } - function produceInputType(typeAST: Type): GraphQLInputType { - const type = produceType(typeAST); + function produceInputType(typeNode: TypeNode): GraphQLInputType { + const type = produceType(typeNode); invariant(isInputType(type), 'Expected Input type.'); return (type: any); } - function produceOutputType(typeAST: Type): GraphQLOutputType { - const type = produceType(typeAST); + function produceOutputType(typeNode: TypeNode): GraphQLOutputType { + const type = produceType(typeNode); invariant(isOutputType(type), 'Expected Output type.'); return (type: any); } - function produceObjectType(typeAST: Type): GraphQLObjectType { - const type = produceType(typeAST); + function produceObjectType(typeNode: TypeNode): GraphQLObjectType { + const type = produceType(typeNode); invariant(type instanceof GraphQLObjectType, 'Expected Object type.'); return type; } - function produceInterfaceType(typeAST: Type): GraphQLInterfaceType { - const type = produceType(typeAST); + function produceInterfaceType(typeNode: TypeNode): GraphQLInterfaceType { + const type = produceType(typeNode); invariant(type instanceof GraphQLInterfaceType, 'Expected Object type.'); return type; } @@ -322,11 +327,11 @@ export function buildASTSchema(ast: Document): GraphQLSchema { return innerTypeMap[typeName]; } - if (!astMap[typeName]) { + if (!nodeMap[typeName]) { throw new Error(`Type "${typeName}" not found in document.`); } - const innerTypeDef = makeSchemaDef(astMap[typeName]); + const innerTypeDef = makeSchemaDef(nodeMap[typeName]); if (!innerTypeDef) { throw new Error(`Nothing constructed for "${typeName}".`); } @@ -356,7 +361,7 @@ export function buildASTSchema(ast: Document): GraphQLSchema { } } - function makeTypeDef(def: ObjectTypeDefinition) { + function makeTypeDef(def: ObjectTypeDefinitionNode) { const typeName = def.name.value; return new GraphQLObjectType({ name: typeName, @@ -367,7 +372,7 @@ export function buildASTSchema(ast: Document): GraphQLSchema { } function makeFieldDefMap( - def: ObjectTypeDefinition | InterfaceTypeDefinition + def: ObjectTypeDefinitionNode | InterfaceTypeDefinitionNode ) { return keyValMap( def.fields, @@ -381,12 +386,12 @@ export function buildASTSchema(ast: Document): GraphQLSchema { ); } - function makeImplementedInterfaces(def: ObjectTypeDefinition) { + function makeImplementedInterfaces(def: ObjectTypeDefinitionNode) { return def.interfaces && def.interfaces.map(iface => produceInterfaceType(iface)); } - function makeInputValues(values: Array) { + function makeInputValues(values: Array) { return keyValMap( values, value => value.name.value, @@ -401,7 +406,7 @@ export function buildASTSchema(ast: Document): GraphQLSchema { ); } - function makeInterfaceDef(def: InterfaceTypeDefinition) { + function makeInterfaceDef(def: InterfaceTypeDefinitionNode) { const typeName = def.name.value; return new GraphQLInterfaceType({ name: typeName, @@ -411,7 +416,7 @@ export function buildASTSchema(ast: Document): GraphQLSchema { }); } - function makeEnumDef(def: EnumTypeDefinition) { + function makeEnumDef(def: EnumTypeDefinitionNode) { const enumType = new GraphQLEnumType({ name: def.name.value, description: getDescription(def), @@ -428,7 +433,7 @@ export function buildASTSchema(ast: Document): GraphQLSchema { return enumType; } - function makeUnionDef(def: UnionTypeDefinition) { + function makeUnionDef(def: UnionTypeDefinitionNode) { return new GraphQLUnionType({ name: def.name.value, description: getDescription(def), @@ -437,7 +442,7 @@ export function buildASTSchema(ast: Document): GraphQLSchema { }); } - function makeScalarDef(def: ScalarTypeDefinition) { + function makeScalarDef(def: ScalarTypeDefinitionNode) { return new GraphQLScalarType({ name: def.name.value, description: getDescription(def), @@ -451,7 +456,7 @@ export function buildASTSchema(ast: Document): GraphQLSchema { }); } - function makeInputObjectDef(def: InputObjectTypeDefinition) { + function makeInputObjectDef(def: InputObjectTypeDefinitionNode) { return new GraphQLInputObjectType({ name: def.name.value, description: getDescription(def), @@ -460,7 +465,7 @@ export function buildASTSchema(ast: Document): GraphQLSchema { } } -function getDeprecationReason(directives: ?Array): ?string { +function getDeprecationReason(directives: ?Array): ?string { const deprecatedAST = directives && find( directives, directive => directive.name.value === GraphQLDeprecatedDirective.name diff --git a/src/utilities/concatAST.js b/src/utilities/concatAST.js index 578761bded..c14ff22046 100644 --- a/src/utilities/concatAST.js +++ b/src/utilities/concatAST.js @@ -8,7 +8,7 @@ * of patent rights can be found in the PATENTS file in the same directory. */ -import type { Document } from '../language/ast'; +import type { DocumentNode } from '../language/ast'; /** @@ -16,7 +16,7 @@ import type { Document } from '../language/ast'; * concatenate the ASTs together into batched AST, useful for validating many * GraphQL source files which together represent one conceptual application. */ -export function concatAST(asts: Array): Document { +export function concatAST(asts: Array): DocumentNode { const batchDefinitions = []; for (let i = 0; i < asts.length; i++) { const definitions = asts[i].definitions; diff --git a/src/utilities/extendSchema.js b/src/utilities/extendSchema.js index 31824f1c0f..398221d075 100644 --- a/src/utilities/extendSchema.js +++ b/src/utilities/extendSchema.js @@ -78,18 +78,18 @@ import type { } from '../type/directives'; import type { - Document, - InputValueDefinition, - Type, - NamedType, - TypeDefinition, - ObjectTypeDefinition, - InterfaceTypeDefinition, - UnionTypeDefinition, - ScalarTypeDefinition, - EnumTypeDefinition, - InputObjectTypeDefinition, - DirectiveDefinition, + DocumentNode, + InputValueDefinitionNode, + TypeNode, + NamedTypeNode, + TypeDefinitionNode, + ObjectTypeDefinitionNode, + InterfaceTypeDefinitionNode, + UnionTypeDefinitionNode, + ScalarTypeDefinitionNode, + EnumTypeDefinitionNode, + InputObjectTypeDefinitionNode, + DirectiveDefinitionNode, } from '../language/ast'; @@ -107,7 +107,7 @@ import type { */ export function extendSchema( schema: GraphQLSchema, - documentAST: Document + documentAST: DocumentNode ): GraphQLSchema { invariant( schema instanceof GraphQLSchema, @@ -125,7 +125,7 @@ export function extendSchema( // New directives and types are separate because a directives and types can // have the same name. For example, a type named "skip". - const directiveDefinitions : Array = []; + const directiveDefinitions : Array = []; for (let i = 0; i < documentAST.definitions.length; i++) { const def = documentAST.definitions[i]; @@ -258,8 +258,8 @@ export function extendSchema( const existingDirectives = schema.getDirectives(); invariant(existingDirectives, 'schema must have default directives'); - const newDirectives = directiveDefinitions.map(directiveAST => - getDirective(directiveAST) + const newDirectives = directiveDefinitions.map(directiveNode => + getDirective(directiveNode) ); return existingDirectives.concat(newDirectives); } @@ -270,38 +270,38 @@ export function extendSchema( return (type: any); } - function getTypeFromAST(astNode: NamedType): GraphQLNamedType { - const type = _getNamedType(astNode.name.value); + function getTypeFromAST(node: NamedTypeNode): GraphQLNamedType { + const type = _getNamedType(node.name.value); if (!type) { throw new GraphQLError( - `Unknown type: "${astNode.name.value}". Ensure that this type exists ` + + `Unknown type: "${node.name.value}". Ensure that this type exists ` + 'either in the original schema, or is added in a type definition.', - [ astNode ] + [ node ] ); } return type; } - function getObjectTypeFromAST(astNode: NamedType): GraphQLObjectType { - const type = getTypeFromAST(astNode); + function getObjectTypeFromAST(node: NamedTypeNode): GraphQLObjectType { + const type = getTypeFromAST(node); invariant(type instanceof GraphQLObjectType, 'Must be Object type.'); return type; } - function getInterfaceTypeFromAST(astNode: NamedType): GraphQLInterfaceType { - const type = getTypeFromAST(astNode); + function getInterfaceTypeFromAST(node: NamedTypeNode): GraphQLInterfaceType { + const type = getTypeFromAST(node); invariant(type instanceof GraphQLInterfaceType, 'Must be Interface type.'); return type; } - function getInputTypeFromAST(astNode: NamedType): GraphQLInputType { - const type = getTypeFromAST(astNode); + function getInputTypeFromAST(node: NamedTypeNode): GraphQLInputType { + const type = getTypeFromAST(node); invariant(isInputType(type), 'Must be Input type.'); return (type: any); } - function getOutputTypeFromAST(astNode: NamedType): GraphQLOutputType { - const type = getTypeFromAST(astNode); + function getOutputTypeFromAST(node: NamedTypeNode): GraphQLOutputType { + const type = getTypeFromAST(node); invariant(isOutputType(type), 'Must be Output type.'); return (type: any); } @@ -321,9 +321,9 @@ export function extendSchema( return typeDef; } - const typeAST = typeDefinitionMap[typeName]; - if (typeAST) { - const typeDef = buildType(typeAST); + const typeNode = typeDefinitionMap[typeName]; + if (typeNode) { + const typeDef = buildType(typeNode); typeDefCache[typeName] = typeDef; return typeDef; } @@ -449,49 +449,49 @@ export function extendSchema( return getTypeFromDef(typeDef); } - function buildType(typeAST: TypeDefinition): GraphQLNamedType { - switch (typeAST.kind) { - case OBJECT_TYPE_DEFINITION: return buildObjectType(typeAST); - case INTERFACE_TYPE_DEFINITION: return buildInterfaceType(typeAST); - case UNION_TYPE_DEFINITION: return buildUnionType(typeAST); - case SCALAR_TYPE_DEFINITION: return buildScalarType(typeAST); - case ENUM_TYPE_DEFINITION: return buildEnumType(typeAST); - case INPUT_OBJECT_TYPE_DEFINITION: return buildInputObjectType(typeAST); + function buildType(typeNode: TypeDefinitionNode): GraphQLNamedType { + switch (typeNode.kind) { + case OBJECT_TYPE_DEFINITION: return buildObjectType(typeNode); + case INTERFACE_TYPE_DEFINITION: return buildInterfaceType(typeNode); + case UNION_TYPE_DEFINITION: return buildUnionType(typeNode); + case SCALAR_TYPE_DEFINITION: return buildScalarType(typeNode); + case ENUM_TYPE_DEFINITION: return buildEnumType(typeNode); + case INPUT_OBJECT_TYPE_DEFINITION: return buildInputObjectType(typeNode); } - throw new TypeError('Unknown type kind ' + typeAST.kind); + throw new TypeError('Unknown type kind ' + typeNode.kind); } - function buildObjectType(typeAST: ObjectTypeDefinition): GraphQLObjectType { + function buildObjectType(typeNode: ObjectTypeDefinitionNode) { return new GraphQLObjectType({ - name: typeAST.name.value, - description: getDescription(typeAST), - interfaces: () => buildImplementedInterfaces(typeAST), - fields: () => buildFieldMap(typeAST), + name: typeNode.name.value, + description: getDescription(typeNode), + interfaces: () => buildImplementedInterfaces(typeNode), + fields: () => buildFieldMap(typeNode), }); } - function buildInterfaceType(typeAST: InterfaceTypeDefinition) { + function buildInterfaceType(typeNode: InterfaceTypeDefinitionNode) { return new GraphQLInterfaceType({ - name: typeAST.name.value, - description: getDescription(typeAST), - fields: () => buildFieldMap(typeAST), + name: typeNode.name.value, + description: getDescription(typeNode), + fields: () => buildFieldMap(typeNode), resolveType: cannotExecuteExtendedSchema, }); } - function buildUnionType(typeAST: UnionTypeDefinition) { + function buildUnionType(typeNode: UnionTypeDefinitionNode) { return new GraphQLUnionType({ - name: typeAST.name.value, - description: getDescription(typeAST), - types: typeAST.types.map(getObjectTypeFromAST), + name: typeNode.name.value, + description: getDescription(typeNode), + types: typeNode.types.map(getObjectTypeFromAST), resolveType: cannotExecuteExtendedSchema, }); } - function buildScalarType(typeAST: ScalarTypeDefinition) { + function buildScalarType(typeNode: ScalarTypeDefinitionNode) { return new GraphQLScalarType({ - name: typeAST.name.value, - description: getDescription(typeAST), + name: typeNode.name.value, + description: getDescription(typeNode), serialize: id => id, // Note: validation calls the parse functions to determine if a // literal value is correct. Returning null would cause use of custom @@ -502,42 +502,43 @@ export function extendSchema( }); } - function buildEnumType(typeAST: EnumTypeDefinition) { + function buildEnumType(typeNode: EnumTypeDefinitionNode) { return new GraphQLEnumType({ - name: typeAST.name.value, - description: getDescription(typeAST), - values: keyValMap(typeAST.values, v => v.name.value, () => ({})), + name: typeNode.name.value, + description: getDescription(typeNode), + values: keyValMap(typeNode.values, v => v.name.value, () => ({})), }); } - function buildInputObjectType(typeAST: InputObjectTypeDefinition) { + function buildInputObjectType(typeNode: InputObjectTypeDefinitionNode) { return new GraphQLInputObjectType({ - name: typeAST.name.value, - description: getDescription(typeAST), - fields: () => buildInputValues(typeAST.fields), + name: typeNode.name.value, + description: getDescription(typeNode), + fields: () => buildInputValues(typeNode.fields), }); } function getDirective( - directiveAST: DirectiveDefinition + directiveNode: DirectiveDefinitionNode ): GraphQLDirective { return new GraphQLDirective({ - name: directiveAST.name.value, - locations: directiveAST.locations.map( + name: directiveNode.name.value, + locations: directiveNode.locations.map( node => ((node.value: any): DirectiveLocationEnum) ), - args: directiveAST.arguments && buildInputValues(directiveAST.arguments), + args: + directiveNode.arguments && buildInputValues(directiveNode.arguments), }); } - function buildImplementedInterfaces(typeAST: ObjectTypeDefinition) { - return typeAST.interfaces && - typeAST.interfaces.map(getInterfaceTypeFromAST); + function buildImplementedInterfaces(typeNode: ObjectTypeDefinitionNode) { + return typeNode.interfaces && + typeNode.interfaces.map(getInterfaceTypeFromAST); } - function buildFieldMap(typeAST) { + function buildFieldMap(typeNode) { return keyValMap( - typeAST.fields, + typeNode.fields, field => field.name.value, field => ({ type: buildOutputFieldType(field.type), @@ -547,7 +548,7 @@ export function extendSchema( ); } - function buildInputValues(values: Array) { + function buildInputValues(values: Array) { return keyValMap( values, value => value.name.value, @@ -562,28 +563,28 @@ export function extendSchema( ); } - function buildInputFieldType(typeAST: Type): GraphQLInputType { - if (typeAST.kind === LIST_TYPE) { - return new GraphQLList(buildInputFieldType(typeAST.type)); + function buildInputFieldType(typeNode: TypeNode): GraphQLInputType { + if (typeNode.kind === LIST_TYPE) { + return new GraphQLList(buildInputFieldType(typeNode.type)); } - if (typeAST.kind === NON_NULL_TYPE) { - const nullableType = buildInputFieldType(typeAST.type); + if (typeNode.kind === NON_NULL_TYPE) { + const nullableType = buildInputFieldType(typeNode.type); invariant(!(nullableType instanceof GraphQLNonNull), 'Must be nullable'); return new GraphQLNonNull(nullableType); } - return getInputTypeFromAST(typeAST); + return getInputTypeFromAST(typeNode); } - function buildOutputFieldType(typeAST: Type): GraphQLOutputType { - if (typeAST.kind === LIST_TYPE) { - return new GraphQLList(buildOutputFieldType(typeAST.type)); + function buildOutputFieldType(typeNode: TypeNode): GraphQLOutputType { + if (typeNode.kind === LIST_TYPE) { + return new GraphQLList(buildOutputFieldType(typeNode.type)); } - if (typeAST.kind === NON_NULL_TYPE) { - const nullableType = buildOutputFieldType(typeAST.type); + if (typeNode.kind === NON_NULL_TYPE) { + const nullableType = buildOutputFieldType(typeNode.type); invariant(!(nullableType instanceof GraphQLNonNull), 'Must be nullable'); return new GraphQLNonNull(nullableType); } - return getOutputTypeFromAST(typeAST); + return getOutputTypeFromAST(typeNode); } } diff --git a/src/utilities/getOperationAST.js b/src/utilities/getOperationAST.js index c7e38d598f..b065b3fe68 100644 --- a/src/utilities/getOperationAST.js +++ b/src/utilities/getOperationAST.js @@ -9,7 +9,7 @@ */ import { OPERATION_DEFINITION } from '../language/kinds'; -import type { Document, OperationDefinition } from '../language/ast'; +import type { DocumentNode, OperationDefinitionNode } from '../language/ast'; /** @@ -18,9 +18,9 @@ import type { Document, OperationDefinition } from '../language/ast'; * provided in the document. */ export function getOperationAST( - documentAST: Document, + documentAST: DocumentNode, operationName: ?string -): ?OperationDefinition { +): ?OperationDefinitionNode { let operation = null; for (let i = 0; i < documentAST.definitions.length; i++) { const definition = documentAST.definitions[i]; diff --git a/src/utilities/isValidLiteralValue.js b/src/utilities/isValidLiteralValue.js index 64ad7e4bc2..0ace788110 100644 --- a/src/utilities/isValidLiteralValue.js +++ b/src/utilities/isValidLiteralValue.js @@ -9,7 +9,11 @@ */ import { print } from '../language/printer'; -import type { Value, ListValue, ObjectValue } from '../language/ast'; +import type { + ValueNode, + ListValueNode, + ObjectValueNode +} from '../language/ast'; import { NULL, VARIABLE, @@ -30,51 +34,51 @@ import isNullish from '../jsutils/isNullish'; /** - * Utility for validators which determines if a value literal AST is valid given - * an input type. + * Utility for validators which determines if a value literal node is valid + * given an input type. * * Note that this only validates literal values, variables are assumed to * provide values of the correct type. */ export function isValidLiteralValue( type: GraphQLInputType, - valueAST: Value + valueNode: ValueNode ): Array { // A value must be provided if the type is non-null. if (type instanceof GraphQLNonNull) { - if (!valueAST || (valueAST.kind === NULL)) { + if (!valueNode || (valueNode.kind === NULL)) { return [ `Expected "${String(type)}", found null.` ]; } - return isValidLiteralValue(type.ofType, valueAST); + return isValidLiteralValue(type.ofType, valueNode); } - if (!valueAST || (valueAST.kind === NULL)) { + if (!valueNode || (valueNode.kind === NULL)) { return []; } // This function only tests literals, and assumes variables will provide // values of the correct type. - if (valueAST.kind === VARIABLE) { + if (valueNode.kind === VARIABLE) { return []; } // Lists accept a non-list value as a list of one. if (type instanceof GraphQLList) { const itemType = type.ofType; - if (valueAST.kind === LIST) { - return (valueAST: ListValue).values.reduce((acc, itemAST, index) => { - const errors = isValidLiteralValue(itemType, itemAST); + if (valueNode.kind === LIST) { + return (valueNode: ListValueNode).values.reduce((acc, item, index) => { + const errors = isValidLiteralValue(itemType, item); return acc.concat(errors.map(error => `In element #${index}: ${error}` )); }, []); } - return isValidLiteralValue(itemType, valueAST); + return isValidLiteralValue(itemType, valueNode); } // Input objects check each defined field and look for undefined fields. if (type instanceof GraphQLInputObjectType) { - if (valueAST.kind !== OBJECT) { + if (valueNode.kind !== OBJECT) { return [ `Expected "${type.name}", found not an object.` ]; } const fields = type.getFields(); @@ -82,21 +86,21 @@ export function isValidLiteralValue( const errors = []; // Ensure every provided field is defined. - const fieldASTs = (valueAST: ObjectValue).fields; - fieldASTs.forEach(providedFieldAST => { - if (!fields[providedFieldAST.name.value]) { + const fieldNodes = (valueNode: ObjectValueNode).fields; + fieldNodes.forEach(providedFieldNode => { + if (!fields[providedFieldNode.name.value]) { errors.push( - `In field "${providedFieldAST.name.value}": Unknown field.` + `In field "${providedFieldNode.name.value}": Unknown field.` ); } }); // Ensure every defined field is valid. - const fieldASTMap = keyMap(fieldASTs, fieldAST => fieldAST.name.value); + const fieldNodeMap = keyMap(fieldNodes, fieldNode => fieldNode.name.value); Object.keys(fields).forEach(fieldName => { const result = isValidLiteralValue( fields[fieldName].type, - fieldASTMap[fieldName] && fieldASTMap[fieldName].value + fieldNodeMap[fieldName] && fieldNodeMap[fieldName].value ); errors.push(...(result.map(error => `In field "${fieldName}": ${error}` @@ -113,9 +117,9 @@ export function isValidLiteralValue( // Scalar/Enum input checks to ensure the type can parse the value to // a non-null value. - const parseResult = type.parseLiteral(valueAST); + const parseResult = type.parseLiteral(valueNode); if (isNullish(parseResult)) { - return [ `Expected type "${type.name}", found ${print(valueAST)}.` ]; + return [ `Expected type "${type.name}", found ${print(valueNode)}.` ]; } return []; diff --git a/src/utilities/separateOperations.js b/src/utilities/separateOperations.js index 937a3fcb21..9ccfd5ff12 100644 --- a/src/utilities/separateOperations.js +++ b/src/utilities/separateOperations.js @@ -10,8 +10,8 @@ import { visit } from '../language/visitor'; import type { - Document, - OperationDefinition, + DocumentNode, + OperationDefinitionNode, } from '../language/ast'; /** @@ -21,8 +21,8 @@ import type { * refers to. */ export function separateOperations( - documentAST: Document -): { [operationName: string]: Document } { + documentAST: DocumentNode +): { [operationName: string]: DocumentNode } { const operations = []; const depGraph: DepGraph = Object.create(null); @@ -67,7 +67,7 @@ export function separateOperations( type DepGraph = {[from: string]: {[to: string]: boolean}}; // Provides the empty string for anonymous operations. -function opName(operation: OperationDefinition): string { +function opName(operation: OperationDefinitionNode): string { return operation.name ? operation.name.value : ''; } diff --git a/src/utilities/typeFromAST.js b/src/utilities/typeFromAST.js index 768a37760c..b0df5b476d 100644 --- a/src/utilities/typeFromAST.js +++ b/src/utilities/typeFromAST.js @@ -10,7 +10,7 @@ import invariant from '../jsutils/invariant'; import { NAMED_TYPE, LIST_TYPE, NON_NULL_TYPE } from '../language/kinds'; -import type { Type } from '../language/ast'; +import type { TypeNode } from '../language/ast'; import { GraphQLList, GraphQLNonNull } from '../type/definition'; import type { GraphQLType, GraphQLNullableType } from '../type/definition'; import type { GraphQLSchema } from '../type/schema'; @@ -18,19 +18,19 @@ import type { GraphQLSchema } from '../type/schema'; export function typeFromAST( schema: GraphQLSchema, - inputTypeAST: Type + typeNode: TypeNode ): ?GraphQLType { let innerType; - if (inputTypeAST.kind === LIST_TYPE) { - innerType = typeFromAST(schema, inputTypeAST.type); + if (typeNode.kind === LIST_TYPE) { + innerType = typeFromAST(schema, typeNode.type); return innerType && new GraphQLList(innerType); } - if (inputTypeAST.kind === NON_NULL_TYPE) { - innerType = typeFromAST(schema, inputTypeAST.type); + if (typeNode.kind === NON_NULL_TYPE) { + innerType = typeFromAST(schema, typeNode.type); return innerType && new GraphQLNonNull( ((innerType: any): GraphQLNullableType) ); } - invariant(inputTypeAST.kind === NAMED_TYPE, 'Must be a named type.'); - return schema.getType(inputTypeAST.name.value); + invariant(typeNode.kind === NAMED_TYPE, 'Must be a named type.'); + return schema.getType(typeNode.name.value); } diff --git a/src/utilities/valueFromAST.js b/src/utilities/valueFromAST.js index 73ee90ef8c..7524386578 100644 --- a/src/utilities/valueFromAST.js +++ b/src/utilities/valueFromAST.js @@ -22,10 +22,10 @@ import { } from '../type/definition'; import type { GraphQLInputType } from '../type/definition'; import type { - Value, - Variable, - ListValue, - ObjectValue + ValueNode, + VariableNode, + ListValueNode, + ObjectValueNode, } from '../language/ast'; @@ -50,30 +50,30 @@ import type { * */ export function valueFromAST( - valueAST: ?Value, + valueNode: ?ValueNode, type: GraphQLInputType, variables?: ?{ [key: string]: mixed } ): mixed | void { - if (!valueAST) { - // When there is no AST, then there is also no value. + if (!valueNode) { + // When there is no node, then there is also no value. // Importantly, this is different from returning the value null. return; } if (type instanceof GraphQLNonNull) { - if (valueAST.kind === Kind.NULL) { + if (valueNode.kind === Kind.NULL) { return; // Invalid: intentionally return no value. } - return valueFromAST(valueAST, type.ofType, variables); + return valueFromAST(valueNode, type.ofType, variables); } - if (valueAST.kind === Kind.NULL) { + if (valueNode.kind === Kind.NULL) { // This is explicitly returning the value null. return null; } - if (valueAST.kind === Kind.VARIABLE) { - const variableName = (valueAST: Variable).name.value; + if (valueNode.kind === Kind.VARIABLE) { + const variableName = (valueNode: VariableNode).name.value; if (!variables || isInvalid(variables[variableName])) { // No valid return value. return; @@ -86,11 +86,11 @@ export function valueFromAST( if (type instanceof GraphQLList) { const itemType = type.ofType; - if (valueAST.kind === Kind.LIST) { + if (valueNode.kind === Kind.LIST) { const coercedValues = []; - const itemASTs = (valueAST: ListValue).values; - for (let i = 0; i < itemASTs.length; i++) { - if (isMissingVariable(itemASTs[i], variables)) { + const itemNodes = (valueNode: ListValueNode).values; + for (let i = 0; i < itemNodes.length; i++) { + if (isMissingVariable(itemNodes[i], variables)) { // If an array contains a missing variable, it is either coerced to // null or if the item type is non-null, it considered invalid. if (itemType instanceof GraphQLNonNull) { @@ -98,7 +98,7 @@ export function valueFromAST( } coercedValues.push(null); } else { - const itemValue = valueFromAST(itemASTs[i], itemType, variables); + const itemValue = valueFromAST(itemNodes[i], itemType, variables); if (isInvalid(itemValue)) { return; // Invalid: intentionally return no value. } @@ -107,7 +107,7 @@ export function valueFromAST( } return coercedValues; } - const coercedValue = valueFromAST(valueAST, itemType, variables); + const coercedValue = valueFromAST(valueNode, itemType, variables); if (isInvalid(coercedValue)) { return; // Invalid: intentionally return no value. } @@ -115,21 +115,21 @@ export function valueFromAST( } if (type instanceof GraphQLInputObjectType) { - if (valueAST.kind !== Kind.OBJECT) { + if (valueNode.kind !== Kind.OBJECT) { return; // Invalid: intentionally return no value. } const coercedObj = Object.create(null); const fields = type.getFields(); - const fieldASTs = keyMap( - (valueAST: ObjectValue).fields, + const fieldNodes = keyMap( + (valueNode: ObjectValueNode).fields, field => field.name.value ); const fieldNames = Object.keys(fields); for (let i = 0; i < fieldNames.length; i++) { const fieldName = fieldNames[i]; const field = fields[fieldName]; - const fieldAST = fieldASTs[fieldName]; - if (!fieldAST || isMissingVariable(fieldAST.value, variables)) { + const fieldNode = fieldNodes[fieldName]; + if (!fieldNode || isMissingVariable(fieldNode.value, variables)) { if (!isInvalid(field.defaultValue)) { coercedObj[fieldName] = field.defaultValue; } else if (field.type instanceof GraphQLNonNull) { @@ -137,7 +137,7 @@ export function valueFromAST( } continue; } - const fieldValue = valueFromAST(fieldAST.value, field.type, variables); + const fieldValue = valueFromAST(fieldNode.value, field.type, variables); if (isInvalid(fieldValue)) { return; // Invalid: intentionally return no value. } @@ -151,7 +151,7 @@ export function valueFromAST( 'Must be input type' ); - const parsed = type.parseLiteral(valueAST); + const parsed = type.parseLiteral(valueNode); if (isNullish(parsed)) { // null or invalid values represent a failure to parse correctly, // in which case no value is returned. @@ -161,9 +161,9 @@ export function valueFromAST( return parsed; } -// Returns true if the provided valueAST is a variable which is not defined +// Returns true if the provided valueNode is a variable which is not defined // in the set of variables. -function isMissingVariable(valueAST, variables) { - return valueAST.kind === Kind.VARIABLE && - (!variables || isInvalid(variables[(valueAST: Variable).name.value])); +function isMissingVariable(valueNode, variables) { + return valueNode.kind === Kind.VARIABLE && + (!variables || isInvalid(variables[(valueNode: VariableNode).name.value])); } diff --git a/src/validation/rules/ArgumentsOfCorrectType.js b/src/validation/rules/ArgumentsOfCorrectType.js index 32d626534e..58fa7e7031 100644 --- a/src/validation/rules/ArgumentsOfCorrectType.js +++ b/src/validation/rules/ArgumentsOfCorrectType.js @@ -35,19 +35,19 @@ export function badValueMessage( */ export function ArgumentsOfCorrectType(context: ValidationContext): any { return { - Argument(argAST) { + Argument(node) { const argDef = context.getArgument(); if (argDef) { - const errors = isValidLiteralValue(argDef.type, argAST.value); + const errors = isValidLiteralValue(argDef.type, node.value); if (errors && errors.length > 0) { context.reportError(new GraphQLError( badValueMessage( - argAST.name.value, + node.name.value, argDef.type, - print(argAST.value), + print(node.value), errors ), - [ argAST.value ] + [ node.value ] )); } } diff --git a/src/validation/rules/DefaultValuesOfCorrectType.js b/src/validation/rules/DefaultValuesOfCorrectType.js index 7522a11b7a..3931434cbb 100644 --- a/src/validation/rules/DefaultValuesOfCorrectType.js +++ b/src/validation/rules/DefaultValuesOfCorrectType.js @@ -45,9 +45,9 @@ export function badValueForDefaultArgMessage( */ export function DefaultValuesOfCorrectType(context: ValidationContext): any { return { - VariableDefinition(varDefAST) { - const name = varDefAST.variable.name.value; - const defaultValue = varDefAST.defaultValue; + VariableDefinition(node) { + const name = node.variable.name.value; + const defaultValue = node.defaultValue; const type = context.getInputType(); if (type instanceof GraphQLNonNull && defaultValue) { context.reportError(new GraphQLError( diff --git a/src/validation/rules/FieldsOnCorrectType.js b/src/validation/rules/FieldsOnCorrectType.js index 7cf78145e4..9eb4faf944 100644 --- a/src/validation/rules/FieldsOnCorrectType.js +++ b/src/validation/rules/FieldsOnCorrectType.js @@ -12,7 +12,7 @@ import type { ValidationContext } from '../index'; import { GraphQLError } from '../../error'; import suggestionList from '../../jsutils/suggestionList'; import quotedOrList from '../../jsutils/quotedOrList'; -import type { Field } from '../../language/ast'; +import type { FieldNode } from '../../language/ast'; import type { GraphQLSchema } from '../../type/schema'; import type { GraphQLOutputType } from '../../type/definition'; import { @@ -46,7 +46,7 @@ export function undefinedFieldMessage( */ export function FieldsOnCorrectType(context: ValidationContext): any { return { - Field(node: Field) { + Field(node: FieldNode) { const type = context.getParentType(); if (type) { const fieldDef = context.getFieldDef(); diff --git a/src/validation/rules/NoFragmentCycles.js b/src/validation/rules/NoFragmentCycles.js index 683b2ab5db..637ec289da 100644 --- a/src/validation/rules/NoFragmentCycles.js +++ b/src/validation/rules/NoFragmentCycles.js @@ -10,7 +10,7 @@ import type { ValidationContext } from '../index'; import { GraphQLError } from '../../error'; -import type { FragmentDefinition } from '../../language/ast'; +import type { FragmentDefinitionNode } from '../../language/ast'; export function cycleErrorMessage( @@ -45,7 +45,7 @@ export function NoFragmentCycles(context: ValidationContext): any { // This does a straight-forward DFS to find cycles. // It does not terminate when a cycle was found but continues to explore // the graph to find all possible cycles. - function detectCycleRecursive(fragment: FragmentDefinition) { + function detectCycleRecursive(fragment: FragmentDefinitionNode) { const fragmentName = fragment.name.value; visitedFrags[fragmentName] = true; diff --git a/src/validation/rules/NoUndefinedVariables.js b/src/validation/rules/NoUndefinedVariables.js index 353e253130..69dbdb48d1 100644 --- a/src/validation/rules/NoUndefinedVariables.js +++ b/src/validation/rules/NoUndefinedVariables.js @@ -49,8 +49,8 @@ export function NoUndefinedVariables(context: ValidationContext): any { }); } }, - VariableDefinition(varDefAST) { - variableNameDefined[varDefAST.variable.name.value] = true; + VariableDefinition(node) { + variableNameDefined[node.variable.name.value] = true; } }; } diff --git a/src/validation/rules/OverlappingFieldsCanBeMerged.js b/src/validation/rules/OverlappingFieldsCanBeMerged.js index 7c73e6bbce..89d9e6e4ec 100644 --- a/src/validation/rules/OverlappingFieldsCanBeMerged.js +++ b/src/validation/rules/OverlappingFieldsCanBeMerged.js @@ -12,10 +12,10 @@ import type { ValidationContext } from '../index'; import { GraphQLError } from '../../error'; import find from '../../jsutils/find'; import type { - SelectionSet, - Field, - Argument, - FragmentDefinition, + SelectionSetNode, + FieldNode, + ArgumentNode, + FragmentDefinitionNode, } from '../../language/ast'; import { FIELD, INLINE_FRAGMENT, FRAGMENT_SPREAD } from '../../language/kinds'; import { print } from '../../language/printer'; @@ -92,15 +92,15 @@ export function OverlappingFieldsCanBeMerged(context: ValidationContext): any { }; } -type Conflict = [ ConflictReason, Array, Array ]; +type Conflict = [ ConflictReason, Array, Array ]; // Field name and reason. type ConflictReason = [ string, ConflictReasonMessage ]; // Reason is a string, or a nested list of conflicts. type ConflictReasonMessage = string | Array; -// Tuple defining an AST in a context -type AstAndDef = [ GraphQLCompositeType, Field, ?GraphQLField ]; +// Tuple defining a field node in a context. +type NodeAndDef = [ GraphQLCompositeType, FieldNode, ?GraphQLField ]; // Map of array of those. -type AstAndDefCollection = { [key: string]: Array }; +type NodeAndDefCollection = { [key: string]: Array }; /** * Algorithm: @@ -165,7 +165,7 @@ function findConflictsWithinSelectionSet( cachedFieldsAndFragmentNames, comparedFragments: PairSet, parentType: ?GraphQLNamedType, - selectionSet: SelectionSet + selectionSet: SelectionSetNode ): Array { const conflicts = []; @@ -225,7 +225,7 @@ function collectConflictsBetweenFieldsAndFragment( cachedFieldsAndFragmentNames, comparedFragments: PairSet, areMutuallyExclusive: boolean, - fieldMap: AstAndDefCollection, + fieldMap: NodeAndDefCollection, fragmentName: string ): void { const fragment = context.getFragment(fragmentName); @@ -357,9 +357,9 @@ function findConflictsBetweenSubSelectionSets( comparedFragments: PairSet, areMutuallyExclusive: boolean, parentType1: ?GraphQLNamedType, - selectionSet1: SelectionSet, + selectionSet1: SelectionSetNode, parentType2: ?GraphQLNamedType, - selectionSet2: SelectionSet + selectionSet2: SelectionSetNode ): Array { const conflicts = []; @@ -440,7 +440,7 @@ function collectConflictsWithin( conflicts: Array, cachedFieldsAndFragmentNames, comparedFragments: PairSet, - fieldMap: AstAndDefCollection + fieldMap: NodeAndDefCollection ): void { // A field map is a keyed collection, where each key represents a response // name and the value at that key is a list of all fields which provide that @@ -483,8 +483,8 @@ function collectConflictsBetween( cachedFieldsAndFragmentNames, comparedFragments: PairSet, parentFieldsAreMutuallyExclusive: boolean, - fieldMap1: AstAndDefCollection, - fieldMap2: AstAndDefCollection + fieldMap1: NodeAndDefCollection, + fieldMap2: NodeAndDefCollection ): void { // A field map is a keyed collection, where each key represents a response // name and the value at that key is a list of all fields which provide that @@ -523,11 +523,11 @@ function findConflict( comparedFragments: PairSet, parentFieldsAreMutuallyExclusive: boolean, responseName: string, - field1: AstAndDef, - field2: AstAndDef + field1: NodeAndDef, + field2: NodeAndDef ): ?Conflict { - const [ parentType1, ast1, def1 ] = field1; - const [ parentType2, ast2, def2 ] = field2; + const [ parentType1, node1, def1 ] = field1; + const [ parentType2, node2, def2 ] = field2; // If it is known that two fields could not possibly apply at the same // time, due to the parent types, then it is safe to permit them to diverge @@ -549,22 +549,22 @@ function findConflict( if (!areMutuallyExclusive) { // Two aliases must refer to the same field. - const name1 = ast1.name.value; - const name2 = ast2.name.value; + const name1 = node1.name.value; + const name2 = node2.name.value; if (name1 !== name2) { return [ [ responseName, `${name1} and ${name2} are different fields` ], - [ ast1 ], - [ ast2 ] + [ node1 ], + [ node2 ] ]; } // Two field calls must have the same arguments. - if (!sameArguments(ast1.arguments || [], ast2.arguments || [])) { + if (!sameArguments(node1.arguments || [], node2.arguments || [])) { return [ [ responseName, 'they have differing arguments' ], - [ ast1 ], - [ ast2 ] + [ node1 ], + [ node2 ] ]; } } @@ -573,16 +573,16 @@ function findConflict( return [ [ responseName, `they return conflicting types ${String(type1)} and ${String(type2)}` ], - [ ast1 ], - [ ast2 ] + [ node1 ], + [ node2 ] ]; } // Collect and compare sub-fields. Use the same "visited fragment names" list // for both collections so fields in a fragment reference are never // compared to themselves. - const selectionSet1 = ast1.selectionSet; - const selectionSet2 = ast2.selectionSet; + const selectionSet1 = node1.selectionSet; + const selectionSet2 = node2.selectionSet; if (selectionSet1 && selectionSet2) { const conflicts = findConflictsBetweenSubSelectionSets( context, @@ -594,13 +594,13 @@ function findConflict( getNamedType(type2), selectionSet2 ); - return subfieldConflicts(conflicts, responseName, ast1, ast2); + return subfieldConflicts(conflicts, responseName, node1, node2); } } function sameArguments( - arguments1: Array, - arguments2: Array + arguments1: Array, + arguments2: Array ): boolean { if (arguments1.length !== arguments2.length) { return false; @@ -655,26 +655,26 @@ function doTypesConflict( } // Given a selection set, return the collection of fields (a mapping of response -// name to field ASTs and definitions) as well as a list of fragment names +// name to field nodes and definitions) as well as a list of fragment names // referenced via fragment spreads. function getFieldsAndFragmentNames( context: ValidationContext, cachedFieldsAndFragmentNames, parentType: ?GraphQLNamedType, - selectionSet: SelectionSet -): [ AstAndDefCollection, Array ] { + selectionSet: SelectionSetNode +): [ NodeAndDefCollection, Array ] { let cached = cachedFieldsAndFragmentNames.get(selectionSet); if (!cached) { - const astAndDefs = {}; + const nodeAndDefs = {}; const fragmentNames = {}; _collectFieldsAndFragmentNames( context, parentType, selectionSet, - astAndDefs, + nodeAndDefs, fragmentNames ); - cached = [ astAndDefs, Object.keys(fragmentNames) ]; + cached = [ nodeAndDefs, Object.keys(fragmentNames) ]; cachedFieldsAndFragmentNames.set(selectionSet, cached); } return cached; @@ -685,9 +685,9 @@ function getFieldsAndFragmentNames( function getReferencedFieldsAndFragmentNames( context: ValidationContext, cachedFieldsAndFragmentNames, - fragment: FragmentDefinition + fragment: FragmentDefinitionNode ) { - // Short-circuit building a type from the AST if possible. + // Short-circuit building a type from the node if possible. const cached = cachedFieldsAndFragmentNames.get(fragment.selectionSet); if (cached) { return cached; @@ -705,8 +705,8 @@ function getReferencedFieldsAndFragmentNames( function _collectFieldsAndFragmentNames( context: ValidationContext, parentType: ?GraphQLNamedType, - selectionSet: SelectionSet, - astAndDefs, + selectionSet: SelectionSetNode, + nodeAndDefs, fragmentNames ): void { for (let i = 0; i < selectionSet.selections.length; i++) { @@ -721,10 +721,10 @@ function _collectFieldsAndFragmentNames( } const responseName = selection.alias ? selection.alias.value : fieldName; - if (!astAndDefs[responseName]) { - astAndDefs[responseName] = []; + if (!nodeAndDefs[responseName]) { + nodeAndDefs[responseName] = []; } - astAndDefs[responseName].push([ parentType, selection, fieldDef ]); + nodeAndDefs[responseName].push([ parentType, selection, fieldDef ]); break; case FRAGMENT_SPREAD: fragmentNames[selection.name.value] = true; @@ -738,7 +738,7 @@ function _collectFieldsAndFragmentNames( context, ((inlineFragmentType: any): GraphQLNamedType), selection.selectionSet, - astAndDefs, + nodeAndDefs, fragmentNames ); break; @@ -751,19 +751,19 @@ function _collectFieldsAndFragmentNames( function subfieldConflicts( conflicts: Array, responseName: string, - ast1: Field, - ast2: Field + node1: FieldNode, + node2: FieldNode ): ?Conflict { if (conflicts.length > 0) { return [ [ responseName, conflicts.map(([ reason ]) => reason) ], conflicts.reduce( (allFields, [ , fields1 ]) => allFields.concat(fields1), - [ ast1 ] + [ node1 ] ), conflicts.reduce( (allFields, [ , , fields2 ]) => allFields.concat(fields2), - [ ast2 ] + [ node2 ] ) ]; } diff --git a/src/validation/rules/ProvidedNonNullArguments.js b/src/validation/rules/ProvidedNonNullArguments.js index fb952a6708..7525581f90 100644 --- a/src/validation/rules/ProvidedNonNullArguments.js +++ b/src/validation/rules/ProvidedNonNullArguments.js @@ -43,24 +43,24 @@ export function ProvidedNonNullArguments(context: ValidationContext): any { return { Field: { // Validate on leave to allow for deeper errors to appear first. - leave(fieldAST) { + leave(node) { const fieldDef = context.getFieldDef(); if (!fieldDef) { return false; } - const argASTs = fieldAST.arguments || []; + const argNodes = node.arguments || []; - const argASTMap = keyMap(argASTs, arg => arg.name.value); + const argNodeMap = keyMap(argNodes, arg => arg.name.value); fieldDef.args.forEach(argDef => { - const argAST = argASTMap[argDef.name]; - if (!argAST && argDef.type instanceof GraphQLNonNull) { + const argNode = argNodeMap[argDef.name]; + if (!argNode && argDef.type instanceof GraphQLNonNull) { context.reportError(new GraphQLError( missingFieldArgMessage( - fieldAST.name.value, + node.name.value, argDef.name, argDef.type ), - [ fieldAST ] + [ node ] )); } }); @@ -69,24 +69,24 @@ export function ProvidedNonNullArguments(context: ValidationContext): any { Directive: { // Validate on leave to allow for deeper errors to appear first. - leave(directiveAST) { + leave(node) { const directiveDef = context.getDirective(); if (!directiveDef) { return false; } - const argASTs = directiveAST.arguments || []; + const argNodes = node.arguments || []; - const argASTMap = keyMap(argASTs, arg => arg.name.value); + const argNodeMap = keyMap(argNodes, arg => arg.name.value); directiveDef.args.forEach(argDef => { - const argAST = argASTMap[argDef.name]; - if (!argAST && argDef.type instanceof GraphQLNonNull) { + const argNode = argNodeMap[argDef.name]; + if (!argNode && argDef.type instanceof GraphQLNonNull) { context.reportError(new GraphQLError( missingDirectiveArgMessage( - directiveAST.name.value, + node.name.value, argDef.name, argDef.type ), - [ directiveAST ] + [ node ] )); } }); diff --git a/src/validation/rules/ScalarLeafs.js b/src/validation/rules/ScalarLeafs.js index 2a468bf1a5..48a90d8b34 100644 --- a/src/validation/rules/ScalarLeafs.js +++ b/src/validation/rules/ScalarLeafs.js @@ -10,7 +10,7 @@ import type { ValidationContext } from '../index'; import { GraphQLError } from '../../error'; -import type { Field } from '../../language/ast'; +import type { FieldNode } from '../../language/ast'; import { isLeafType } from '../../type/definition'; import type { GraphQLType } from '../../type/definition'; @@ -39,7 +39,7 @@ export function requiredSubselectionMessage( */ export function ScalarLeafs(context: ValidationContext): any { return { - Field(node: Field) { + Field(node: FieldNode) { const type = context.getType(); if (type) { if (isLeafType(type)) { diff --git a/src/validation/rules/UniqueVariableNames.js b/src/validation/rules/UniqueVariableNames.js index 607b558150..808415f396 100644 --- a/src/validation/rules/UniqueVariableNames.js +++ b/src/validation/rules/UniqueVariableNames.js @@ -9,7 +9,7 @@ */ import type { ValidationContext } from '../index'; -import type { VariableDefinition } from '../../language/ast'; +import type { VariableDefinitionNode } from '../../language/ast'; import { GraphQLError } from '../../error'; @@ -28,7 +28,7 @@ export function UniqueVariableNames(context: ValidationContext): any { OperationDefinition() { knownVariableNames = Object.create(null); }, - VariableDefinition(node: VariableDefinition) { + VariableDefinition(node: VariableDefinitionNode) { const variableName = node.variable.name.value; if (knownVariableNames[variableName]) { context.reportError(new GraphQLError( diff --git a/src/validation/rules/VariablesAreInputTypes.js b/src/validation/rules/VariablesAreInputTypes.js index 94bebd9f2f..6cfd7a192a 100644 --- a/src/validation/rules/VariablesAreInputTypes.js +++ b/src/validation/rules/VariablesAreInputTypes.js @@ -10,7 +10,7 @@ import type { ValidationContext } from '../index'; import { GraphQLError } from '../../error'; -import type { VariableDefinition } from '../../language/ast'; +import type { VariableDefinitionNode } from '../../language/ast'; import { print } from '../../language/printer'; import { isInputType } from '../../type/definition'; import { typeFromAST } from '../../utilities/typeFromAST'; @@ -31,7 +31,7 @@ export function nonInputTypeOnVarMessage( */ export function VariablesAreInputTypes(context: ValidationContext): any { return { - VariableDefinition(node: VariableDefinition): ?GraphQLError { + VariableDefinition(node: VariableDefinitionNode): ?GraphQLError { const type = typeFromAST(context.getSchema(), node.type); // If the variable type is not an input type, return an error. diff --git a/src/validation/rules/VariablesInAllowedPosition.js b/src/validation/rules/VariablesInAllowedPosition.js index be03ea8133..45fa3d6d3e 100644 --- a/src/validation/rules/VariablesInAllowedPosition.js +++ b/src/validation/rules/VariablesInAllowedPosition.js @@ -63,8 +63,8 @@ export function VariablesInAllowedPosition(context: ValidationContext): any { }); } }, - VariableDefinition(varDefAST) { - varDefMap[varDefAST.variable.name.value] = varDefAST; + VariableDefinition(node) { + varDefMap[node.variable.name.value] = node; } }; } diff --git a/src/validation/validate.js b/src/validation/validate.js index 96bbb64c07..d85bbbcda7 100644 --- a/src/validation/validate.js +++ b/src/validation/validate.js @@ -13,12 +13,12 @@ import { GraphQLError } from '../error'; import { visit, visitInParallel, visitWithTypeInfo } from '../language/visitor'; import * as Kind from '../language/kinds'; import type { - Document, - OperationDefinition, - Variable, - SelectionSet, - FragmentSpread, - FragmentDefinition, + DocumentNode, + OperationDefinitionNode, + VariableNode, + SelectionSetNode, + FragmentSpreadNode, + FragmentDefinitionNode, } from '../language/ast'; import { GraphQLSchema } from '../type/schema'; import type { @@ -48,7 +48,7 @@ import { specifiedRules } from './specifiedRules'; */ export function validate( schema: GraphQLSchema, - ast: Document, + ast: DocumentNode, rules?: Array ): Array { invariant(schema, 'Must provide schema'); @@ -71,7 +71,7 @@ export function validate( export function visitUsingRules( schema: GraphQLSchema, typeInfo: TypeInfo, - documentAST: Document, + documentAST: DocumentNode, rules: Array ): Array { const context = new ValidationContext(schema, documentAST, typeInfo); @@ -81,8 +81,8 @@ export function visitUsingRules( return context.getErrors(); } -type HasSelectionSet = OperationDefinition | FragmentDefinition; -type VariableUsage = { node: Variable, type: ?GraphQLInputType }; +type NodeWithSelectionSet = OperationDefinitionNode | FragmentDefinitionNode; +type VariableUsage = { node: VariableNode, type: ?GraphQLInputType }; /** * An instance of this class is passed as the "this" context to all validators, @@ -91,19 +91,19 @@ type VariableUsage = { node: Variable, type: ?GraphQLInputType }; */ export class ValidationContext { _schema: GraphQLSchema; - _ast: Document; + _ast: DocumentNode; _typeInfo: TypeInfo; _errors: Array; - _fragments: {[name: string]: FragmentDefinition}; - _fragmentSpreads: Map>; + _fragments: {[name: string]: FragmentDefinitionNode}; + _fragmentSpreads: Map>; _recursivelyReferencedFragments: Map< - OperationDefinition, - Array + OperationDefinitionNode, + Array >; - _variableUsages: Map>; - _recursiveVariableUsages: Map>; + _variableUsages: Map>; + _recursiveVariableUsages: Map>; - constructor(schema: GraphQLSchema, ast: Document, typeInfo: TypeInfo) { + constructor(schema: GraphQLSchema, ast: DocumentNode, typeInfo: TypeInfo) { this._schema = schema; this._ast = ast; this._typeInfo = typeInfo; @@ -126,11 +126,11 @@ export class ValidationContext { return this._schema; } - getDocument(): Document { + getDocument(): DocumentNode { return this._ast; } - getFragment(name: string): ?FragmentDefinition { + getFragment(name: string): ?FragmentDefinitionNode { let fragments = this._fragments; if (!fragments) { this._fragments = fragments = @@ -144,11 +144,11 @@ export class ValidationContext { return fragments[name]; } - getFragmentSpreads(node: SelectionSet): Array { + getFragmentSpreads(node: SelectionSetNode): Array { let spreads = this._fragmentSpreads.get(node); if (!spreads) { spreads = []; - const setsToVisit: Array = [ node ]; + const setsToVisit: Array = [ node ]; while (setsToVisit.length !== 0) { const set = setsToVisit.pop(); for (let i = 0; i < set.selections.length; i++) { @@ -166,13 +166,13 @@ export class ValidationContext { } getRecursivelyReferencedFragments( - operation: OperationDefinition - ): Array { + operation: OperationDefinitionNode + ): Array { let fragments = this._recursivelyReferencedFragments.get(operation); if (!fragments) { fragments = []; const collectedNames = Object.create(null); - const nodesToVisit: Array = [ operation.selectionSet ]; + const nodesToVisit: Array = [ operation.selectionSet ]; while (nodesToVisit.length !== 0) { const node = nodesToVisit.pop(); const spreads = this.getFragmentSpreads(node); @@ -193,7 +193,7 @@ export class ValidationContext { return fragments; } - getVariableUsages(node: HasSelectionSet): Array { + getVariableUsages(node: NodeWithSelectionSet): Array { let usages = this._variableUsages.get(node); if (!usages) { const newUsages = []; @@ -211,7 +211,7 @@ export class ValidationContext { } getRecursiveVariableUsages( - operation: OperationDefinition + operation: OperationDefinitionNode ): Array { let usages = this._recursiveVariableUsages.get(operation); if (!usages) {