Skip to content

Commit

Permalink
Allow for parse to return a Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Nov 1, 2024
1 parent 51e53d7 commit d0349a4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
27 changes: 17 additions & 10 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 @@ -20,10 +26,10 @@ function expectSyntaxError(text: string) {
}

describe('Parser', () => {
it('parse provides useful errors', () => {
it('parse provides useful errors', async () => {
let caughtError;
try {
parse('{');
await parse('{');
} catch (error) {
caughtError = error;
}
Expand Down Expand Up @@ -71,10 +77,10 @@ describe('Parser', () => {
});
});

it('parse provides useful error when using source', () => {
it('parse provides useful error when using source', async () => {
let caughtError;
try {
parse(new Source('query', 'MyQuery.graphql'));
await parse(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
14 changes: 13 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,17 @@ export function parse(
return document;
}

export function parseSync(
source: string | Source,
options?: ParseOptions | undefined,
): DocumentNode {
const result = parse(source, options);
if (isPromise(result)) {
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

0 comments on commit d0349a4

Please sign in to comment.