Skip to content

Commit 91f5556

Browse files
committed
✨ Allow empty schema parsing :D
1 parent 0447dc6 commit 91f5556

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-js-tree",
3-
"version": "0.1.7",
3+
"version": "0.1.8",
44
"private": false,
55
"license": "MIT",
66
"description": "GraphQL Parser providing simplier structure",

src/Parser/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ export class Parser {
6767
static parse = (schema: string, excludeRoots: string[] = [], libraries = ''): ParserTree => {
6868
let parsedSchema: DocumentNode | undefined;
6969
const compiledSchema = [libraries, schema].join('\n');
70-
70+
const isEmptySchema = compiledSchema.replace(/\s+/gm, '').length === 0;
71+
if (isEmptySchema) {
72+
return { nodes: [] };
73+
}
7174
try {
7275
parsedSchema = parse(compiledSchema);
7376
} catch (error) {

src/__tests__/Parser/Empty.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Parser } from '../../Parser';
2+
3+
describe('Empty schema tests on parser', () => {
4+
it('Creates empty tree from schema', () => {
5+
const schema = ` `;
6+
const tree = Parser.parse(schema);
7+
expect(tree.nodes.length).toEqual(0);
8+
});
9+
it('Creates empty tree from schema', () => {
10+
const schema = ` \t\t \n\n\n `;
11+
const tree = Parser.parse(schema);
12+
expect(tree.nodes.length).toEqual(0);
13+
});
14+
});

src/shared/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const decompileType = (typeName: string): FieldType => {
3939
};
4040

4141
export const generateNodeId = (name: string, dataType: AllTypes, args: ParserField[]) => {
42-
const s = [name, dataType, args.map((a) => a.name)].join('-');
42+
const s = [name, dataType, args.map((a) => a.name).join('-')].join('-');
4343
return cyrb53(s);
4444
};
4545
const cyrb53 = (str: string, seed = 0) => {

0 commit comments

Comments
 (0)