@@ -7,7 +7,7 @@ const constants = require('../constants');
77const { loadConfig } = require ( '../codegen-config' ) ;
88const { ensureIntrospectionSchema, getFrontEndHandler, getAppSyncAPIDetails, getAppSyncAPIInfoFromProject } = require ( '../utils' ) ;
99const { 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' ) ;
1111
1212async function generateTypes ( context , forceDownloadSchema , withoutInit = false , decoupleFrontend = '' ) {
1313 let frontend = decoupleFrontend ;
@@ -57,25 +57,24 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
5757 const target = cfg . amplifyExtension . codeGenTarget ;
5858
5959 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+ } ) ;
7776 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" ) ;
7978 }
8079 const queries = queryFiles . join ( '\n' ) ;
8180
@@ -96,22 +95,30 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
9695 const introspection = path . extname ( schemaPath ) === '.json' ;
9796
9897 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+ } ) ;
111103 } 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 ,
114110 } ) ;
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+ }
115122 }
116123 codeGenSpinner . succeed ( `${ constants . INFO_MESSAGE_CODEGEN_GENERATE_SUCCESS } ${ path . relative ( path . resolve ( '.' ) , outputPath ) } ` ) ;
117124 } catch ( err ) {
0 commit comments