@@ -4,16 +4,29 @@ import { PluginFunction } from '@graphql-codegen/plugin-helpers';
4
4
import { pascalCase } from 'pascal-case' ;
5
5
import { upperCase } from 'upper-case' ;
6
6
7
- type EnumValuesTypes = 'upper-case#upperCase' | 'pascal-case#pascalCase' ;
7
+ type NamingConvention = 'upper-case#upperCase' | 'pascal-case#pascalCase' | 'keep' ;
8
+
9
+ const createNameConverter = ( convention : NamingConvention ) => ( value : string ) => {
10
+ switch ( convention ) {
11
+ case 'upper-case#upperCase' :
12
+ return upperCase ( value || '' ) ;
13
+ case 'keep' :
14
+ return value ;
15
+ case 'pascal-case#pascalCase' :
16
+ // fallthrough
17
+ default :
18
+ // default to pascal case in case of unknown values
19
+ return pascalCase ( value || '' ) ;
20
+ }
21
+ } ;
8
22
9
23
const toMockName = ( name : string ) => {
10
24
const isVowel = name . match ( / ^ [ A E I O ] / ) ;
11
25
return isVowel ? `an${ name } ` : `a${ name } ` ;
12
26
} ;
13
27
14
- const updateTextCase = ( str : string , enumValues : EnumValuesTypes ) => {
15
- const convert = ( value : string ) =>
16
- enumValues === 'upper-case#upperCase' ? upperCase ( value || '' ) : pascalCase ( value || '' ) ;
28
+ const updateTextCase = ( str : string , enumValuesConvention : NamingConvention ) => {
29
+ const convert = createNameConverter ( enumValuesConvention ) ;
17
30
18
31
if ( str . charAt ( 0 ) === '_' ) {
19
32
return str . replace (
@@ -44,7 +57,8 @@ const getNamedType = (
44
57
typeName : string ,
45
58
fieldName : string ,
46
59
types : TypeItem [ ] ,
47
- enumValues : EnumValuesTypes ,
60
+ typenamesConvention : NamingConvention ,
61
+ enumValuesConvention : NamingConvention ,
48
62
namedType ?: NamedTypeNode ,
49
63
) : string | number | boolean => {
50
64
if ( ! namedType ) {
@@ -72,16 +86,18 @@ const getNamedType = (
72
86
switch ( foundType . type ) {
73
87
case 'enum' : {
74
88
// It's an enum
89
+ const typenameConverter = createNameConverter ( typenamesConvention ) ;
75
90
const value = foundType . values ? foundType . values [ 0 ] : '' ;
76
- return `${ foundType . name } .${ updateTextCase ( value , enumValues ) } ` ;
91
+ return `${ typenameConverter ( foundType . name ) } .${ updateTextCase ( value , enumValuesConvention ) } ` ;
77
92
}
78
93
case 'union' :
79
94
// Return the first union type node.
80
95
return getNamedType (
81
96
typeName ,
82
97
fieldName ,
83
98
types ,
84
- enumValues ,
99
+ typenamesConvention ,
100
+ enumValuesConvention ,
85
101
foundType . types && foundType . types [ 0 ] ,
86
102
) ;
87
103
default :
@@ -97,25 +113,53 @@ const generateMockValue = (
97
113
typeName : string ,
98
114
fieldName : string ,
99
115
types : TypeItem [ ] ,
100
- enumValues : EnumValuesTypes ,
116
+ typenamesConvention : NamingConvention ,
117
+ enumValuesConvention : NamingConvention ,
101
118
currentType : TypeNode ,
102
119
) : string | number | boolean => {
103
120
switch ( currentType . kind ) {
104
121
case 'NamedType' :
105
- return getNamedType ( typeName , fieldName , types , enumValues , currentType as NamedTypeNode ) ;
122
+ return getNamedType (
123
+ typeName ,
124
+ fieldName ,
125
+ types ,
126
+ typenamesConvention ,
127
+ enumValuesConvention ,
128
+ currentType as NamedTypeNode ,
129
+ ) ;
106
130
case 'NonNullType' :
107
- return generateMockValue ( typeName , fieldName , types , enumValues , currentType . type ) ;
131
+ return generateMockValue (
132
+ typeName ,
133
+ fieldName ,
134
+ types ,
135
+ typenamesConvention ,
136
+ enumValuesConvention ,
137
+ currentType . type ,
138
+ ) ;
108
139
case 'ListType' : {
109
- const value = generateMockValue ( typeName , fieldName , types , enumValues , currentType . type ) ;
140
+ const value = generateMockValue (
141
+ typeName ,
142
+ fieldName ,
143
+ types ,
144
+ typenamesConvention ,
145
+ enumValuesConvention ,
146
+ currentType . type ,
147
+ ) ;
110
148
return `[${ value } ]` ;
111
149
}
112
150
}
113
151
} ;
114
152
115
- const getMockString = ( typeName : string , fields : string , addTypename = false ) => {
116
- const typename = addTypename ? `\n __typename: '${ typeName } ',` : '' ;
153
+ const getMockString = (
154
+ typeName : string ,
155
+ fields : string ,
156
+ typenamesConvention : NamingConvention ,
157
+ addTypename = false ,
158
+ ) => {
159
+ const casedName = createNameConverter ( typenamesConvention ) ( typeName ) ;
160
+ const typename = addTypename ? `\n __typename: '${ casedName } ',` : '' ;
117
161
return `
118
- export const ${ toMockName ( typeName ) } = (overrides?: Partial<${ typeName } >): ${ typeName } => {
162
+ export const ${ toMockName ( casedName ) } = (overrides?: Partial<${ casedName } >): ${ casedName } => {
119
163
return {${ typename }
120
164
${ fields }
121
165
};
@@ -124,7 +168,8 @@ ${fields}
124
168
125
169
export interface TypescriptMocksPluginConfig {
126
170
typesFile ?: string ;
127
- enumValues ?: EnumValuesTypes ;
171
+ enumValues ?: NamingConvention ;
172
+ typenames ?: NamingConvention ;
128
173
addTypename ?: boolean ;
129
174
}
130
175
@@ -144,7 +189,8 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
144
189
const printedSchema = printSchema ( schema ) ; // Returns a string representation of the schema
145
190
const astNode = parse ( printedSchema ) ; // Transforms the string into ASTNode
146
191
147
- const enumValues = config . enumValues || 'pascal-case#pascalCase' ;
192
+ const enumValuesConvention = config . enumValues || 'pascal-case#pascalCase' ;
193
+ const typenamesConvention = config . typenames || 'pascal-case#pascalCase' ;
148
194
// List of types that are enums
149
195
const types : TypeItem [ ] = [ ] ;
150
196
const visitor : VisitorType = {
@@ -174,7 +220,14 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
174
220
return {
175
221
name : fieldName ,
176
222
mockFn : ( typeName : string ) => {
177
- const value = generateMockValue ( typeName , fieldName , types , enumValues , node . type ) ;
223
+ const value = generateMockValue (
224
+ typeName ,
225
+ fieldName ,
226
+ types ,
227
+ typenamesConvention ,
228
+ enumValuesConvention ,
229
+ node . type ,
230
+ ) ;
178
231
179
232
return ` ${ fieldName } : overrides && overrides.hasOwnProperty('${ fieldName } ') ? overrides.${ fieldName } ! : ${ value } ,` ;
180
233
} ,
@@ -193,7 +246,8 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
193
246
fieldName ,
194
247
field . name . value ,
195
248
types ,
196
- enumValues ,
249
+ typenamesConvention ,
250
+ enumValuesConvention ,
197
251
field . type ,
198
252
) ;
199
253
@@ -202,7 +256,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
202
256
. join ( '\n' )
203
257
: '' ;
204
258
205
- return getMockString ( fieldName , mockFields , false ) ;
259
+ return getMockString ( fieldName , mockFields , typenamesConvention , false ) ;
206
260
} ,
207
261
} ;
208
262
} ,
@@ -220,7 +274,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
220
274
mockFn : ( ) => {
221
275
const mockFields = fields ? fields . map ( ( { mockFn } : any ) => mockFn ( typeName ) ) . join ( '\n' ) : '' ;
222
276
223
- return getMockString ( typeName , mockFields , ! ! config . addTypename ) ;
277
+ return getMockString ( typeName , mockFields , typenamesConvention , ! ! config . addTypename ) ;
224
278
} ,
225
279
} ;
226
280
} ,
0 commit comments