Skip to content

Commit e289e64

Browse files
language: change OperationTypeNode to enum (#3312)
Co-authored-by: Ivan Goncharov <[email protected]>
1 parent d48d502 commit e289e64

File tree

5 files changed

+25
-18
lines changed

5 files changed

+25
-18
lines changed

src/execution/execute.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import type {
2323
FieldNode,
2424
FragmentDefinitionNode,
2525
} from '../language/ast';
26+
import { OperationTypeNode } from '../language/ast';
2627
import { Kind } from '../language/kinds';
2728

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

368369
switch (operation.operation) {
369-
case 'query':
370+
case OperationTypeNode.QUERY:
370371
return executeFields(exeContext, rootType, rootValue, path, rootFields);
371-
case 'mutation':
372+
case OperationTypeNode.MUTATION:
372373
return executeFieldsSerially(
373374
exeContext,
374375
rootType,
375376
rootValue,
376377
path,
377378
rootFields,
378379
);
379-
case 'subscription':
380+
case OperationTypeNode.SUBSCRIPTION:
380381
// TODO: deprecate `subscribe` and move all logic here
381382
// Temporary solution until we finish merging execute and subscribe together
382383
return executeFields(exeContext, rootType, rootValue, path, rootFields);

src/language/ast.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,11 @@ export interface OperationDefinitionNode {
365365
readonly selectionSet: SelectionSetNode;
366366
}
367367

368-
export type OperationTypeNode = 'query' | 'mutation' | 'subscription';
368+
export enum OperationTypeNode {
369+
QUERY = 'query',
370+
MUTATION = 'mutation',
371+
SUBSCRIPTION = 'subscription',
372+
}
369373

370374
export interface VariableDefinitionNode {
371375
readonly kind: typeof Kind.VARIABLE_DEFINITION;

src/language/parser.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import type {
1111
DocumentNode,
1212
DefinitionNode,
1313
OperationDefinitionNode,
14-
OperationTypeNode,
1514
VariableDefinitionNode,
1615
SelectionSetNode,
1716
SelectionNode,
@@ -63,7 +62,7 @@ import type {
6362
InputObjectTypeExtensionNode,
6463
} from './ast';
6564
import { Kind } from './kinds';
66-
import { Location } from './ast';
65+
import { Location, OperationTypeNode } from './ast';
6766
import { TokenKind } from './tokenKind';
6867
import { Source, isSource } from './source';
6968
import { DirectiveLocation } from './directiveLocation';
@@ -305,7 +304,7 @@ export class Parser {
305304
if (this.peek(TokenKind.BRACE_L)) {
306305
return this.node<OperationDefinitionNode>(start, {
307306
kind: Kind.OPERATION_DEFINITION,
308-
operation: 'query',
307+
operation: OperationTypeNode.QUERY,
309308
name: undefined,
310309
variableDefinitions: [],
311310
directives: [],
@@ -334,11 +333,11 @@ export class Parser {
334333
const operationToken = this.expectToken(TokenKind.NAME);
335334
switch (operationToken.value) {
336335
case 'query':
337-
return 'query';
336+
return OperationTypeNode.QUERY;
338337
case 'mutation':
339-
return 'mutation';
338+
return OperationTypeNode.MUTATION;
340339
case 'subscription':
341-
return 'subscription';
340+
return OperationTypeNode.SUBSCRIPTION;
342341
}
343342

344343
throw this.unexpected(operationToken);

src/type/validate.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import type {
77
ASTNode,
88
NamedTypeNode,
99
DirectiveNode,
10-
OperationTypeNode,
1110
ObjectTypeDefinitionNode,
1211
ObjectTypeExtensionNode,
1312
InterfaceTypeDefinitionNode,
1413
InterfaceTypeExtensionNode,
1514
UnionTypeDefinitionNode,
1615
UnionTypeExtensionNode,
1716
} from '../language/ast';
17+
import { OperationTypeNode } from '../language/ast';
1818

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

@@ -120,7 +120,8 @@ function validateRootTypes(context: SchemaValidationContext): void {
120120
`Query root type must be Object type, it cannot be ${inspect(
121121
queryType,
122122
)}.`,
123-
getOperationTypeNode(schema, 'query') ?? (queryType as any).astNode,
123+
getOperationTypeNode(schema, OperationTypeNode.QUERY) ??
124+
(queryType as any).astNode,
124125
);
125126
}
126127

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

@@ -138,7 +140,7 @@ function validateRootTypes(context: SchemaValidationContext): void {
138140
context.reportError(
139141
'Subscription root type must be Object type if provided, it cannot be ' +
140142
`${inspect(subscriptionType)}.`,
141-
getOperationTypeNode(schema, 'subscription') ??
143+
getOperationTypeNode(schema, OperationTypeNode.SUBSCRIPTION) ??
142144
(subscriptionType as any).astNode,
143145
);
144146
}

src/validation/rules/KnownDirectivesRule.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { invariant } from '../../jsutils/invariant';
44
import { GraphQLError } from '../../error/GraphQLError';
55

66
import type { ASTVisitor } from '../../language/visitor';
7-
import type { ASTNode, OperationTypeNode } from '../../language/ast';
7+
import type { ASTNode } from '../../language/ast';
88
import type { DirectiveLocationEnum } from '../../language/directiveLocation';
99
import { Kind } from '../../language/kinds';
10+
import { OperationTypeNode } from '../../language/ast';
1011
import { DirectiveLocation } from '../../language/directiveLocation';
1112

1213
import { specifiedDirectives } from '../../type/directives';
@@ -127,11 +128,11 @@ function getDirectiveLocationForOperation(
127128
operation: OperationTypeNode,
128129
): DirectiveLocationEnum {
129130
switch (operation) {
130-
case 'query':
131+
case OperationTypeNode.QUERY:
131132
return DirectiveLocation.QUERY;
132-
case 'mutation':
133+
case OperationTypeNode.MUTATION:
133134
return DirectiveLocation.MUTATION;
134-
case 'subscription':
135+
case OperationTypeNode.SUBSCRIPTION:
135136
return DirectiveLocation.SUBSCRIPTION;
136137
}
137138

0 commit comments

Comments
 (0)