File tree Expand file tree Collapse file tree 3 files changed +30
-12
lines changed
Expand file tree Collapse file tree 3 files changed +30
-12
lines changed Original file line number Diff line number Diff line change 11{
22 "name" : " graphql-js-tree" ,
3- "version" : " 0.1.2 " ,
3+ "version" : " 0.1.3 " ,
44 "private" : false ,
55 "license" : " MIT" ,
66 "description" : " GraphQL Parser providing simplier structure" ,
Original file line number Diff line number Diff line change @@ -34,16 +34,13 @@ export class TemplateUtils {
3434 fn : ( x : string ) => string = ( x ) => x ,
3535 required = false ,
3636 ) : string => {
37- if ( f . type === Options . name && f . name ) {
38- return fn ( required ? `${ f . name } !` : f . name ) ;
39- }
4037 if ( f . type === Options . array ) {
4138 return TemplateUtils . resolveFieldType ( f . nest , ( x ) => ( required ? `[${ fn ( x ) } ]!` : `[${ fn ( x ) } ]` ) ) ;
4239 }
4340 if ( f . type === Options . required ) {
4441 return TemplateUtils . resolveFieldType ( f . nest , fn , true ) ;
4542 }
46- throw new Error ( 'Invalid field type:' + JSON . stringify ( f ) ) ;
43+ return fn ( required ? ` ${ f . name } !` : f . name ) ;
4744 } ;
4845 /**
4946 *
Original file line number Diff line number Diff line change @@ -7,12 +7,33 @@ 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 ) : string => {
11- if ( f . type === Options . name ) {
12- return fn ( f . name ) ;
13- } else if ( f . type === Options . array ) {
14- return compileType ( f . nest , ( x ) => `[${ fn ( x ) } ]` ) ;
15- } else {
16- return compileType ( f . nest , ( x ) => `${ fn ( x ) } !` ) ;
10+ export const compileType = ( f : FieldType , fn : ( x : string ) => string = ( x ) => x , required = false ) : string => {
11+ if ( f . type === Options . array ) {
12+ return compileType ( f . nest , ( x ) => ( required ? `[${ fn ( x ) } ]!` : `[${ fn ( x ) } ]` ) ) ;
13+ }
14+ if ( f . type === Options . required ) {
15+ return compileType ( f . nest , fn , true ) ;
16+ }
17+ return fn ( f . name ) ;
18+ } ;
19+
20+ export const decompileType = ( typeName : string ) : FieldType => {
21+ const arrayType = typeName . endsWith ( ']' ) ;
22+ const requiredType = typeName . endsWith ( '!' ) ;
23+ if ( arrayType ) {
24+ return {
25+ type : Options . array ,
26+ nest : decompileType ( typeName . substring ( 1 , typeName . length - 1 ) ) ,
27+ } ;
28+ }
29+ if ( requiredType ) {
30+ return {
31+ type : Options . required ,
32+ nest : decompileType ( typeName . substring ( 0 , typeName . length - 1 ) ) ,
33+ } ;
1734 }
35+ return {
36+ type : Options . name ,
37+ name : typeName ,
38+ } ;
1839} ;
You can’t perform that action at this time.
0 commit comments