Skip to content

Schema coordinates #4463

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

Open
wants to merge 5 commits into
base: schema-coordinates
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ export {
printSourceLocation,
// Lex
Lexer,
SchemaCoordinateLexer,
TokenKind,
// Parse
parse,
Expand Down Expand Up @@ -262,7 +261,6 @@ export {

export type {
ParseOptions,
ParseSchemaCoordinateOptions,
SourceLocation,
// Visitor utilities
ASTVisitor,
Expand Down
44 changes: 3 additions & 41 deletions src/language/__tests__/lexer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import { inspect } from '../../jsutils/inspect.js';
import { GraphQLError } from '../../error/GraphQLError.js';

import type { Token } from '../ast.js';
import {
isPunctuatorTokenKind,
Lexer,
SchemaCoordinateLexer,
} from '../lexer.js';
import { isPunctuatorTokenKind, Lexer } from '../lexer.js';
import { Source } from '../source.js';
import { TokenKind } from '../tokenKind.js';

Expand Down Expand Up @@ -170,8 +166,8 @@ describe('Lexer', () => {
});

it('reports unexpected characters', () => {
expectSyntaxError('^').to.deep.equal({
message: 'Syntax Error: Unexpected character: "^".',
expectSyntaxError('.').to.deep.equal({
message: 'Syntax Error: Unexpected character: ".".',
locations: [{ line: 1, column: 1 }],
});
});
Expand Down Expand Up @@ -969,13 +965,6 @@ describe('Lexer', () => {
value: undefined,
});

expect(lexOne('.')).to.contain({
kind: TokenKind.DOT,
start: 0,
end: 1,
value: undefined,
});

expect(lexOne('...')).to.contain({
kind: TokenKind.SPREAD,
start: 0,
Expand Down Expand Up @@ -1193,33 +1182,6 @@ describe('Lexer', () => {
});
});

describe('SchemaCoordinateLexer', () => {
it('can be stringified', () => {
const lexer = new SchemaCoordinateLexer(new Source('Name.field'));
expect(Object.prototype.toString.call(lexer)).to.equal(
'[object SchemaCoordinateLexer]',
);
});

it('tracks a schema coordinate', () => {
const lexer = new SchemaCoordinateLexer(new Source('Name.field'));
expect(lexer.advance()).to.contain({
kind: TokenKind.NAME,
start: 0,
end: 4,
value: 'Name',
});
});

it('forbids ignored tokens', () => {
const lexer = new SchemaCoordinateLexer(new Source('\nName.field'));
expectToThrowJSON(() => lexer.advance()).to.deep.equal({
message: 'Syntax Error: Invalid character: U+000A.',
locations: [{ line: 1, column: 1 }],
});
});
});

describe('isPunctuatorTokenKind', () => {
function isPunctuatorToken(text: string) {
return isPunctuatorTokenKind(lexOne(text).kind);
Expand Down
14 changes: 10 additions & 4 deletions src/language/__tests__/parser-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ describe('Parser', () => {
expect(() => parseSchemaCoordinate('MyType.field.deep'))
.to.throw()
.to.deep.include({
message: 'Syntax Error: Expected <EOF>, found ".".',
message: 'Syntax Error: Expected <EOF>, found ..',
locations: [{ line: 1, column: 13 }],
});
});
Expand Down Expand Up @@ -751,10 +751,10 @@ describe('Parser', () => {
});

it('rejects Name . Name ( Name : Name )', () => {
expect(() => parseSchemaCoordinate('MyType.field(arg:value)'))
expect(() => parseSchemaCoordinate('MyType.field(arg: value)'))
.to.throw()
.to.deep.include({
message: 'Syntax Error: Expected ")", found Name "value".',
message: 'Syntax Error: Invalid character: " ".',
locations: [{ line: 1, column: 18 }],
});
});
Expand Down Expand Up @@ -794,9 +794,15 @@ describe('Parser', () => {
expect(() => parseSchemaCoordinate('@myDirective.field'))
.to.throw()
.to.deep.include({
message: 'Syntax Error: Expected <EOF>, found ".".',
message: 'Syntax Error: Expected <EOF>, found ..',
locations: [{ line: 1, column: 13 }],
});
});

it('accepts a Source object', () => {
expect(parseSchemaCoordinate('MyType')).to.deep.equal(
parseSchemaCoordinate(new Source('MyType')),
);
});
});
});
52 changes: 52 additions & 0 deletions src/language/__tests__/schemaCoordinateLexer-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { expectToThrowJSON } from '../../__testUtils__/expectJSON.js';

import { SchemaCoordinateLexer } from '../schemaCoordinateLexer.js';
import { Source } from '../source.js';
import { TokenKind } from '../tokenKind.js';

function lexSecond(str: string) {
const lexer = new SchemaCoordinateLexer(new Source(str));
lexer.advance();
return lexer.advance();
}

function expectSyntaxError(text: string) {
return expectToThrowJSON(() => lexSecond(text));
}

describe('SchemaCoordinateLexer', () => {
it('can be stringified', () => {
const lexer = new SchemaCoordinateLexer(new Source('Name.field'));
expect(Object.prototype.toString.call(lexer)).to.equal(
'[object SchemaCoordinateLexer]',
);
});

it('tracks a schema coordinate', () => {
const lexer = new SchemaCoordinateLexer(new Source('Name.field'));
expect(lexer.advance()).to.contain({
kind: TokenKind.NAME,
start: 0,
end: 4,
value: 'Name',
});
});

it('forbids ignored tokens', () => {
const lexer = new SchemaCoordinateLexer(new Source('\nName.field'));
expectToThrowJSON(() => lexer.advance()).to.deep.equal({
message: 'Syntax Error: Invalid character: U+000A.',
locations: [{ line: 1, column: 1 }],
});
});

it('lex reports a useful syntax errors', () => {
expectSyntaxError('Foo .bar').to.deep.equal({
message: 'Syntax Error: Invalid character: " ".',
locations: [{ line: 1, column: 4 }],
});
});
});
4 changes: 2 additions & 2 deletions src/language/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export { Kind } from './kinds.js';

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

export { Lexer, SchemaCoordinateLexer } from './lexer.js';
export { Lexer } from './lexer.js';

export {
parse,
Expand All @@ -20,7 +20,7 @@ export {
parseType,
parseSchemaCoordinate,
} from './parser.js';
export type { ParseOptions, ParseSchemaCoordinateOptions } from './parser.js';
export type { ParseOptions } from './parser.js';

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

Expand Down
Loading
Loading