1
1
import { printSchema , parse , visit , ASTKindToNode , NamedTypeNode , TypeNode , VisitFn } from 'graphql' ;
2
2
import casual from 'casual' ;
3
3
import { PluginFunction } from '@graphql-codegen/plugin-helpers' ;
4
- import { pascalCase } from 'pascal-case' ;
5
-
6
- export function toPascalCase ( str : string ) {
7
- if ( str . charAt ( 0 ) === '_' ) {
8
- return str . replace ( / ^ ( _ * ) ( .* ) / , ( _match , underscorePrefix , typeName ) => `${ underscorePrefix } ${ pascalCase ( typeName || '' ) } ` ) ;
9
- }
4
+ import { pascalCase } from 'pascal-case' ;
5
+
6
+ export function toPascalCase ( str : string ) {
7
+ if ( str . charAt ( 0 ) === '_' ) {
8
+ return str . replace (
9
+ / ^ ( _ * ) ( .* ) / ,
10
+ ( _match , underscorePrefix , typeName ) => `${ underscorePrefix } ${ pascalCase ( typeName || '' ) } ` ,
11
+ ) ;
12
+ }
10
13
11
- return pascalCase ( str || '' ) ;
14
+ return pascalCase ( str || '' ) ;
12
15
}
13
16
14
17
const toMockName = ( name : string ) => {
@@ -22,7 +25,7 @@ const hashedString = (value: string) => {
22
25
return hash ;
23
26
}
24
27
for ( let i = 0 ; i < value . length ; i ++ ) {
25
- let char = value . charCodeAt ( i ) ;
28
+ const char = value . charCodeAt ( i ) ;
26
29
// eslint-disable-next-line no-bitwise
27
30
hash = ( hash << 5 ) - hash + char ;
28
31
// eslint-disable-next-line no-bitwise
@@ -56,14 +59,15 @@ const getNamedType = (
56
59
return casual . integer ( 0 , 9999 ) ;
57
60
case 'Date' :
58
61
return `'${ new Date ( casual . unix_time ) . toISOString ( ) } '` ;
59
- default :
60
- const foundType = types . find ( enumType => enumType . name === name ) ;
62
+ default : {
63
+ const foundType = types . find ( ( enumType : TypeItem ) => enumType . name === name ) ;
61
64
if ( foundType ) {
62
65
switch ( foundType . type ) {
63
- case 'enum' :
66
+ case 'enum' : {
64
67
// It's an enum
65
68
const value = foundType . values ? foundType . values [ 0 ] : '' ;
66
69
return `${ foundType . name } .${ toPascalCase ( value ) } ` ;
70
+ }
67
71
case 'union' :
68
72
// Return the first union type node.
69
73
return getNamedType ( typeName , fieldName , types , foundType . types && foundType . types [ 0 ] ) ;
@@ -72,6 +76,7 @@ const getNamedType = (
72
76
}
73
77
}
74
78
return `${ toMockName ( name ) } ()` ;
79
+ }
75
80
}
76
81
} ;
77
82
@@ -86,9 +91,10 @@ const generateMockValue = (
86
91
return getNamedType ( typeName , fieldName , types , currentType as NamedTypeNode ) ;
87
92
case 'NonNullType' :
88
93
return generateMockValue ( typeName , fieldName , types , currentType . type ) ;
89
- case 'ListType' :
94
+ case 'ListType' : {
90
95
const value = generateMockValue ( typeName , fieldName , types , currentType . type ) ;
91
96
return `[${ value } ]` ;
97
+ }
92
98
}
93
99
} ;
94
100
@@ -126,27 +132,27 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
126
132
// List of types that are enums
127
133
const types : TypeItem [ ] = [ ] ;
128
134
const visitor : VisitorType = {
129
- EnumTypeDefinition : node => {
135
+ EnumTypeDefinition : ( node ) => {
130
136
const name = node . name . value ;
131
137
if ( ! types . find ( ( enumType : TypeItem ) => enumType . name === name ) ) {
132
138
types . push ( {
133
139
name,
134
140
type : 'enum' ,
135
- values : node . values ? node . values . map ( node => node . name . value ) : [ ] ,
141
+ values : node . values ? node . values . map ( ( node ) => node . name . value ) : [ ] ,
136
142
} ) ;
137
143
}
138
144
} ,
139
- UnionTypeDefinition : node => {
145
+ UnionTypeDefinition : ( node ) => {
140
146
const name = node . name . value ;
141
- if ( ! types . find ( enumType => enumType . name === name ) ) {
147
+ if ( ! types . find ( ( enumType ) => enumType . name === name ) ) {
142
148
types . push ( {
143
149
name,
144
150
type : 'union' ,
145
151
types : node . types ,
146
152
} ) ;
147
153
}
148
154
} ,
149
- FieldDefinition : node => {
155
+ FieldDefinition : ( node ) => {
150
156
const fieldName = node . name . value ;
151
157
152
158
return {
@@ -158,15 +164,15 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
158
164
} ,
159
165
} ;
160
166
} ,
161
- InputObjectTypeDefinition : node => {
167
+ InputObjectTypeDefinition : ( node ) => {
162
168
const fieldName = node . name . value ;
163
169
164
170
return {
165
171
typeName : fieldName ,
166
172
mockFn : ( ) => {
167
173
const mockFields = node . fields
168
174
? node . fields
169
- . map ( field => {
175
+ . map ( ( field ) => {
170
176
const value = generateMockValue ( fieldName , field . name . value , types , field . type ) ;
171
177
172
178
return ` ${ field . name . value } : ${ value } ,` ;
@@ -178,7 +184,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
178
184
} ,
179
185
} ;
180
186
} ,
181
- ObjectTypeDefinition : node => {
187
+ ObjectTypeDefinition : ( node ) => {
182
188
// This function triggered per each type
183
189
const typeName = node . name . value ;
184
190
0 commit comments