Skip to content

Commit 557a986

Browse files
committed
Add semantic optional type
1 parent 3a900a9 commit 557a986

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

src/language/ast.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export type ASTNode =
162162
| ListTypeNode
163163
| NonNullTypeNode
164164
| SemanticNonNullTypeNode
165+
| SemanticOptionalTypeNode
165166
| SchemaDefinitionNode
166167
| OperationTypeDefinitionNode
167168
| ScalarTypeDefinitionNode
@@ -237,6 +238,7 @@ export const QueryDocumentKeys: {
237238
ListType: ['type'],
238239
NonNullType: ['type'],
239240
SemanticNonNullType: ['type'],
241+
SemanticOptionalType: ['type'],
240242

241243
SchemaDefinition: ['description', 'directives', 'operationTypes'],
242244
OperationTypeDefinition: ['type'],
@@ -526,7 +528,8 @@ export type TypeNode =
526528
| NamedTypeNode
527529
| ListTypeNode
528530
| NonNullTypeNode
529-
| SemanticNonNullTypeNode;
531+
| SemanticNonNullTypeNode
532+
| SemanticOptionalTypeNode;
530533

531534
export interface NamedTypeNode {
532535
readonly kind: Kind.NAMED_TYPE;
@@ -552,6 +555,12 @@ export interface SemanticNonNullTypeNode {
552555
readonly type: NamedTypeNode | ListTypeNode;
553556
}
554557

558+
export interface SemanticOptionalTypeNode {
559+
readonly kind: Kind.SEMANTIC_OPTIONAL_TYPE;
560+
readonly loc?: Location;
561+
readonly type: NamedTypeNode | ListTypeNode;
562+
}
563+
555564
/** Type System Definition */
556565

557566
export type TypeSystemDefinitionNode =

src/language/kinds.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ enum Kind {
3838
LIST_TYPE = 'ListType',
3939
NON_NULL_TYPE = 'NonNullType',
4040
SEMANTIC_NON_NULL_TYPE = 'SemanticNonNullType',
41+
SEMANTIC_OPTIONAL_TYPE = 'SemanticOptionalType',
4142

4243
/** Type System Definitions */
4344
SCHEMA_DEFINITION = 'SchemaDefinition',

src/language/parser.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import type {
5151
SelectionNode,
5252
SelectionSetNode,
5353
SemanticNonNullTypeNode,
54+
SemanticOptionalTypeNode,
5455
StringValueNode,
5556
Token,
5657
TypeNode,
@@ -774,7 +775,10 @@ export class Parser {
774775
type,
775776
});
776777
} else if (this.expectOptionalToken(TokenKind.QUESTION_MARK)) {
777-
return type;
778+
return this.node<SemanticOptionalTypeNode>(start, {
779+
kind: Kind.SEMANTIC_OPTIONAL_TYPE,
780+
type,
781+
});
778782
} else {
779783
return this.node<SemanticNonNullTypeNode>(start, {
780784
kind: Kind.SEMANTIC_NON_NULL_TYPE,

src/language/printer.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@ import { printString } from './printString';
66
import type { ASTReducer } from './visitor';
77
import { visit } from './visitor';
88

9+
/**
10+
* Configuration options to control parser behavior
11+
*/
12+
export interface PrintOptions {
13+
useSemanticNullability?: boolean;
14+
}
15+
916
/**
1017
* Converts an AST into a string, using one set of reasonable
1118
* formatting rules.
1219
*/
13-
export function print(ast: ASTNode): string {
20+
export function print(ast: ASTNode, options: PrintOptions = {}): string {
1421
return visit(ast, printDocASTReducer);
1522
}
1623

@@ -128,10 +135,13 @@ const printDocASTReducer: ASTReducer<string> = {
128135

129136
// Type
130137

131-
NamedType: { leave: ({ name }) => name },
138+
NamedType: { leave: ({ name }) =>
139+
name
140+
},
132141
ListType: { leave: ({ type }) => '[' + type + ']' },
133142
NonNullType: { leave: ({ type }) => type + '!' },
134-
SemanticNonNullType: { leave: ({ type }) => type + '*' },
143+
SemanticNonNullType: { leave: ({ type }) => type },
144+
SemanticOptionalType: { leave: ({ type }) => type + '?' },
135145

136146
// Type System Definitions
137147

0 commit comments

Comments
 (0)