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