@@ -7,7 +7,7 @@ const constants = require('../constants');
7
7
const { loadConfig } = require ( '../codegen-config' ) ;
8
8
const { ensureIntrospectionSchema, getFrontEndHandler, getAppSyncAPIDetails, getAppSyncAPIInfoFromProject } = require ( '../utils' ) ;
9
9
const { generateTypes : generateTypesHelper } = require ( '@aws-amplify/graphql-generator' ) ;
10
- const { extractDocumentFromJavascript } = require ( '@aws-amplify/graphql-types-generator' ) ;
10
+ const { generate , extractDocumentFromJavascript } = require ( '@aws-amplify/graphql-types-generator' ) ;
11
11
12
12
async function generateTypes ( context , forceDownloadSchema , withoutInit = false , decoupleFrontend = '' ) {
13
13
let frontend = decoupleFrontend ;
@@ -57,25 +57,24 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
57
57
const target = cfg . amplifyExtension . codeGenTarget ;
58
58
59
59
const excludes = cfg . excludes . map ( pattern => `!${ pattern } ` ) ;
60
- const queryFiles = glob
61
- . sync ( [ ...includeFiles , ...excludes ] , {
62
- cwd : projectPath ,
63
- absolute : true ,
64
- } )
65
- . map ( queryFilePath => {
66
- const fileContents = fs . readFileSync ( queryFilePath , 'utf8' ) ;
67
- if (
68
- queryFilePath . endsWith ( '.jsx' ) ||
69
- queryFilePath . endsWith ( '.js' ) ||
70
- queryFilePath . endsWith ( '.tsx' ) ||
71
- queryFilePath . endsWith ( '.ts' )
72
- ) {
73
- return extractDocumentFromJavascript ( fileContents , '' ) ;
74
- }
75
- return fileContents ;
76
- } ) ;
60
+ const queryFilePaths = glob . sync ( [ ...includeFiles , ...excludes ] , {
61
+ cwd : projectPath ,
62
+ absolute : true ,
63
+ } ) ;
64
+ const queryFiles = queryFilePaths . map ( queryFilePath => {
65
+ const fileContents = fs . readFileSync ( queryFilePath , 'utf8' ) ;
66
+ if (
67
+ queryFilePath . endsWith ( '.jsx' ) ||
68
+ queryFilePath . endsWith ( '.js' ) ||
69
+ queryFilePath . endsWith ( '.tsx' ) ||
70
+ queryFilePath . endsWith ( '.ts' )
71
+ ) {
72
+ return extractDocumentFromJavascript ( fileContents , '' ) ;
73
+ }
74
+ return fileContents ;
75
+ } ) ;
77
76
if ( queryFiles . length === 0 ) {
78
- throw new Error ( ' No queries found to generate types for, you may need to run \ 'codegen statements\ ' first' ) ;
77
+ throw new Error ( " No queries found to generate types for, you may need to run 'codegen statements' first" ) ;
79
78
}
80
79
const queries = queryFiles . join ( '\n' ) ;
81
80
@@ -96,22 +95,30 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
96
95
const introspection = path . extname ( schemaPath ) === '.json' ;
97
96
98
97
try {
99
- const output = await generateTypesHelper ( {
100
- schema,
101
- queries,
102
- target,
103
- introspection,
104
- } ) ;
105
- const outputs = Object . entries ( output ) ;
106
-
107
- const outputPath = path . join ( projectPath , generatedFileName ) ;
108
- if ( outputs . length === 1 ) {
109
- const [ [ , contents ] ] = outputs ;
110
- fs . outputFileSync ( path . resolve ( outputPath ) , contents ) ;
98
+ if ( target === 'swift' && fs . existsSync ( outputPath ) && fs . statSync ( outputPath ) . isDirectory ( ) ) {
99
+ generate ( queryFilePaths , schemaPath , outputPath , '' , target , '' , {
100
+ addTypename : true ,
101
+ complexObjectSupport : 'auto' ,
102
+ } ) ;
111
103
} else {
112
- outputs . forEach ( ( [ filepath , contents ] ) => {
113
- fs . outputFileSync ( path . resolve ( path . join ( outputPath , filepath ) ) , contents ) ;
104
+ const output = await generateTypesHelper ( {
105
+ schema,
106
+ queries,
107
+ target,
108
+ introspection,
109
+ multipleSwiftFiles : false ,
114
110
} ) ;
111
+ const outputs = Object . entries ( output ) ;
112
+
113
+ const outputPath = path . join ( projectPath , generatedFileName ) ;
114
+ if ( outputs . length === 1 ) {
115
+ const [ [ , contents ] ] = outputs ;
116
+ fs . outputFileSync ( path . resolve ( outputPath ) , contents ) ;
117
+ } else {
118
+ outputs . forEach ( ( [ filepath , contents ] ) => {
119
+ fs . outputFileSync ( path . resolve ( path . join ( outputPath , filepath ) ) , contents ) ;
120
+ } ) ;
121
+ }
115
122
}
116
123
codeGenSpinner . succeed ( `${ constants . INFO_MESSAGE_CODEGEN_GENERATE_SUCCESS } ${ path . relative ( path . resolve ( '.' ) , outputPath ) } ` ) ;
117
124
} catch ( err ) {
0 commit comments