@@ -101,6 +101,33 @@ const getScalarDefinition = (value: ScalarDefinition | ScalarGeneratorName): Sca
101
101
return value ;
102
102
} ;
103
103
104
+ const getCustomScalarValue = ( customScalar : ScalarDefinition , opts : Options < NamedTypeNode > ) => {
105
+ // If there is a mapping to a `casual` type, then use it and make sure
106
+ // to call it if it's a function
107
+ const embeddedGenerator = casual [ customScalar . generator ] ;
108
+ if ( ! embeddedGenerator && customScalar . generator ) {
109
+ return customScalar . generator ;
110
+ }
111
+
112
+ const generatorArgs : unknown [ ] = Array . isArray ( customScalar . arguments )
113
+ ? customScalar . arguments
114
+ : [ customScalar . arguments ] ;
115
+ if ( opts . dynamicValues ) {
116
+ return `casual['${ customScalar . generator } ']${
117
+ typeof embeddedGenerator === 'function' ? `(...${ JSON . stringify ( generatorArgs ) } )` : ''
118
+ } `;
119
+ }
120
+ const value = typeof embeddedGenerator === 'function' ? embeddedGenerator ( ...generatorArgs ) : embeddedGenerator ;
121
+
122
+ if ( typeof value === 'string' ) {
123
+ return `'${ value } '` ;
124
+ }
125
+ if ( typeof value === 'object' ) {
126
+ return `${ JSON . stringify ( value ) } ` ;
127
+ }
128
+ return value ;
129
+ } ;
130
+
104
131
const getNamedType = ( opts : Options < NamedTypeNode > ) : string | number | boolean => {
105
132
if ( ! opts . currentType ) {
106
133
return '' ;
@@ -114,16 +141,26 @@ const getNamedType = (opts: Options<NamedTypeNode>): string | number | boolean =
114
141
const name = opts . currentType . name . value ;
115
142
const casedName = createNameConverter ( opts . typenamesConvention , opts . transformUnderscore ) ( name ) ;
116
143
switch ( name ) {
117
- case 'String' :
118
- return mockValueGenerator . word ( ) ;
119
- case 'Float' :
120
- return mockValueGenerator . float ( ) ;
121
- case 'ID' :
122
- return mockValueGenerator . uuid ( ) ;
123
- case 'Boolean' :
124
- return mockValueGenerator . boolean ( ) ;
125
- case 'Int' :
126
- return mockValueGenerator . integer ( ) ;
144
+ case 'String' : {
145
+ const customScalar = opts . customScalars ? getScalarDefinition ( opts . customScalars [ 'String' ] ) : null ;
146
+ return customScalar ? getCustomScalarValue ( customScalar , opts ) : mockValueGenerator . word ( ) ;
147
+ }
148
+ case 'Float' : {
149
+ const customScalar = opts . customScalars ? getScalarDefinition ( opts . customScalars [ 'Float' ] ) : null ;
150
+ return customScalar ? getCustomScalarValue ( customScalar , opts ) : mockValueGenerator . float ( ) ;
151
+ }
152
+ case 'ID' : {
153
+ const customScalar = opts . customScalars ? getScalarDefinition ( opts . customScalars [ 'ID' ] ) : null ;
154
+ return customScalar ? getCustomScalarValue ( customScalar , opts ) : mockValueGenerator . uuid ( ) ;
155
+ }
156
+ case 'Boolean' : {
157
+ const customScalar = opts . customScalars ? getScalarDefinition ( opts . customScalars [ 'Boolean' ] ) : null ;
158
+ return customScalar ? getCustomScalarValue ( customScalar , opts ) : mockValueGenerator . boolean ( ) ;
159
+ }
160
+ case 'Int' : {
161
+ const customScalar = opts . customScalars ? getScalarDefinition ( opts . customScalars [ 'Int' ] ) : null ;
162
+ return customScalar ? getCustomScalarValue ( customScalar , opts ) : mockValueGenerator . integer ( ) ;
163
+ }
127
164
default : {
128
165
const foundType = opts . types . find ( ( enumType : TypeItem ) => enumType . name === name ) ;
129
166
if ( foundType ) {
@@ -160,34 +197,7 @@ const getNamedType = (opts: Options<NamedTypeNode>): string | number | boolean =
160
197
return mockValueGenerator . word ( ) ;
161
198
}
162
199
163
- // If there is a mapping to a `casual` type, then use it and make sure
164
- // to call it if it's a function
165
- const embeddedGenerator = casual [ customScalar . generator ] ;
166
-
167
- if ( ! embeddedGenerator && customScalar . generator ) {
168
- return customScalar . generator ;
169
- }
170
-
171
- const generatorArgs : unknown [ ] = Array . isArray ( customScalar . arguments )
172
- ? customScalar . arguments
173
- : [ customScalar . arguments ] ;
174
- if ( opts . dynamicValues ) {
175
- return `casual['${ customScalar . generator } ']${
176
- typeof embeddedGenerator === 'function' ? `(...${ JSON . stringify ( generatorArgs ) } )` : ''
177
- } `;
178
- }
179
- const value =
180
- typeof embeddedGenerator === 'function'
181
- ? embeddedGenerator ( ...generatorArgs )
182
- : embeddedGenerator ;
183
-
184
- if ( typeof value === 'string' ) {
185
- return `'${ value } '` ;
186
- }
187
- if ( typeof value === 'object' ) {
188
- return `${ JSON . stringify ( value ) } ` ;
189
- }
190
- return value ;
200
+ return getCustomScalarValue ( customScalar , opts ) ;
191
201
}
192
202
default :
193
203
throw `foundType is unknown: ${ foundType . name } : ${ foundType . type } ` ;
0 commit comments