Skip to content

Commit 368d5a1

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

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

src/language/__tests__/parser-test.ts

Lines changed: 15 additions & 8 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

@@ -23,7 +29,7 @@ describe('Parser', () => {
2329
it('parse provides useful errors', () => {
2430
let caughtError;
2531
try {
26-
parse('{');
32+
parseSync('{');
2733
} catch (error) {
2834
caughtError = error;
2935
}
@@ -74,7 +80,7 @@ describe('Parser', () => {
7480
it('parse provides useful error when using source', () => {
7581
let caughtError;
7682
try {
77-
parse(new Source('query', 'MyQuery.graphql'));
83+
parseSync(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: 14 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,18 @@ 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+
/* c8 ignore next 3 */
137+
if (isPromise(result)) {
138+
throw new Error('GraphQL parsing failed to complete synchronously.');
139+
}
140+
return result;
141+
}
142+
130143
/**
131144
* Given a string containing a GraphQL value (ex. `[42]`), parse the AST for
132145
* 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)