@@ -2,12 +2,13 @@ const path = require('path');
22const fs = require ( 'fs-extra' ) ;
33const Ora = require ( 'ora' ) ;
44const glob = require ( 'glob-all' ) ;
5+ const { Source } = require ( 'graphql' ) ;
56
67const constants = require ( '../constants' ) ;
78const { loadConfig } = require ( '../codegen-config' ) ;
89const { ensureIntrospectionSchema, getFrontEndHandler, getAppSyncAPIDetails, getAppSyncAPIInfoFromProject } = require ( '../utils' ) ;
910const { generateTypes : generateTypesHelper } = require ( '@aws-amplify/graphql-generator' ) ;
10- const { generate , extractDocumentFromJavascript } = require ( '@aws-amplify/graphql-types-generator' ) ;
11+ const { extractDocumentFromJavascript } = require ( '@aws-amplify/graphql-types-generator' ) ;
1112
1213async function generateTypes ( context , forceDownloadSchema , withoutInit = false , decoupleFrontend = '' ) {
1314 let frontend = decoupleFrontend ;
@@ -61,7 +62,7 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
6162 cwd : projectPath ,
6263 absolute : true ,
6364 } ) ;
64- const queryFiles = queryFilePaths . map ( queryFilePath => {
65+ const queries = queryFilePaths . map ( queryFilePath => {
6566 const fileContents = fs . readFileSync ( queryFilePath , 'utf8' ) ;
6667 if (
6768 queryFilePath . endsWith ( '.jsx' ) ||
@@ -71,12 +72,11 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
7172 ) {
7273 return extractDocumentFromJavascript ( fileContents , '' ) ;
7374 }
74- return fileContents ;
75+ return new Source ( fileContents , queryFilePath ) ;
7576 } ) ;
76- if ( queryFiles . length === 0 ) {
77+ if ( queries . length === 0 ) {
7778 throw new Error ( "No queries found to generate types for, you may need to run 'codegen statements' first" ) ;
7879 }
79- const queries = queryFiles . join ( '\n' ) ;
8080
8181 const schemaPath = path . join ( projectPath , cfg . schema ) ;
8282
@@ -93,32 +93,25 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
9393 codeGenSpinner . start ( ) ;
9494 const schema = fs . readFileSync ( schemaPath , 'utf8' ) ;
9595 const introspection = path . extname ( schemaPath ) === '.json' ;
96-
96+ const multipleSwiftFiles = target === 'swift' && fs . existsSync ( outputPath ) && fs . statSync ( outputPath ) . isDirectory ( ) ;
9797 try {
98- if ( target === 'swift' && fs . existsSync ( outputPath ) && fs . statSync ( outputPath ) . isDirectory ( ) ) {
99- generate ( queryFilePaths , schemaPath , outputPath , '' , target , '' , {
100- addTypename : true ,
101- complexObjectSupport : 'auto' ,
102- } ) ;
98+ const output = await generateTypesHelper ( {
99+ schema,
100+ queries,
101+ target,
102+ introspection,
103+ multipleSwiftFiles,
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 ) ;
103111 } else {
104- const output = await generateTypesHelper ( {
105- schema,
106- queries,
107- target,
108- introspection,
109- multipleSwiftFiles : false ,
112+ outputs . forEach ( ( [ filepath , contents ] ) => {
113+ fs . outputFileSync ( path . resolve ( path . join ( outputPath , filepath ) ) , contents ) ;
110114 } ) ;
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- }
122115 }
123116 codeGenSpinner . succeed ( `${ constants . INFO_MESSAGE_CODEGEN_GENERATE_SUCCESS } ${ path . relative ( path . resolve ( '.' ) , outputPath ) } ` ) ;
124117 } catch ( err ) {
0 commit comments