Skip to content

Commit

Permalink
Remove SemanticNonNull from TypeNode
Browse files Browse the repository at this point in the history
This type is reused for variables, inputs and list-types which
can't be smantically non-null. list-types can be but only if they
are used in an SDL context.

This is kind of a short-coming of our types, we conflate SDL
and execution language.
  • Loading branch information
JoviDeCroock committed Feb 12, 2025
1 parent 2321f93 commit 930c7d2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 23 deletions.
8 changes: 2 additions & 6 deletions src/language/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,11 +529,7 @@ export interface SemanticNonNullTypeNode {

/** Type Reference */

export type TypeNode =
| NamedTypeNode
| ListTypeNode
| NonNullTypeNode
| SemanticNonNullTypeNode;
export type TypeNode = NamedTypeNode | ListTypeNode | NonNullTypeNode;

export interface NamedTypeNode {
readonly kind: Kind.NAMED_TYPE;
Expand Down Expand Up @@ -609,7 +605,7 @@ export interface FieldDefinitionNode {
readonly description?: StringValueNode;
readonly name: NameNode;
readonly arguments?: ReadonlyArray<InputValueDefinitionNode>;
readonly type: TypeNode;
readonly type: TypeNode | SemanticNonNullTypeNode;
readonly directives?: ReadonlyArray<ConstDirectiveNode>;
}

Expand Down
11 changes: 6 additions & 5 deletions src/language/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export function parseConstValue(
export function parseType(
source: string | Source,
options?: ParseOptions | undefined,
): TypeNode {
): TypeNode | SemanticNonNullTypeNode {
const parser = new Parser(source, options);
parser.expectToken(TokenKind.SOF);
const type = parser.parseTypeReference();
Expand Down Expand Up @@ -403,7 +403,8 @@ export class Parser {
return this.node<VariableDefinitionNode>(this._lexer.token, {
kind: Kind.VARIABLE_DEFINITION,
variable: this.parseVariable(),
type: (this.expectToken(TokenKind.COLON), this.parseTypeReference()),
type: (this.expectToken(TokenKind.COLON),
this.parseTypeReference()) as TypeNode,
defaultValue: this.expectOptionalToken(TokenKind.EQUALS)
? this.parseConstValueLiteral()
: undefined,
Expand Down Expand Up @@ -773,15 +774,15 @@ export class Parser {
* - ListType
* - NonNullType
*/
parseTypeReference(): TypeNode {
parseTypeReference(): TypeNode | SemanticNonNullTypeNode {
const start = this._lexer.token;
let type;
if (this.expectOptionalToken(TokenKind.BRACKET_L)) {
const innerType = this.parseTypeReference();
this.expectToken(TokenKind.BRACKET_R);
type = this.node<ListTypeNode>(start, {
kind: Kind.LIST_TYPE,
type: innerType,
type: innerType as TypeNode,
});
} else {
type = this.parseNamedType();
Expand Down Expand Up @@ -992,7 +993,7 @@ export class Parser {
kind: Kind.INPUT_VALUE_DEFINITION,
description,
name,
type,
type: type as TypeNode,
defaultValue,
directives,
});
Expand Down
5 changes: 4 additions & 1 deletion src/utilities/extendSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
ScalarTypeExtensionNode,
SchemaDefinitionNode,
SchemaExtensionNode,
SemanticNonNullTypeNode,
TypeDefinitionNode,
TypeNode,
UnionTypeDefinitionNode,
Expand Down Expand Up @@ -431,7 +432,9 @@ export function extendSchemaImpl(
return type;
}

function getWrappedType(node: TypeNode): GraphQLType {
function getWrappedType(
node: TypeNode | SemanticNonNullTypeNode,
): GraphQLType {
if (node.kind === Kind.LIST_TYPE) {
return new GraphQLList(getWrappedType(node.type));
}
Expand Down
12 changes: 1 addition & 11 deletions src/utilities/typeFromAST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import type {
import { Kind } from '../language/kinds';

import type { GraphQLNamedType, GraphQLType } from '../type/definition';
import {
GraphQLList,
GraphQLNonNull,
GraphQLSemanticNonNull,
} from '../type/definition';
import { GraphQLList, GraphQLNonNull } from '../type/definition';
import type { GraphQLSchema } from '../type/schema';

/**
Expand Down Expand Up @@ -50,12 +46,6 @@ export function typeFromAST(
const innerType = typeFromAST(schema, typeNode.type);
return innerType && new GraphQLNonNull(innerType);
}
// We only use typeFromAST for fragment/variable type inference
// which should not be affected by semantic non-null types
// case Kind.SEMANTIC_NON_NULL_TYPE: {
// const innerType = typeFromAST(schema, typeNode.type);
// return innerType && new GraphQLSemanticNonNull(innerType);
// }
case Kind.NAMED_TYPE:
return schema.getType(typeNode.name.value);
}
Expand Down

0 comments on commit 930c7d2

Please sign in to comment.