@@ -2,12 +2,13 @@ const path = require('path');
2
2
const fs = require ( 'fs-extra' ) ;
3
3
const Ora = require ( 'ora' ) ;
4
4
const glob = require ( 'glob-all' ) ;
5
+ const { Source } = require ( 'graphql' ) ;
5
6
6
7
const constants = require ( '../constants' ) ;
7
8
const { loadConfig } = require ( '../codegen-config' ) ;
8
9
const { ensureIntrospectionSchema, getFrontEndHandler, getAppSyncAPIDetails, getAppSyncAPIInfoFromProject } = require ( '../utils' ) ;
9
10
const { 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' ) ;
11
12
12
13
async function generateTypes ( context , forceDownloadSchema , withoutInit = false , decoupleFrontend = '' ) {
13
14
let frontend = decoupleFrontend ;
@@ -61,7 +62,7 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
61
62
cwd : projectPath ,
62
63
absolute : true ,
63
64
} ) ;
64
- const queryFiles = queryFilePaths . map ( queryFilePath => {
65
+ const queries = queryFilePaths . map ( queryFilePath => {
65
66
const fileContents = fs . readFileSync ( queryFilePath , 'utf8' ) ;
66
67
if (
67
68
queryFilePath . endsWith ( '.jsx' ) ||
@@ -71,12 +72,11 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
71
72
) {
72
73
return extractDocumentFromJavascript ( fileContents , '' ) ;
73
74
}
74
- return fileContents ;
75
+ return new Source ( fileContents , queryFilePath ) ;
75
76
} ) ;
76
- if ( queryFiles . length === 0 ) {
77
+ if ( queries . length === 0 ) {
77
78
throw new Error ( "No queries found to generate types for, you may need to run 'codegen statements' first" ) ;
78
79
}
79
- const queries = queryFiles . join ( '\n' ) ;
80
80
81
81
const schemaPath = path . join ( projectPath , cfg . schema ) ;
82
82
@@ -93,32 +93,25 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
93
93
codeGenSpinner . start ( ) ;
94
94
const schema = fs . readFileSync ( schemaPath , 'utf8' ) ;
95
95
const introspection = path . extname ( schemaPath ) === '.json' ;
96
-
96
+ const multipleSwiftFiles = target === 'swift' && fs . existsSync ( outputPath ) && fs . statSync ( outputPath ) . isDirectory ( ) ;
97
97
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 ) ;
103
111
} 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 ) ;
110
114
} ) ;
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
- }
122
115
}
123
116
codeGenSpinner . succeed ( `${ constants . INFO_MESSAGE_CODEGEN_GENERATE_SUCCESS } ${ path . relative ( path . resolve ( '.' ) , outputPath ) } ` ) ;
124
117
} catch ( err ) {
0 commit comments