Skip to content

Commit

Permalink
fix: change OperationTypeNode to enum
Browse files Browse the repository at this point in the history
Changes OperationTypeNode to enum instead of a union type.
This allows developers to use the type as `OperationTypeNode.QUERY`
instead of risking to msispell the string 'query'.

Example:

Instead of having to write

`operation === 'query'`

you can instead write

`operation === OperationTypeNode.QUERY`
  • Loading branch information
lekoaf committed Oct 12, 2021
1 parent 82900fa commit c933764
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
9 changes: 6 additions & 3 deletions src/execution/execute.ts
Expand Up @@ -23,6 +23,9 @@ 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 +369,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
9 changes: 5 additions & 4 deletions src/type/validate.ts
Expand Up @@ -7,7 +7,6 @@ import type {
ASTNode,
NamedTypeNode,
DirectiveNode,
OperationTypeNode,
ObjectTypeDefinitionNode,
ObjectTypeExtensionNode,
InterfaceTypeDefinitionNode,
Expand All @@ -16,6 +15,8 @@ import type {
UnionTypeExtensionNode,
} from '../language/ast';

import { OperationTypeNode } from '../language/ast';

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

import type { GraphQLSchema } from './schema';
Expand Down Expand Up @@ -120,7 +121,7 @@ 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,7 @@ 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 +139,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,7 +4,8 @@ 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 { OperationTypeNode } from '../../language/ast';
import type { DirectiveLocationEnum } from '../../language/directiveLocation';
import { Kind } from '../../language/kinds';
import { DirectiveLocation } from '../../language/directiveLocation';
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 c933764

Please sign in to comment.