Skip to content

Commit

Permalink
language: change OperationTypeNode to enum (#3312)
Browse files Browse the repository at this point in the history
Co-authored-by: Ivan Goncharov <[email protected]>
  • Loading branch information
lekoaf and IvanGoncharov authored Oct 13, 2021
1 parent d48d502 commit e289e64
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
7 changes: 4 additions & 3 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
FieldNode,
FragmentDefinitionNode,
} from '../language/ast';
import { OperationTypeNode } from '../language/ast';
import { Kind } from '../language/kinds';

import type { GraphQLSchema } from '../type/schema';
Expand Down Expand Up @@ -366,17 +367,17 @@ function executeOperation(
const path = undefined;

switch (operation.operation) {
case 'query':
case OperationTypeNode.QUERY:
return executeFields(exeContext, rootType, rootValue, path, rootFields);
case 'mutation':
case OperationTypeNode.MUTATION:
return executeFieldsSerially(
exeContext,
rootType,
rootValue,
path,
rootFields,
);
case 'subscription':
case OperationTypeNode.SUBSCRIPTION:
// TODO: deprecate `subscribe` and move all logic here
// Temporary solution until we finish merging execute and subscribe together
return executeFields(exeContext, rootType, rootValue, path, rootFields);
Expand Down
6 changes: 5 additions & 1 deletion src/language/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,11 @@ export interface OperationDefinitionNode {
readonly selectionSet: SelectionSetNode;
}

export type OperationTypeNode = 'query' | 'mutation' | 'subscription';
export enum OperationTypeNode {
QUERY = 'query',
MUTATION = 'mutation',
SUBSCRIPTION = 'subscription',
}

export interface VariableDefinitionNode {
readonly kind: typeof Kind.VARIABLE_DEFINITION;
Expand Down
11 changes: 5 additions & 6 deletions src/language/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type {
DocumentNode,
DefinitionNode,
OperationDefinitionNode,
OperationTypeNode,
VariableDefinitionNode,
SelectionSetNode,
SelectionNode,
Expand Down Expand Up @@ -63,7 +62,7 @@ import type {
InputObjectTypeExtensionNode,
} from './ast';
import { Kind } from './kinds';
import { Location } from './ast';
import { Location, OperationTypeNode } from './ast';
import { TokenKind } from './tokenKind';
import { Source, isSource } from './source';
import { DirectiveLocation } from './directiveLocation';
Expand Down Expand Up @@ -305,7 +304,7 @@ export class Parser {
if (this.peek(TokenKind.BRACE_L)) {
return this.node<OperationDefinitionNode>(start, {
kind: Kind.OPERATION_DEFINITION,
operation: 'query',
operation: OperationTypeNode.QUERY,
name: undefined,
variableDefinitions: [],
directives: [],
Expand Down Expand Up @@ -334,11 +333,11 @@ export class Parser {
const operationToken = this.expectToken(TokenKind.NAME);
switch (operationToken.value) {
case 'query':
return 'query';
return OperationTypeNode.QUERY;
case 'mutation':
return 'mutation';
return OperationTypeNode.MUTATION;
case 'subscription':
return 'subscription';
return OperationTypeNode.SUBSCRIPTION;
}

throw this.unexpected(operationToken);
Expand Down
10 changes: 6 additions & 4 deletions src/type/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import type {
ASTNode,
NamedTypeNode,
DirectiveNode,
OperationTypeNode,
ObjectTypeDefinitionNode,
ObjectTypeExtensionNode,
InterfaceTypeDefinitionNode,
InterfaceTypeExtensionNode,
UnionTypeDefinitionNode,
UnionTypeExtensionNode,
} from '../language/ast';
import { OperationTypeNode } from '../language/ast';

import { isEqualType, isTypeSubTypeOf } from '../utilities/typeComparators';

Expand Down Expand Up @@ -120,7 +120,8 @@ function validateRootTypes(context: SchemaValidationContext): void {
`Query root type must be Object type, it cannot be ${inspect(
queryType,
)}.`,
getOperationTypeNode(schema, 'query') ?? (queryType as any).astNode,
getOperationTypeNode(schema, OperationTypeNode.QUERY) ??
(queryType as any).astNode,
);
}

Expand All @@ -129,7 +130,8 @@ function validateRootTypes(context: SchemaValidationContext): void {
context.reportError(
'Mutation root type must be Object type if provided, it cannot be ' +
`${inspect(mutationType)}.`,
getOperationTypeNode(schema, 'mutation') ?? (mutationType as any).astNode,
getOperationTypeNode(schema, OperationTypeNode.MUTATION) ??
(mutationType as any).astNode,
);
}

Expand All @@ -138,7 +140,7 @@ function validateRootTypes(context: SchemaValidationContext): void {
context.reportError(
'Subscription root type must be Object type if provided, it cannot be ' +
`${inspect(subscriptionType)}.`,
getOperationTypeNode(schema, 'subscription') ??
getOperationTypeNode(schema, OperationTypeNode.SUBSCRIPTION) ??
(subscriptionType as any).astNode,
);
}
Expand Down
9 changes: 5 additions & 4 deletions src/validation/rules/KnownDirectivesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { invariant } from '../../jsutils/invariant';
import { GraphQLError } from '../../error/GraphQLError';

import type { ASTVisitor } from '../../language/visitor';
import type { ASTNode, OperationTypeNode } from '../../language/ast';
import type { ASTNode } from '../../language/ast';
import type { DirectiveLocationEnum } from '../../language/directiveLocation';
import { Kind } from '../../language/kinds';
import { OperationTypeNode } from '../../language/ast';
import { DirectiveLocation } from '../../language/directiveLocation';

import { specifiedDirectives } from '../../type/directives';
Expand Down Expand Up @@ -127,11 +128,11 @@ function getDirectiveLocationForOperation(
operation: OperationTypeNode,
): DirectiveLocationEnum {
switch (operation) {
case 'query':
case OperationTypeNode.QUERY:
return DirectiveLocation.QUERY;
case 'mutation':
case OperationTypeNode.MUTATION:
return DirectiveLocation.MUTATION;
case 'subscription':
case OperationTypeNode.SUBSCRIPTION:
return DirectiveLocation.SUBSCRIPTION;
}

Expand Down

0 comments on commit e289e64

Please sign in to comment.