Skip to content

Commit d0349a4

Browse files
committed
Allow for parse to return a Promise
1 parent 51e53d7 commit d0349a4

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

src/language/__tests__/parser-test.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ import { kitchenSinkQuery } from '../../__testUtils__/kitchenSinkQuery.js';
1111
import { inspect } from '../../jsutils/inspect.js';
1212

1313
import { Kind } from '../kinds.js';
14-
import { parse, parseConstValue, parseType, parseValue } from '../parser.js';
14+
import {
15+
parse,
16+
parseConstValue,
17+
parseSync,
18+
parseType,
19+
parseValue,
20+
} from '../parser.js';
1521
import { Source } from '../source.js';
1622
import { TokenKind } from '../tokenKind.js';
1723

@@ -20,10 +26,10 @@ function expectSyntaxError(text: string) {
2026
}
2127

2228
describe('Parser', () => {
23-
it('parse provides useful errors', () => {
29+
it('parse provides useful errors', async () => {
2430
let caughtError;
2531
try {
26-
parse('{');
32+
await parse('{');
2733
} catch (error) {
2834
caughtError = error;
2935
}
@@ -71,10 +77,10 @@ describe('Parser', () => {
7177
});
7278
});
7379

74-
it('parse provides useful error when using source', () => {
80+
it('parse provides useful error when using source', async () => {
7581
let caughtError;
7682
try {
77-
parse(new Source('query', 'MyQuery.graphql'));
83+
await parse(new Source('query', 'MyQuery.graphql'));
7884
} catch (error) {
7985
caughtError = error;
8086
}
@@ -100,9 +106,10 @@ describe('Parser', () => {
100106
);
101107
});
102108

103-
it('exposes the tokenCount', () => {
104-
expect(parse('{ foo }').tokenCount).to.equal(3);
105-
expect(parse('{ foo(bar: "baz") }').tokenCount).to.equal(8);
109+
it('exposes the tokenCount', async () => {
110+
expect(parseSync('{ foo }').tokenCount).to.equal(3);
111+
expect((await parse('{ foo }')).tokenCount).to.equal(3);
112+
expect((await parse('{ foo(bar: "baz") }')).tokenCount).to.equal(8);
106113
});
107114

108115
it('parses variable inline values', () => {
@@ -431,8 +438,8 @@ describe('Parser', () => {
431438
expect(() => parse(document)).to.throw();
432439
});
433440

434-
it('contains location that can be Object.toStringified, JSON.stringified, or jsutils.inspected', () => {
435-
const { loc } = parse('{ id }');
441+
it('contains location that can be Object.toStringified, JSON.stringified, or jsutils.inspected', async () => {
442+
const { loc } = await parse('{ id }');
436443

437444
expect(Object.prototype.toString.call(loc)).to.equal('[object Location]');
438445
expect(JSON.stringify(loc)).to.equal('{"start":0,"end":6}');

src/language/__tests__/visitor-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { kitchenSinkQuery } from '../../__testUtils__/kitchenSinkQuery.js';
66
import type { ASTNode, SelectionSetNode } from '../ast.js';
77
import { isNode } from '../ast.js';
88
import { Kind } from '../kinds.js';
9-
import { parse } from '../parser.js';
9+
import { parseSync as parse } from '../parser.js';
1010
import type { ASTVisitor, ASTVisitorKeyMap } from '../visitor.js';
1111
import { BREAK, visit, visitInParallel } from '../visitor.js';
1212

src/language/parser.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isPromise } from '../jsutils/isPromise.js';
12
import type { Maybe } from '../jsutils/Maybe.js';
23

34
import type { GraphQLError } from '../error/GraphQLError.js';
@@ -117,7 +118,7 @@ export interface ParseOptions {
117118
export function parse(
118119
source: string | Source,
119120
options?: ParseOptions | undefined,
120-
): DocumentNode {
121+
): Promise<DocumentNode> | DocumentNode {
121122
const parser = new Parser(source, options);
122123
const document = parser.parseDocument();
123124
Object.defineProperty(document, 'tokenCount', {
@@ -127,6 +128,17 @@ export function parse(
127128
return document;
128129
}
129130

131+
export function parseSync(
132+
source: string | Source,
133+
options?: ParseOptions | undefined,
134+
): DocumentNode {
135+
const result = parse(source, options);
136+
if (isPromise(result)) {
137+
throw new Error('GraphQL parsing failed to complete synchronously.');
138+
}
139+
return result;
140+
}
141+
130142
/**
131143
* Given a string containing a GraphQL value (ex. `[42]`), parse the AST for
132144
* that value.

src/utilities/__tests__/extendSchema-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { dedent } from '../../__testUtils__/dedent.js';
66
import type { Maybe } from '../../jsutils/Maybe.js';
77

88
import type { ASTNode } from '../../language/ast.js';
9-
import { parse } from '../../language/parser.js';
9+
import { parseSync as parse } from '../../language/parser.js';
1010
import { print } from '../../language/printer.js';
1111

1212
import {

0 commit comments

Comments
 (0)