@@ -4,6 +4,7 @@ import { join } from 'path';
4
4
import { JAVA_SCALAR_MAP , SWIFT_SCALAR_MAP , TYPESCRIPT_SCALAR_MAP , DART_SCALAR_MAP , METADATA_SCALAR_MAP } from './scalars' ;
5
5
import { LOADER_CLASS_NAME , GENERATED_PACKAGE_NAME } from './configs/java-config' ;
6
6
import { graphqlName , toUpper } from 'graphql-transformer-common' ;
7
+ import { SyncTypes } from './types/sync' ;
7
8
8
9
const APPSYNC_DATA_STORE_CODEGEN_TARGETS = [ 'java' , 'swift' , 'javascript' , 'typescript' , 'dart' , 'introspection' ] ;
9
10
@@ -31,12 +32,25 @@ export type AppSyncModelCodeGenPresetConfig = {
31
32
isDataStoreEnabled ?: boolean ;
32
33
} ;
33
34
35
+ /**
36
+ * NOTE: The different codegen target presets restructure the options to meet the needs of the target plugin
37
+ * None of this remapping interacts with the pluginMap or cache interface, so we can reuse all logic if we strip out
38
+ * the pluginMap and cache, then re-introduce them in the returned preset.
39
+ */
40
+
41
+ /**
42
+ * Internal types that represent the options without the pluginMap and cache, which we will use in each of our
43
+ * target preset option construction implementations
44
+ */
45
+ type GenerateOptions = Omit < SyncTypes . GenerateOptions , 'cache' | 'pluginMap' > ;
46
+ type PresetFnArgs = Omit < SyncTypes . PresetFnArgs < AppSyncModelCodeGenPresetConfig > , 'cache' | 'pluginMap' > ;
47
+
34
48
const generateJavaPreset = (
35
- options : Types . PresetFnArgs < AppSyncModelCodeGenPresetConfig > ,
49
+ options : PresetFnArgs ,
36
50
models : TypeDefinitionNode [ ] ,
37
51
manyToManyJoinModels : TypeDefinitionNode [ ] ,
38
- ) : Types . GenerateOptions [ ] => {
39
- const config : Types . GenerateOptions [ ] = [ ] ;
52
+ ) : GenerateOptions [ ] => {
53
+ const config : GenerateOptions [ ] = [ ] ;
40
54
const modelFolder = options . config . overrideOutputDir
41
55
? [ options . config . overrideOutputDir ]
42
56
: [ options . baseOutputDir , ...GENERATED_PACKAGE_NAME . split ( '.' ) ] ;
@@ -105,10 +119,10 @@ const generateJavaPreset = (
105
119
} ;
106
120
107
121
const generateSwiftPreset = (
108
- options : Types . PresetFnArgs < AppSyncModelCodeGenPresetConfig > ,
122
+ options : PresetFnArgs ,
109
123
models : TypeDefinitionNode [ ] ,
110
- ) : Types . GenerateOptions [ ] => {
111
- const config : Types . GenerateOptions [ ] = [ ] ;
124
+ ) : GenerateOptions [ ] => {
125
+ const config : GenerateOptions [ ] = [ ] ;
112
126
const modelFolder = options . config . overrideOutputDir ? options . config . overrideOutputDir : options . baseOutputDir ;
113
127
models . forEach ( model => {
114
128
const modelName = model . name . value ;
@@ -152,10 +166,10 @@ const generateSwiftPreset = (
152
166
} ;
153
167
154
168
const generateTypeScriptPreset = (
155
- options : Types . PresetFnArgs < AppSyncModelCodeGenPresetConfig > ,
169
+ options : PresetFnArgs ,
156
170
models : TypeDefinitionNode [ ] ,
157
- ) : Types . GenerateOptions [ ] => {
158
- const config : Types . GenerateOptions [ ] = [ ] ;
171
+ ) : GenerateOptions [ ] => {
172
+ const config : GenerateOptions [ ] = [ ] ;
159
173
const modelFolder = options . config . overrideOutputDir ? options . config . overrideOutputDir : join ( options . baseOutputDir ) ;
160
174
config . push ( {
161
175
...options ,
@@ -181,10 +195,10 @@ const generateTypeScriptPreset = (
181
195
} ;
182
196
183
197
const generateJavasScriptPreset = (
184
- options : Types . PresetFnArgs < AppSyncModelCodeGenPresetConfig > ,
198
+ options : PresetFnArgs ,
185
199
models : TypeDefinitionNode [ ] ,
186
- ) : Types . GenerateOptions [ ] => {
187
- const config : Types . GenerateOptions [ ] = [ ] ;
200
+ ) : GenerateOptions [ ] => {
201
+ const config : GenerateOptions [ ] = [ ] ;
188
202
const modelFolder = options . config . overrideOutputDir ? options . config . overrideOutputDir : join ( options . baseOutputDir ) ;
189
203
config . push ( {
190
204
...options ,
@@ -234,10 +248,10 @@ const generateJavasScriptPreset = (
234
248
} ;
235
249
236
250
const generateDartPreset = (
237
- options : Types . PresetFnArgs < AppSyncModelCodeGenPresetConfig > ,
251
+ options : PresetFnArgs ,
238
252
models : TypeDefinitionNode [ ] ,
239
- ) : Types . GenerateOptions [ ] => {
240
- const config : Types . GenerateOptions [ ] = [ ] ;
253
+ ) : GenerateOptions [ ] => {
254
+ const config : GenerateOptions [ ] = [ ] ;
241
255
const modelFolder = options . config . overrideOutputDir ?? options . baseOutputDir ;
242
256
models . forEach ( model => {
243
257
const modelName = model . name . value ;
@@ -264,7 +278,7 @@ const generateDartPreset = (
264
278
return config ;
265
279
} ;
266
280
267
- const generateManyToManyModelStubs = ( options : Types . PresetFnArgs < AppSyncModelCodeGenPresetConfig > ) : TypeDefinitionNode [ ] => {
281
+ const generateManyToManyModelStubs = ( options : PresetFnArgs ) : TypeDefinitionNode [ ] => {
268
282
let models = new Array < TypeDefinitionNode > ( ) ;
269
283
let manyToManySet = new Set < string > ( ) ;
270
284
options . schema . definitions . forEach ( def => {
@@ -295,10 +309,10 @@ const generateManyToManyModelStubs = (options: Types.PresetFnArgs<AppSyncModelCo
295
309
} ;
296
310
297
311
const generateIntrospectionPreset = (
298
- options : Types . PresetFnArgs < AppSyncModelCodeGenPresetConfig > ,
312
+ options : PresetFnArgs ,
299
313
models : TypeDefinitionNode [ ] ,
300
- ) : Types . GenerateOptions [ ] => {
301
- const config : Types . GenerateOptions [ ] = [ ] ;
314
+ ) : GenerateOptions [ ] => {
315
+ const config : GenerateOptions [ ] = [ ] ;
302
316
// model-intropection.json
303
317
config . push ( {
304
318
...options ,
@@ -312,8 +326,7 @@ const generateIntrospectionPreset = (
312
326
return config ;
313
327
} ;
314
328
315
- export const preset : Types . OutputPreset < AppSyncModelCodeGenPresetConfig > = {
316
- buildGeneratesSection : ( options : Types . PresetFnArgs < AppSyncModelCodeGenPresetConfig > ) : Types . GenerateOptions [ ] => {
329
+ const buildGenerations = ( options : PresetFnArgs ) : GenerateOptions [ ] => {
317
330
const codeGenTarget = options . config . target ;
318
331
const typesToSkip : string [ ] = [ 'Query' , 'Mutation' , 'Subscription' ] ;
319
332
const models : TypeDefinitionNode [ ] = options . schema . definitions . filter (
@@ -346,5 +359,42 @@ export const preset: Types.OutputPreset<AppSyncModelCodeGenPresetConfig> = {
346
359
) } `,
347
360
) ;
348
361
}
349
- } ,
350
- } ;
362
+ } ;
363
+
364
+
365
+
366
+ /**
367
+ * @internal
368
+ * The presetSync interface uses our SyncTypes __without__ promise/async typing
369
+ */
370
+ export const presetSync : SyncTypes . OutputPreset < AppSyncModelCodeGenPresetConfig > = {
371
+ buildGeneratesSection : ( options : SyncTypes . PresetFnArgs < AppSyncModelCodeGenPresetConfig > ) : SyncTypes . GenerateOptions [ ] => {
372
+ // Extract cache and pluginMap from the options
373
+ const { cache, pluginMap, ...otherOptions } = options ;
374
+
375
+ // Generate the list of options and re-introduce the pluginMap and cache
376
+ return buildGenerations ( otherOptions ) . map ( ( config : GenerateOptions ) => ( {
377
+ pluginMap,
378
+ cache,
379
+ ...config ,
380
+ } ) )
381
+ }
382
+ }
383
+
384
+ /**
385
+ * @internal
386
+ * The preset interface uses the @graphql-codegen/core interfaces __with__ promise/async typing
387
+ */
388
+ export const preset : Types . OutputPreset < AppSyncModelCodeGenPresetConfig > = {
389
+ buildGeneratesSection : ( options : Types . PresetFnArgs < AppSyncModelCodeGenPresetConfig > ) : Types . GenerateOptions [ ] => {
390
+ // Extract cache and pluginMap from the options
391
+ const { cache, pluginMap, ...otherOptions } = options ;
392
+
393
+ // Generate the list of options and re-introduce the pluginMap and cache
394
+ return buildGenerations ( otherOptions ) . map ( ( config : GenerateOptions ) => ( {
395
+ pluginMap,
396
+ cache,
397
+ ...config ,
398
+ } ) )
399
+ }
400
+ }
0 commit comments