Skip to content

Allow for parse to return a Promise #4278

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

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 15 additions & 8 deletions src/language/__tests__/parser-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import { kitchenSinkQuery } from '../../__testUtils__/kitchenSinkQuery.js';
import { inspect } from '../../jsutils/inspect.js';

import { Kind } from '../kinds.js';
import { parse, parseConstValue, parseType, parseValue } from '../parser.js';
import {
parse,
parseConstValue,
parseSync,
parseType,
parseValue,
} from '../parser.js';
import { Source } from '../source.js';
import { TokenKind } from '../tokenKind.js';

Expand All @@ -23,7 +29,7 @@ describe('Parser', () => {
it('parse provides useful errors', () => {
let caughtError;
try {
parse('{');
parseSync('{');
} catch (error) {
caughtError = error;
}
Expand Down Expand Up @@ -74,7 +80,7 @@ describe('Parser', () => {
it('parse provides useful error when using source', () => {
let caughtError;
try {
parse(new Source('query', 'MyQuery.graphql'));
parseSync(new Source('query', 'MyQuery.graphql'));
} catch (error) {
caughtError = error;
}
Expand All @@ -100,9 +106,10 @@ describe('Parser', () => {
);
});

it('exposes the tokenCount', () => {
expect(parse('{ foo }').tokenCount).to.equal(3);
expect(parse('{ foo(bar: "baz") }').tokenCount).to.equal(8);
it('exposes the tokenCount', async () => {
expect(parseSync('{ foo }').tokenCount).to.equal(3);
expect((await parse('{ foo }')).tokenCount).to.equal(3);
expect((await parse('{ foo(bar: "baz") }')).tokenCount).to.equal(8);
});

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

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

expect(Object.prototype.toString.call(loc)).to.equal('[object Location]');
expect(JSON.stringify(loc)).to.equal('{"start":0,"end":6}');
Expand Down
2 changes: 1 addition & 1 deletion src/language/__tests__/visitor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { kitchenSinkQuery } from '../../__testUtils__/kitchenSinkQuery.js';
import type { ASTNode, SelectionSetNode } from '../ast.js';
import { isNode } from '../ast.js';
import { Kind } from '../kinds.js';
import { parse } from '../parser.js';
import { parseSync as parse } from '../parser.js';
import type { ASTVisitor, ASTVisitorKeyMap } from '../visitor.js';
import { BREAK, visit, visitInParallel } from '../visitor.js';

Expand Down
15 changes: 14 additions & 1 deletion src/language/parser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isPromise } from '../jsutils/isPromise.js';
import type { Maybe } from '../jsutils/Maybe.js';

import type { GraphQLError } from '../error/GraphQLError.js';
Expand Down Expand Up @@ -117,7 +118,7 @@ export interface ParseOptions {
export function parse(
source: string | Source,
options?: ParseOptions | undefined,
): DocumentNode {
): Promise<DocumentNode> | DocumentNode {
const parser = new Parser(source, options);
const document = parser.parseDocument();
Object.defineProperty(document, 'tokenCount', {
Expand All @@ -127,6 +128,18 @@ export function parse(
return document;
}

export function parseSync(
source: string | Source,
options?: ParseOptions | undefined,
): DocumentNode {
const result = parse(source, options);
/* c8 ignore next 3 */
if (isPromise(result)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we can't really test this I've opted to ignore the lines, it's more so that consumers will be prepared to handle a Promise

throw new Error('GraphQL parsing failed to complete synchronously.');
}
return result;
}

/**
* Given a string containing a GraphQL value (ex. `[42]`), parse the AST for
* that value.
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/__tests__/extendSchema-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { dedent } from '../../__testUtils__/dedent.js';
import type { Maybe } from '../../jsutils/Maybe.js';

import type { ASTNode } from '../../language/ast.js';
import { parse } from '../../language/parser.js';
import { parseSync as parse } from '../../language/parser.js';
import { print } from '../../language/printer.js';

import {
Expand Down
Loading