@@ -4,6 +4,7 @@ import { join } from 'path';
44import { JAVA_SCALAR_MAP , SWIFT_SCALAR_MAP , TYPESCRIPT_SCALAR_MAP , DART_SCALAR_MAP , METADATA_SCALAR_MAP } from './scalars' ;
55import { LOADER_CLASS_NAME , GENERATED_PACKAGE_NAME } from './configs/java-config' ;
66import { graphqlName , toUpper } from 'graphql-transformer-common' ;
7+ import { SyncTypes } from './types/sync' ;
78
89const APPSYNC_DATA_STORE_CODEGEN_TARGETS = [ 'java' , 'swift' , 'javascript' , 'typescript' , 'dart' , 'introspection' ] ;
910
@@ -31,12 +32,25 @@ export type AppSyncModelCodeGenPresetConfig = {
3132 isDataStoreEnabled ?: boolean ;
3233} ;
3334
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+
3448const generateJavaPreset = (
35- options : Types . PresetFnArgs < AppSyncModelCodeGenPresetConfig > ,
49+ options : PresetFnArgs ,
3650 models : TypeDefinitionNode [ ] ,
3751 manyToManyJoinModels : TypeDefinitionNode [ ] ,
38- ) : Types . GenerateOptions [ ] => {
39- const config : Types . GenerateOptions [ ] = [ ] ;
52+ ) : GenerateOptions [ ] => {
53+ const config : GenerateOptions [ ] = [ ] ;
4054 const modelFolder = options . config . overrideOutputDir
4155 ? [ options . config . overrideOutputDir ]
4256 : [ options . baseOutputDir , ...GENERATED_PACKAGE_NAME . split ( '.' ) ] ;
@@ -105,10 +119,10 @@ const generateJavaPreset = (
105119} ;
106120
107121const generateSwiftPreset = (
108- options : Types . PresetFnArgs < AppSyncModelCodeGenPresetConfig > ,
122+ options : PresetFnArgs ,
109123 models : TypeDefinitionNode [ ] ,
110- ) : Types . GenerateOptions [ ] => {
111- const config : Types . GenerateOptions [ ] = [ ] ;
124+ ) : GenerateOptions [ ] => {
125+ const config : GenerateOptions [ ] = [ ] ;
112126 const modelFolder = options . config . overrideOutputDir ? options . config . overrideOutputDir : options . baseOutputDir ;
113127 models . forEach ( model => {
114128 const modelName = model . name . value ;
@@ -152,10 +166,10 @@ const generateSwiftPreset = (
152166} ;
153167
154168const generateTypeScriptPreset = (
155- options : Types . PresetFnArgs < AppSyncModelCodeGenPresetConfig > ,
169+ options : PresetFnArgs ,
156170 models : TypeDefinitionNode [ ] ,
157- ) : Types . GenerateOptions [ ] => {
158- const config : Types . GenerateOptions [ ] = [ ] ;
171+ ) : GenerateOptions [ ] => {
172+ const config : GenerateOptions [ ] = [ ] ;
159173 const modelFolder = options . config . overrideOutputDir ? options . config . overrideOutputDir : join ( options . baseOutputDir ) ;
160174 config . push ( {
161175 ...options ,
@@ -181,10 +195,10 @@ const generateTypeScriptPreset = (
181195} ;
182196
183197const generateJavasScriptPreset = (
184- options : Types . PresetFnArgs < AppSyncModelCodeGenPresetConfig > ,
198+ options : PresetFnArgs ,
185199 models : TypeDefinitionNode [ ] ,
186- ) : Types . GenerateOptions [ ] => {
187- const config : Types . GenerateOptions [ ] = [ ] ;
200+ ) : GenerateOptions [ ] => {
201+ const config : GenerateOptions [ ] = [ ] ;
188202 const modelFolder = options . config . overrideOutputDir ? options . config . overrideOutputDir : join ( options . baseOutputDir ) ;
189203 config . push ( {
190204 ...options ,
@@ -234,10 +248,10 @@ const generateJavasScriptPreset = (
234248} ;
235249
236250const generateDartPreset = (
237- options : Types . PresetFnArgs < AppSyncModelCodeGenPresetConfig > ,
251+ options : PresetFnArgs ,
238252 models : TypeDefinitionNode [ ] ,
239- ) : Types . GenerateOptions [ ] => {
240- const config : Types . GenerateOptions [ ] = [ ] ;
253+ ) : GenerateOptions [ ] => {
254+ const config : GenerateOptions [ ] = [ ] ;
241255 const modelFolder = options . config . overrideOutputDir ?? options . baseOutputDir ;
242256 models . forEach ( model => {
243257 const modelName = model . name . value ;
@@ -264,7 +278,7 @@ const generateDartPreset = (
264278 return config ;
265279} ;
266280
267- const generateManyToManyModelStubs = ( options : Types . PresetFnArgs < AppSyncModelCodeGenPresetConfig > ) : TypeDefinitionNode [ ] => {
281+ const generateManyToManyModelStubs = ( options : PresetFnArgs ) : TypeDefinitionNode [ ] => {
268282 let models = new Array < TypeDefinitionNode > ( ) ;
269283 let manyToManySet = new Set < string > ( ) ;
270284 options . schema . definitions . forEach ( def => {
@@ -295,10 +309,10 @@ const generateManyToManyModelStubs = (options: Types.PresetFnArgs<AppSyncModelCo
295309} ;
296310
297311const generateIntrospectionPreset = (
298- options : Types . PresetFnArgs < AppSyncModelCodeGenPresetConfig > ,
312+ options : PresetFnArgs ,
299313 models : TypeDefinitionNode [ ] ,
300- ) : Types . GenerateOptions [ ] => {
301- const config : Types . GenerateOptions [ ] = [ ] ;
314+ ) : GenerateOptions [ ] => {
315+ const config : GenerateOptions [ ] = [ ] ;
302316 // model-intropection.json
303317 config . push ( {
304318 ...options ,
@@ -312,8 +326,7 @@ const generateIntrospectionPreset = (
312326 return config ;
313327} ;
314328
315- export const preset : Types . OutputPreset < AppSyncModelCodeGenPresetConfig > = {
316- buildGeneratesSection : ( options : Types . PresetFnArgs < AppSyncModelCodeGenPresetConfig > ) : Types . GenerateOptions [ ] => {
329+ const buildGenerations = ( options : PresetFnArgs ) : GenerateOptions [ ] => {
317330 const codeGenTarget = options . config . target ;
318331 const typesToSkip : string [ ] = [ 'Query' , 'Mutation' , 'Subscription' ] ;
319332 const models : TypeDefinitionNode [ ] = options . schema . definitions . filter (
@@ -346,5 +359,42 @@ export const preset: Types.OutputPreset<AppSyncModelCodeGenPresetConfig> = {
346359 ) } `,
347360 ) ;
348361 }
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