Skip to content

Commit fd6ce88

Browse files
authored
Schema coordinates (#4463)
1 parent 04dd13e commit fd6ce88

File tree

8 files changed

+254
-146
lines changed

8 files changed

+254
-146
lines changed

src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ export {
230230
printSourceLocation,
231231
// Lex
232232
Lexer,
233-
SchemaCoordinateLexer,
234233
TokenKind,
235234
// Parse
236235
parse,
@@ -262,7 +261,6 @@ export {
262261

263262
export type {
264263
ParseOptions,
265-
ParseSchemaCoordinateOptions,
266264
SourceLocation,
267265
// Visitor utilities
268266
ASTVisitor,

src/language/__tests__/lexer-test.ts

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ import { inspect } from '../../jsutils/inspect.js';
99
import { GraphQLError } from '../../error/GraphQLError.js';
1010

1111
import type { Token } from '../ast.js';
12-
import {
13-
isPunctuatorTokenKind,
14-
Lexer,
15-
SchemaCoordinateLexer,
16-
} from '../lexer.js';
12+
import { isPunctuatorTokenKind, Lexer } from '../lexer.js';
1713
import { Source } from '../source.js';
1814
import { TokenKind } from '../tokenKind.js';
1915

@@ -170,8 +166,8 @@ describe('Lexer', () => {
170166
});
171167

172168
it('reports unexpected characters', () => {
173-
expectSyntaxError('^').to.deep.equal({
174-
message: 'Syntax Error: Unexpected character: "^".',
169+
expectSyntaxError('.').to.deep.equal({
170+
message: 'Syntax Error: Unexpected character: ".".',
175171
locations: [{ line: 1, column: 1 }],
176172
});
177173
});
@@ -969,13 +965,6 @@ describe('Lexer', () => {
969965
value: undefined,
970966
});
971967

972-
expect(lexOne('.')).to.contain({
973-
kind: TokenKind.DOT,
974-
start: 0,
975-
end: 1,
976-
value: undefined,
977-
});
978-
979968
expect(lexOne('...')).to.contain({
980969
kind: TokenKind.SPREAD,
981970
start: 0,
@@ -1193,33 +1182,6 @@ describe('Lexer', () => {
11931182
});
11941183
});
11951184

1196-
describe('SchemaCoordinateLexer', () => {
1197-
it('can be stringified', () => {
1198-
const lexer = new SchemaCoordinateLexer(new Source('Name.field'));
1199-
expect(Object.prototype.toString.call(lexer)).to.equal(
1200-
'[object SchemaCoordinateLexer]',
1201-
);
1202-
});
1203-
1204-
it('tracks a schema coordinate', () => {
1205-
const lexer = new SchemaCoordinateLexer(new Source('Name.field'));
1206-
expect(lexer.advance()).to.contain({
1207-
kind: TokenKind.NAME,
1208-
start: 0,
1209-
end: 4,
1210-
value: 'Name',
1211-
});
1212-
});
1213-
1214-
it('forbids ignored tokens', () => {
1215-
const lexer = new SchemaCoordinateLexer(new Source('\nName.field'));
1216-
expectToThrowJSON(() => lexer.advance()).to.deep.equal({
1217-
message: 'Syntax Error: Invalid character: U+000A.',
1218-
locations: [{ line: 1, column: 1 }],
1219-
});
1220-
});
1221-
});
1222-
12231185
describe('isPunctuatorTokenKind', () => {
12241186
function isPunctuatorToken(text: string) {
12251187
return isPunctuatorTokenKind(lexOne(text).kind);

src/language/__tests__/parser-test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ describe('Parser', () => {
722722
expect(() => parseSchemaCoordinate('MyType.field.deep'))
723723
.to.throw()
724724
.to.deep.include({
725-
message: 'Syntax Error: Expected <EOF>, found ".".',
725+
message: 'Syntax Error: Expected <EOF>, found ..',
726726
locations: [{ line: 1, column: 13 }],
727727
});
728728
});
@@ -751,10 +751,10 @@ describe('Parser', () => {
751751
});
752752

753753
it('rejects Name . Name ( Name : Name )', () => {
754-
expect(() => parseSchemaCoordinate('MyType.field(arg:value)'))
754+
expect(() => parseSchemaCoordinate('MyType.field(arg: value)'))
755755
.to.throw()
756756
.to.deep.include({
757-
message: 'Syntax Error: Expected ")", found Name "value".',
757+
message: 'Syntax Error: Invalid character: " ".',
758758
locations: [{ line: 1, column: 18 }],
759759
});
760760
});
@@ -794,9 +794,15 @@ describe('Parser', () => {
794794
expect(() => parseSchemaCoordinate('@myDirective.field'))
795795
.to.throw()
796796
.to.deep.include({
797-
message: 'Syntax Error: Expected <EOF>, found ".".',
797+
message: 'Syntax Error: Expected <EOF>, found ..',
798798
locations: [{ line: 1, column: 13 }],
799799
});
800800
});
801+
802+
it('accepts a Source object', () => {
803+
expect(parseSchemaCoordinate('MyType')).to.deep.equal(
804+
parseSchemaCoordinate(new Source('MyType')),
805+
);
806+
});
801807
});
802808
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { expect } from 'chai';
2+
import { describe, it } from 'mocha';
3+
4+
import { expectToThrowJSON } from '../../__testUtils__/expectJSON.js';
5+
6+
import { SchemaCoordinateLexer } from '../schemaCoordinateLexer.js';
7+
import { Source } from '../source.js';
8+
import { TokenKind } from '../tokenKind.js';
9+
10+
function lexSecond(str: string) {
11+
const lexer = new SchemaCoordinateLexer(new Source(str));
12+
lexer.advance();
13+
return lexer.advance();
14+
}
15+
16+
function expectSyntaxError(text: string) {
17+
return expectToThrowJSON(() => lexSecond(text));
18+
}
19+
20+
describe('SchemaCoordinateLexer', () => {
21+
it('can be stringified', () => {
22+
const lexer = new SchemaCoordinateLexer(new Source('Name.field'));
23+
expect(Object.prototype.toString.call(lexer)).to.equal(
24+
'[object SchemaCoordinateLexer]',
25+
);
26+
});
27+
28+
it('tracks a schema coordinate', () => {
29+
const lexer = new SchemaCoordinateLexer(new Source('Name.field'));
30+
expect(lexer.advance()).to.contain({
31+
kind: TokenKind.NAME,
32+
start: 0,
33+
end: 4,
34+
value: 'Name',
35+
});
36+
});
37+
38+
it('forbids ignored tokens', () => {
39+
const lexer = new SchemaCoordinateLexer(new Source('\nName.field'));
40+
expectToThrowJSON(() => lexer.advance()).to.deep.equal({
41+
message: 'Syntax Error: Invalid character: U+000A.',
42+
locations: [{ line: 1, column: 1 }],
43+
});
44+
});
45+
46+
it('lex reports a useful syntax errors', () => {
47+
expectSyntaxError('Foo .bar').to.deep.equal({
48+
message: 'Syntax Error: Invalid character: " ".',
49+
locations: [{ line: 1, column: 4 }],
50+
});
51+
});
52+
});

src/language/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export { Kind } from './kinds.js';
1111

1212
export { TokenKind } from './tokenKind.js';
1313

14-
export { Lexer, SchemaCoordinateLexer } from './lexer.js';
14+
export { Lexer } from './lexer.js';
1515

1616
export {
1717
parse,
@@ -20,7 +20,7 @@ export {
2020
parseType,
2121
parseSchemaCoordinate,
2222
} from './parser.js';
23-
export type { ParseOptions, ParseSchemaCoordinateOptions } from './parser.js';
23+
export type { ParseOptions } from './parser.js';
2424

2525
export { print } from './printer.js';
2626

0 commit comments

Comments
 (0)