@@ -3,16 +3,15 @@ const fs = require('fs-extra');
3
3
const { parse } = require ( 'graphql' ) ;
4
4
const glob = require ( 'glob-all' ) ;
5
5
const { FeatureFlags, pathManager } = require ( '@aws-amplify/amplify-cli-core' ) ;
6
- const gqlCodeGen = require ( '@graphql-codegen/core' ) ;
7
- const appSyncDataStoreCodeGen = require ( '@aws-amplify/appsync-modelgen-plugin' ) ;
8
- const { version : packageVersion } = require ( '../../package.json' ) ;
6
+ const { generateModels : generateModelsHelper } = require ( '@aws-amplify/graphql-generator' ) ;
9
7
const { validateAmplifyFlutterMinSupportedVersion } = require ( '../utils/validateAmplifyFlutterMinSupportedVersion' ) ;
10
8
11
9
const platformToLanguageMap = {
12
10
android : 'java' ,
13
11
ios : 'swift' ,
14
12
flutter : 'dart' ,
15
13
javascript : 'javascript' ,
14
+ typescript : 'typescript' ,
16
15
} ;
17
16
18
17
/**
@@ -91,9 +90,7 @@ async function generateModels(context, generateOptions = null) {
91
90
} ) ;
92
91
93
92
const schemaContent = loadSchema ( apiResourcePath ) ;
94
-
95
- const baseOutputDir = path . join ( projectRoot , getModelOutputPath ( context ) ) ;
96
- const schema = parse ( schemaContent ) ;
93
+ const baseOutputDir = overrideOutputDir || path . join ( projectRoot , getModelOutputPath ( context ) ) ;
97
94
const projectConfig = context . amplify . getProjectConfig ( ) ;
98
95
99
96
if ( ! isIntrospection && projectConfig . frontend === 'flutter' && ! validateAmplifyFlutterMinSupportedVersion ( projectRoot ) ) {
@@ -104,64 +101,42 @@ Amplify Flutter versions prior to 0.6.0 are no longer supported by codegen. Plea
104
101
105
102
const generateIndexRules = readFeatureFlag ( 'codegen.generateIndexRules' ) ;
106
103
const emitAuthProvider = readFeatureFlag ( 'codegen.emitAuthProvider' ) ;
107
- const usePipelinedTransformer = readFeatureFlag ( 'graphQLTransformer.useExperimentalPipelinedTransformer' ) ;
104
+ const useExperimentalPipelinedTransformer = readFeatureFlag ( 'graphQLTransformer.useExperimentalPipelinedTransformer' ) ;
108
105
const transformerVersion = readNumericFeatureFlag ( 'graphQLTransformer.transformerVersion' ) ;
109
106
const respectPrimaryKeyAttributesOnConnectionField = readFeatureFlag ( 'graphQLTransformer.respectPrimaryKeyAttributesOnConnectionField' ) ;
110
107
const generateModelsForLazyLoadAndCustomSelectionSet = readFeatureFlag ( 'codegen.generateModelsForLazyLoadAndCustomSelectionSet' ) ;
111
108
const improvePluralization = readFeatureFlag ( 'graphQLTransformer.improvePluralization' ) ;
112
-
113
- let isTimestampFieldsAdded = readFeatureFlag ( 'codegen.addTimestampFields' ) ;
109
+ const addTimestampFields = readFeatureFlag ( 'codegen.addTimestampFields' ) ;
114
110
115
111
const handleListNullabilityTransparently = readFeatureFlag ( 'codegen.handleListNullabilityTransparently' ) ;
116
- const appsyncLocalConfig = await appSyncDataStoreCodeGen . preset . buildGeneratesSection ( {
117
- baseOutputDir,
118
- schema,
119
- config : {
120
- target : isIntrospection ? 'introspection' : platformToLanguageMap [ projectConfig . frontend ] || projectConfig . frontend ,
121
- directives : directiveDefinitions ,
122
- isTimestampFieldsAdded,
123
- emitAuthProvider,
124
- generateIndexRules,
125
- handleListNullabilityTransparently,
126
- usePipelinedTransformer,
127
- transformerVersion,
128
- respectPrimaryKeyAttributesOnConnectionField,
129
- improvePluralization,
130
- generateModelsForLazyLoadAndCustomSelectionSet,
131
- codegenVersion : packageVersion ,
132
- overrideOutputDir, // This needs to live under `config` in order for the GraphQL types to work out.
133
- } ,
134
- } ) ;
135
112
136
- const codeGenPromises = appsyncLocalConfig . map ( cfg => {
137
- return gqlCodeGen . codegen ( {
138
- ...cfg ,
139
- plugins : [
140
- {
141
- appSyncLocalCodeGen : { } ,
142
- } ,
143
- ] ,
144
- pluginMap : {
145
- appSyncLocalCodeGen : appSyncDataStoreCodeGen ,
146
- } ,
147
- } ) ;
113
+ const generatedCode = await generateModelsHelper ( {
114
+ schema : schemaContent ,
115
+ directives : directiveDefinitions ,
116
+ target : isIntrospection ? 'introspection' : platformToLanguageMap [ projectConfig . frontend ] ,
117
+ generateIndexRules,
118
+ emitAuthProvider,
119
+ useExperimentalPipelinedTransformer,
120
+ transformerVersion,
121
+ respectPrimaryKeyAttributesOnConnectionField,
122
+ improvePluralization,
123
+ generateModelsForLazyLoadAndCustomSelectionSet,
124
+ addTimestampFields,
125
+ handleListNullabilityTransparently,
148
126
} ) ;
149
127
150
- const generatedCode = await Promise . all ( codeGenPromises ) ;
151
-
152
128
if ( writeToDisk ) {
153
- appsyncLocalConfig . forEach ( ( cfg , idx ) => {
154
- const outPutPath = cfg . filename ;
155
- fs . ensureFileSync ( outPutPath ) ;
156
- fs . writeFileSync ( outPutPath , generatedCode [ idx ] ) ;
129
+ Object . entries ( generatedCode ) . forEach ( ( [ filepath , contents ] ) => {
130
+ fs . outputFileSync ( path . resolve ( path . join ( baseOutputDir , filepath ) ) , contents ) ;
157
131
} ) ;
158
132
133
+ // TODO: move to @aws -amplify/graphql-generator
159
134
generateEslintIgnore ( context ) ;
160
135
161
- context . print . info ( `Successfully generated models. Generated models can be found in ${ overrideOutputDir ?? baseOutputDir } ` ) ;
136
+ context . print . info ( `Successfully generated models. Generated models can be found in ${ baseOutputDir } ` ) ;
162
137
}
163
138
164
- return generatedCode ;
139
+ return Object . values ( generatedCode ) ;
165
140
}
166
141
167
142
async function validateSchema ( context ) {
@@ -196,9 +171,12 @@ function getModelOutputPath(context) {
196
171
const projectConfig = context . amplify . getProjectConfig ( ) ;
197
172
switch ( projectConfig . frontend ) {
198
173
case 'javascript' :
199
- return projectConfig . javascript && projectConfig . javascript . config && projectConfig . javascript . config . SourceDir
200
- ? path . normalize ( projectConfig . javascript . config . SourceDir )
201
- : 'src' ;
174
+ return path . join (
175
+ projectConfig . javascript && projectConfig . javascript . config && projectConfig . javascript . config . SourceDir
176
+ ? path . normalize ( projectConfig . javascript . config . SourceDir )
177
+ : 'src' ,
178
+ 'models' ,
179
+ ) ;
202
180
case 'android' :
203
181
return projectConfig . android && projectConfig . android . config && projectConfig . android . config . ResDir
204
182
? path . normalize ( path . join ( projectConfig . android . config . ResDir , '..' , 'java' ) )
0 commit comments