Skip to content

Commit 832772b

Browse files
committed
🐛 fixed compileType
1 parent f7c3f4b commit 832772b

File tree

9 files changed

+43
-14
lines changed

9 files changed

+43
-14
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.4",
3+
"version": "0.1.5",
44
"private": false,
55
"license": "MIT",
66
"description": "GraphQL Parser providing simplier structure",

src/TreeToGraphQL/templates/ArgumentTemplate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ParserField } from '@/Models';
2-
import { getTypeName } from '@/shared/getTypeName';
2+
import { getTypeName } from '@/shared';
33
import { TemplateUtils } from './TemplateUtils';
44

55
/**

src/TreeToGraphQL/templates/DirectiveTemplate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ParserField } from '@/Models';
2-
import { getTypeName } from '@/shared/getTypeName';
2+
import { getTypeName } from '@/shared';
33
import { TemplateUtils } from './TemplateUtils';
44

55
/**

src/TreeToGraphQL/templates/TemplateUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
Value,
99
ValueDefinition,
1010
} from '@/Models';
11-
import { compileType } from '@/shared/getTypeName';
11+
import { compileType } from '@/shared';
1212
import { ArgumentTemplate } from './ArgumentTemplate';
1313
import { CommentTemplate } from './CommentTemplate';
1414
import { DirectiveTemplate } from './DirectiveTemplate';

src/TreeToGraphQL/templates/UnionMemberTemplate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ParserField } from '@/Models';
2-
import { getTypeName } from '@/shared/getTypeName';
2+
import { getTypeName } from '@/shared';
33

44
/**
55
* Template for union member represented in GraphQL Union - `type U = A | B` where A and B are union members

src/TreeToGraphQL/templates/ValueTemplate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ParserField, Value } from '@/Models';
2-
import { getTypeName } from '@/shared/getTypeName';
2+
import { getTypeName } from '@/shared';
33
import { TemplateUtils } from './TemplateUtils';
44

55
/**

src/__tests__/TreeToGraphQL/TemplateUtils.spec.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { TemplateUtils } from '@/TreeToGraphQL/templates/TemplateUtils';
2-
import { Options, ParserField, TypeSystemDefinition } from '../../Models';
1+
import { compileType, decompileType } from '@/shared';
2+
import { FieldType, Options, ParserField, TypeSystemDefinition } from '../../Models';
33

44
describe('TemplateUtils tests on parser', () => {
55
test(`ListType fields`, () => {
@@ -25,7 +25,36 @@ describe('TemplateUtils tests on parser', () => {
2525
args: [],
2626
};
2727

28-
const graphql = TemplateUtils.resolveFieldType(treeMock.type.fieldType);
28+
const graphql = compileType(treeMock.type.fieldType);
2929
expect(graphql).toContain(`[Person]!`);
3030
});
31+
test(`ListType fields 2`, () => {
32+
const treeMock: FieldType = {
33+
type: Options.required,
34+
nest: {
35+
type: Options.array,
36+
nest: {
37+
type: Options.array,
38+
nest: {
39+
type: Options.required,
40+
nest: {
41+
type: Options.name,
42+
name: 'Person',
43+
},
44+
},
45+
},
46+
},
47+
};
48+
49+
const graphql = compileType(treeMock);
50+
expect(graphql).toContain(`[[Person!]]!`);
51+
});
52+
test(`ListType fields 3`, () => {
53+
const ft = decompileType(`[[Person]!]!`);
54+
expect(compileType(ft)).toContain(`[[Person]!]!`);
55+
});
56+
test(`ListType fields 4`, () => {
57+
const ft = decompileType(`[[Person]!]!`);
58+
expect(compileType(ft)).toContain(`[[Person]!]!`);
59+
});
3160
});

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export * from './TreeToGraphQL';
22
export * from './Parser';
33
export * from './Models';
4-
export * from './shared/getTypeName';
4+
export * from './shared/index';
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ export const getTypeName = (f: FieldType): string => {
77
return getTypeName(f.nest);
88
};
99

10-
export const compileType = (f: FieldType, fn: (x: string) => string = (x) => x, required = false): string => {
10+
export const compileType = (f: FieldType): string => {
1111
if (f.type === Options.array) {
12-
return compileType(f.nest, (x) => (required ? `[${fn(x)}]!` : `[${fn(x)}]`));
12+
return `[${compileType(f.nest)}]`;
1313
}
1414
if (f.type === Options.required) {
15-
return compileType(f.nest, fn, true);
15+
return `${compileType(f.nest)}!`;
1616
}
17-
return required ? fn(`${f.name}!`) : fn(f.name);
17+
return f.name;
1818
};
1919

2020
export const decompileType = (typeName: string): FieldType => {

0 commit comments

Comments
 (0)