Skip to content

Updates to schema coordinates (#3044) #4422

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

Merged
merged 18 commits into from
Jun 5, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions src/language/__tests__/parser-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,36 +690,31 @@ describe('Parser', () => {
it('parses Name', () => {
const result = parseSchemaCoordinate('MyType');
expectJSON(result).toDeepEqual({
kind: Kind.SCHEMA_COORDINATE,
kind: Kind.TYPE_COORDINATE,
loc: { start: 0, end: 6 },
ofDirective: false,
name: {
kind: Kind.NAME,
loc: { start: 0, end: 6 },
value: 'MyType',
},
memberName: undefined,
argumentName: undefined,
});
});

it('parses Name . Name', () => {
const result = parseSchemaCoordinate('MyType.field');
expectJSON(result).toDeepEqual({
kind: Kind.SCHEMA_COORDINATE,
kind: Kind.FIELD_COORDINATE,
loc: { start: 0, end: 12 },
ofDirective: false,
name: {
kind: Kind.NAME,
loc: { start: 0, end: 6 },
value: 'MyType',
},
memberName: {
fieldName: {
kind: Kind.NAME,
loc: { start: 7, end: 12 },
value: 'field',
},
argumentName: undefined,
});
});

Expand All @@ -732,18 +727,35 @@ describe('Parser', () => {
});
});

it('parses Name :: Name', () => {
const result = parseSchemaCoordinate('MyEnum::value');
expectJSON(result).toDeepEqual({
kind: Kind.VALUE_COORDINATE,
loc: { start: 0, end: 13 },
name: {
kind: Kind.NAME,
loc: { start: 0, end: 6 },
value: 'MyEnum',
},
valueName: {
kind: Kind.NAME,
loc: { start: 8, end: 13 },
value: 'value',
},
});
});

it('parses Name . Name ( Name : )', () => {
const result = parseSchemaCoordinate('MyType.field(arg:)');
expectJSON(result).toDeepEqual({
kind: Kind.SCHEMA_COORDINATE,
kind: Kind.ARGUMENT_COORDINATE,
loc: { start: 0, end: 18 },
ofDirective: false,
name: {
kind: Kind.NAME,
loc: { start: 0, end: 6 },
value: 'MyType',
},
memberName: {
fieldName: {
kind: Kind.NAME,
loc: { start: 7, end: 12 },
value: 'field',
Expand All @@ -768,31 +780,26 @@ describe('Parser', () => {
it('parses @ Name', () => {
const result = parseSchemaCoordinate('@myDirective');
expectJSON(result).toDeepEqual({
kind: Kind.SCHEMA_COORDINATE,
kind: Kind.DIRECTIVE_COORDINATE,
loc: { start: 0, end: 12 },
ofDirective: true,
name: {
kind: Kind.NAME,
loc: { start: 1, end: 12 },
value: 'myDirective',
},
memberName: undefined,
argumentName: undefined,
});
});

it('parses @ Name ( Name : )', () => {
const result = parseSchemaCoordinate('@myDirective(arg:)');
expectJSON(result).toDeepEqual({
kind: Kind.SCHEMA_COORDINATE,
kind: Kind.DIRECTIVE_ARGUMENT_COORDINATE,
loc: { start: 0, end: 18 },
ofDirective: true,
name: {
kind: Kind.NAME,
loc: { start: 1, end: 12 },
value: 'myDirective',
},
memberName: undefined,
argumentName: {
kind: Kind.NAME,
loc: { start: 13, end: 16 },
Expand Down
7 changes: 6 additions & 1 deletion src/language/__tests__/predicates-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@ describe('AST node predicates', () => {

it('isSchemaCoordinateNode', () => {
expect(filterNodes(isSchemaCoordinateNode)).to.deep.equal([
'SchemaCoordinate',
'ArgumentCoordinate',
'DirectiveArgumentCoordinate',
'DirectiveCoordinate',
'FieldCoordinate',
'TypeCoordinate',
'ValueCoordinate',
]);
});
});
68 changes: 60 additions & 8 deletions src/language/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,12 @@ export type ASTNode =
| UnionTypeExtensionNode
| EnumTypeExtensionNode
| InputObjectTypeExtensionNode
| SchemaCoordinateNode;
| TypeCoordinateNode
| FieldCoordinateNode
| ArgumentCoordinateNode
| ValueCoordinateNode
| DirectiveCoordinateNode
| DirectiveArgumentCoordinateNode;

/**
* Utility type listing all nodes indexed by their kind.
Expand Down Expand Up @@ -288,7 +293,14 @@ export const QueryDocumentKeys: {
UnionTypeExtension: ['name', 'directives', 'types'],
EnumTypeExtension: ['name', 'directives', 'values'],
InputObjectTypeExtension: ['name', 'directives', 'fields'],
SchemaCoordinate: ['name', 'memberName', 'argumentName'],

// Schema Coordinates
TypeCoordinate: ['name'],
FieldCoordinate: ['name', 'fieldName'],
ArgumentCoordinate: ['name', 'fieldName', 'argumentName'],
ValueCoordinate: ['name', 'valueName'],
DirectiveCoordinate: ['name'],
DirectiveArgumentCoordinate: ['name', 'argumentName'],
};

const kindValues = new Set<string>(Object.keys(QueryDocumentKeys));
Expand Down Expand Up @@ -765,13 +777,53 @@ export interface InputObjectTypeExtensionNode {
readonly fields?: ReadonlyArray<InputValueDefinitionNode> | undefined;
}

// Schema Coordinates
/** Schema Coordinates */

export type SchemaCoordinateNode =
| TypeCoordinateNode
| FieldCoordinateNode
| ArgumentCoordinateNode
| ValueCoordinateNode
| DirectiveCoordinateNode
| DirectiveArgumentCoordinateNode;

export interface TypeCoordinateNode {
readonly kind: typeof Kind.TYPE_COORDINATE;
readonly loc?: Location;
readonly name: NameNode;
}

export interface FieldCoordinateNode {
readonly kind: typeof Kind.FIELD_COORDINATE;
readonly loc?: Location;
readonly name: NameNode;
readonly fieldName: NameNode;
}

export interface ArgumentCoordinateNode {
readonly kind: typeof Kind.ARGUMENT_COORDINATE;
readonly loc?: Location;
readonly name: NameNode;
readonly fieldName: NameNode;
readonly argumentName: NameNode;
}

export interface ValueCoordinateNode {
readonly kind: typeof Kind.VALUE_COORDINATE;
readonly loc?: Location;
readonly name: NameNode;
readonly valueName: NameNode;
}

export interface DirectiveCoordinateNode {
readonly kind: typeof Kind.DIRECTIVE_COORDINATE;
readonly loc?: Location;
readonly name: NameNode;
}

export interface SchemaCoordinateNode {
readonly kind: 'SchemaCoordinate';
export interface DirectiveArgumentCoordinateNode {
readonly kind: typeof Kind.DIRECTIVE_ARGUMENT_COORDINATE;
readonly loc?: Location;
readonly ofDirective: boolean;
readonly name: NameNode;
readonly memberName?: NameNode | undefined;
readonly argumentName?: NameNode | undefined;
readonly argumentName: NameNode;
}
20 changes: 18 additions & 2 deletions src/language/kinds_.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,21 @@ export const INPUT_OBJECT_TYPE_EXTENSION = 'InputObjectTypeExtension';
export type INPUT_OBJECT_TYPE_EXTENSION = typeof INPUT_OBJECT_TYPE_EXTENSION;

/** Schema Coordinates */
export const SCHEMA_COORDINATE = 'SchemaCoordinate';
export type SCHEMA_COORDINATE = typeof SCHEMA_COORDINATE;
export const TYPE_COORDINATE = 'TypeCoordinate';
export type TYPE_COORDINATE = typeof TYPE_COORDINATE;

export const FIELD_COORDINATE = 'FieldCoordinate';
export type FIELD_COORDINATE = typeof FIELD_COORDINATE;

export const ARGUMENT_COORDINATE = 'ArgumentCoordinate';
export type ARGUMENT_COORDINATE = typeof ARGUMENT_COORDINATE;

export const VALUE_COORDINATE = 'ValueCoordinate';
export type VALUE_COORDINATE = typeof VALUE_COORDINATE;

export const DIRECTIVE_COORDINATE = 'DirectiveCoordinate';
export type DIRECTIVE_COORDINATE = typeof DIRECTIVE_COORDINATE;

export const DIRECTIVE_ARGUMENT_COORDINATE = 'DirectiveArgumentCoordinate';
export type DIRECTIVE_ARGUMENT_COORDINATE =
typeof DIRECTIVE_ARGUMENT_COORDINATE;
9 changes: 9 additions & 0 deletions src/language/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export function isPunctuatorTokenKind(kind: TokenKind): boolean {
kind === TokenKind.DOT ||
kind === TokenKind.SPREAD ||
kind === TokenKind.COLON ||
kind === TokenKind.TWO_COLON ||
kind === TokenKind.EQUALS ||
kind === TokenKind.AT ||
kind === TokenKind.BRACKET_L ||
Expand Down Expand Up @@ -271,6 +272,14 @@ function readNextToken(lexer: Lexer, start: number): Token {
return readDot(lexer, position);
}
case 0x003a: // :
if (body.charCodeAt(position + 1) === 0x003a) {
return createToken(
lexer,
TokenKind.TWO_COLON,
position,
position + 2,
);
}
return createToken(lexer, TokenKind.COLON, position, position + 1);
case 0x003d: // =
return createToken(lexer, TokenKind.EQUALS, position, position + 1);
Expand Down
53 changes: 48 additions & 5 deletions src/language/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { GraphQLError } from '../error/GraphQLError.js';
import { syntaxError } from '../error/syntaxError.js';

import type {
ArgumentCoordinateNode,
ArgumentNode,
BooleanValueNode,
ConstArgumentNode,
Expand All @@ -13,13 +14,16 @@ import type {
ConstObjectValueNode,
ConstValueNode,
DefinitionNode,
DirectiveArgumentCoordinateNode,
DirectiveCoordinateNode,
DirectiveDefinitionNode,
DirectiveNode,
DocumentNode,
EnumTypeDefinitionNode,
EnumTypeExtensionNode,
EnumValueDefinitionNode,
EnumValueNode,
FieldCoordinateNode,
FieldDefinitionNode,
FieldNode,
FloatValueNode,
Expand Down Expand Up @@ -54,10 +58,12 @@ import type {
SelectionSetNode,
StringValueNode,
Token,
TypeCoordinateNode,
TypeNode,
TypeSystemExtensionNode,
UnionTypeDefinitionNode,
UnionTypeExtensionNode,
ValueCoordinateNode,
ValueNode,
VariableDefinitionNode,
VariableNode,
Expand Down Expand Up @@ -1461,13 +1467,24 @@ export class Parser {
* - Name
* - Name . Name
* - Name . Name ( Name : )
* - Name :: Name
* - @ Name
* - @ Name ( Name : )
*/
parseSchemaCoordinate(): SchemaCoordinateNode {
const start = this._lexer.token;
const ofDirective = this.expectOptionalToken(TokenKind.AT);
const name = this.parseName();

if (!ofDirective && this.expectOptionalToken(TokenKind.TWO_COLON)) {
const valueName = this.parseName();
return this.node<ValueCoordinateNode>(start, {
kind: Kind.VALUE_COORDINATE,
name,
valueName,
});
}

let memberName: NameNode | undefined;
if (!ofDirective && this.expectOptionalToken(TokenKind.DOT)) {
memberName = this.parseName();
Expand All @@ -1481,12 +1498,38 @@ export class Parser {
this.expectToken(TokenKind.COLON);
this.expectToken(TokenKind.PAREN_R);
}
return this.node<SchemaCoordinateNode>(start, {
kind: Kind.SCHEMA_COORDINATE,
ofDirective,

if (ofDirective) {
if (argumentName) {
return this.node<DirectiveArgumentCoordinateNode>(start, {
kind: Kind.DIRECTIVE_ARGUMENT_COORDINATE,
name,
argumentName,
});
}
return this.node<DirectiveCoordinateNode>(start, {
kind: Kind.DIRECTIVE_COORDINATE,
name,
});
} else if (memberName) {
if (argumentName) {
return this.node<ArgumentCoordinateNode>(start, {
kind: Kind.ARGUMENT_COORDINATE,
name,
fieldName: memberName,
argumentName,
});
}
return this.node<FieldCoordinateNode>(start, {
kind: Kind.FIELD_COORDINATE,
name,
fieldName: memberName,
});
}

return this.node<TypeCoordinateNode>(start, {
kind: Kind.TYPE_COORDINATE,
name,
memberName,
argumentName,
});
}

Expand Down
9 changes: 8 additions & 1 deletion src/language/predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,12 @@ export function isTypeExtensionNode(node: ASTNode): node is TypeExtensionNode {
export function isSchemaCoordinateNode(
node: ASTNode,
): node is SchemaCoordinateNode {
return node.kind === Kind.SCHEMA_COORDINATE;
return (
node.kind === Kind.TYPE_COORDINATE ||
node.kind === Kind.FIELD_COORDINATE ||
node.kind === Kind.ARGUMENT_COORDINATE ||
node.kind === Kind.VALUE_COORDINATE ||
node.kind === Kind.DIRECTIVE_COORDINATE ||
node.kind === Kind.DIRECTIVE_ARGUMENT_COORDINATE
);
}
Loading
Loading