Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

language: change OperationTypeNode to enum #3312

Merged
merged 2 commits into from Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/execution/execute.ts
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
Expand Up @@ -364,7 +364,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: 'VariableDefinition';
Expand Down
11 changes: 5 additions & 6 deletions src/language/parser.ts
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
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
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