From f7a024d9ec4f7b0156c72b7f7d05139bbb339cbd Mon Sep 17 00:00:00 2001 From: Al Harris <91494052+alharris-at@users.noreply.github.com> Date: Wed, 20 Sep 2023 14:54:49 -0700 Subject: [PATCH 1/3] fix: move types from runtime deps to dev deps (#708) --- packages/appsync-modelgen-plugin/package.json | 6 ++--- packages/graphql-types-generator/package.json | 10 ++++----- yarn.lock | 22 +++++++++---------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/appsync-modelgen-plugin/package.json b/packages/appsync-modelgen-plugin/package.json index c289ee3f0..15b0ff8b4 100644 --- a/packages/appsync-modelgen-plugin/package.json +++ b/packages/appsync-modelgen-plugin/package.json @@ -29,8 +29,6 @@ "@graphql-codegen/plugin-helpers": "^1.18.8", "@graphql-codegen/visitor-plugin-common": "^1.22.0", "@graphql-tools/utils": "^6.0.18", - "@types/node": "^12.12.6", - "@types/pluralize": "0.0.29", "chalk": "^3.0.0", "change-case": "^4.1.1", "lower-case-first": "^2.0.1", @@ -43,7 +41,9 @@ "@graphql-codegen/typescript": "^2.8.3", "graphql": "^15.5.0", "java-ast": "^0.3.0", - "ts-json-schema-generator": "1.0.0" + "ts-json-schema-generator": "1.0.0", + "@types/node": "^12.12.6", + "@types/pluralize": "0.0.29" }, "peerDependencies": { "graphql": "^15.5.0" diff --git a/packages/graphql-types-generator/package.json b/packages/graphql-types-generator/package.json index 374522e64..4224f6a70 100644 --- a/packages/graphql-types-generator/package.json +++ b/packages/graphql-types-generator/package.json @@ -37,10 +37,6 @@ "dependencies": { "@babel/generator": "7.0.0-beta.4", "@babel/types": "7.0.0-beta.4", - "@types/babel-generator": "^6.25.0", - "@types/fs-extra": "^8.1.0", - "@types/prettier": "^1.19.0", - "@types/rimraf": "^3.0.0", "babel-generator": "^6.26.1", "babel-types": "^6.26.0", "change-case": "^4.1.1", @@ -60,7 +56,11 @@ "@types/glob": "^7.1.1", "@types/inflected": "^1.1.29", "@types/node": "^10.17.13", - "@types/yargs": "^15.0.1" + "@types/yargs": "^15.0.1", + "@types/babel-generator": "^6.25.0", + "@types/fs-extra": "^8.1.0", + "@types/prettier": "^1.19.0", + "@types/rimraf": "^3.0.0" }, "publishConfig": { "access": "public" diff --git a/yarn.lock b/yarn.lock index 34e7b820c..8502228bb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3744,9 +3744,9 @@ value-or-promise "^1.0.12" "@graphql-tools/utils@^10.0.0": - version "10.0.5" - resolved "https://registry.npmjs.org/@graphql-tools/utils/-/utils-10.0.5.tgz#b76c7b5b7fc3f67da734b51cf6e33c52dee09974" - integrity sha512-ZTioQqg9z9eCG3j+KDy54k1gp6wRIsLqkx5yi163KVvXVkfjsrdErCyZjrEug21QnKE9piP4tyxMpMMOT1RuRw== + version "10.0.6" + resolved "https://registry.npmjs.org/@graphql-tools/utils/-/utils-10.0.6.tgz#8a809d6bc0df27ffe8964696f182af2383b5974b" + integrity sha512-hZMjl/BbX10iagovakgf3IiqArx8TPsotq5pwBld37uIX1JiZoSbgbCIFol7u55bh32o6cfDEiiJgfAD5fbeyQ== dependencies: "@graphql-typed-document-node/core" "^3.1.1" dset "^3.1.2" @@ -6128,17 +6128,17 @@ integrity sha512-AyQEn5hIPV7Ze+xFoXVU3QTHXVbWPrzaOkxtENMPMuNL6VVHrp4hHfDt9nrQpjO7BgvuM95dMtkycX5M/DZR3w== "@whatwg-node/fetch@^0.9.0": - version "0.9.9" - resolved "https://registry.npmjs.org/@whatwg-node/fetch/-/fetch-0.9.9.tgz#65e68aaf8353755c20657b803f2fe983dbdabf91" - integrity sha512-OTVoDm039CNyAWSRc2WBimMl/N9J4Fk2le21Xzcf+3OiWPNNSIbMnpWKBUyraPh2d9SAEgoBdQxTfVNihXgiUw== + version "0.9.13" + resolved "https://registry.npmjs.org/@whatwg-node/fetch/-/fetch-0.9.13.tgz#1d084cd546b9cd425ae89cbb1252a3e47a9a2e1c" + integrity sha512-PPtMwhjtS96XROnSpowCQM85gCUG2m7AXZFw0PZlGbhzx2GK7f2iOXilfgIJ0uSlCuuGbOIzfouISkA7C4FJOw== dependencies: - "@whatwg-node/node-fetch" "^0.4.8" + "@whatwg-node/node-fetch" "^0.4.17" urlpattern-polyfill "^9.0.0" -"@whatwg-node/node-fetch@^0.4.8": - version "0.4.13" - resolved "https://registry.npmjs.org/@whatwg-node/node-fetch/-/node-fetch-0.4.13.tgz#523b046f9511e6e62ac98365653b5ce63175614b" - integrity sha512-Wijn8jtXq6VBX6EttABXHJIQBcoOP6RRQllXbiaHGORACTDr1xg6g2UnkoggY3dbDkm1VsMjdSe7NVBPc4ukYg== +"@whatwg-node/node-fetch@^0.4.17": + version "0.4.19" + resolved "https://registry.npmjs.org/@whatwg-node/node-fetch/-/node-fetch-0.4.19.tgz#29c72ff65a8e450949238612ff17a3d3717736d3" + integrity sha512-AW7/m2AuweAoSXmESrYQr/KBafueScNbn2iNO0u6xFr2JZdPmYsSm5yvAXYk6yDLv+eDmSSKrf7JnFZ0CsJIdA== dependencies: "@whatwg-node/events" "^0.1.0" busboy "^1.6.0" From 756c3751bf236a64a5f028f61523cb96e0d7a7fa Mon Sep 17 00:00:00 2001 From: Dane Pilcher Date: Fri, 22 Sep 2023 13:10:14 -0600 Subject: [PATCH 2/3] fix: typo in graphql-generator arg (#709) * fix: useExperimentalPipelinedTranformer->useExperimentalPipelinedTransformer (no s in Transformer) * chore: extract api --- packages/graphql-generator/API.md | 2 +- packages/graphql-generator/src/models.ts | 4 ++-- packages/graphql-generator/src/typescript.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/graphql-generator/API.md b/packages/graphql-generator/API.md index 169907436..75571257c 100644 --- a/packages/graphql-generator/API.md +++ b/packages/graphql-generator/API.md @@ -22,7 +22,7 @@ export type GenerateModelsOptions = { directives: string; generateIndexRules?: boolean; emitAuthProvider?: boolean; - useExperimentalPipelinedTranformer?: boolean; + useExperimentalPipelinedTransformer?: boolean; transformerVersion?: boolean; respectPrimaryKeyAttributesOnConnectionField?: boolean; generateModelsForLazyLoadAndCustomSelectionSet?: boolean; diff --git a/packages/graphql-generator/src/models.ts b/packages/graphql-generator/src/models.ts index 3dfb490ed..0871a7854 100644 --- a/packages/graphql-generator/src/models.ts +++ b/packages/graphql-generator/src/models.ts @@ -13,7 +13,7 @@ export async function generateModels(options: GenerateModelsOptions): Promise Date: Mon, 25 Sep 2023 12:17:29 -0600 Subject: [PATCH 3/3] fix: multiple swift file names (#713) --- .../amplify-codegen/src/commands/types.js | 73 ++++++++++--------- .../tests/commands/types.test.js | 28 +++++++ 2 files changed, 68 insertions(+), 33 deletions(-) diff --git a/packages/amplify-codegen/src/commands/types.js b/packages/amplify-codegen/src/commands/types.js index 5da70cddf..ee28bf6ff 100644 --- a/packages/amplify-codegen/src/commands/types.js +++ b/packages/amplify-codegen/src/commands/types.js @@ -7,7 +7,7 @@ const constants = require('../constants'); const { loadConfig } = require('../codegen-config'); const { ensureIntrospectionSchema, getFrontEndHandler, getAppSyncAPIDetails, getAppSyncAPIInfoFromProject } = require('../utils'); const { generateTypes: generateTypesHelper } = require('@aws-amplify/graphql-generator'); -const { extractDocumentFromJavascript } = require('@aws-amplify/graphql-types-generator'); +const { generate, extractDocumentFromJavascript } = require('@aws-amplify/graphql-types-generator'); async function generateTypes(context, forceDownloadSchema, withoutInit = false, decoupleFrontend = '') { let frontend = decoupleFrontend; @@ -57,25 +57,24 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false, const target = cfg.amplifyExtension.codeGenTarget; const excludes = cfg.excludes.map(pattern => `!${pattern}`); - const queryFiles = glob - .sync([...includeFiles, ...excludes], { - cwd: projectPath, - absolute: true, - }) - .map(queryFilePath => { - const fileContents = fs.readFileSync(queryFilePath, 'utf8'); - if ( - queryFilePath.endsWith('.jsx') || - queryFilePath.endsWith('.js') || - queryFilePath.endsWith('.tsx') || - queryFilePath.endsWith('.ts') - ) { - return extractDocumentFromJavascript(fileContents, ''); - } - return fileContents; - }); + const queryFilePaths = glob.sync([...includeFiles, ...excludes], { + cwd: projectPath, + absolute: true, + }); + const queryFiles = queryFilePaths.map(queryFilePath => { + const fileContents = fs.readFileSync(queryFilePath, 'utf8'); + if ( + queryFilePath.endsWith('.jsx') || + queryFilePath.endsWith('.js') || + queryFilePath.endsWith('.tsx') || + queryFilePath.endsWith('.ts') + ) { + return extractDocumentFromJavascript(fileContents, ''); + } + return fileContents; + }); if (queryFiles.length === 0) { - throw new Error('No queries found to generate types for, you may need to run \'codegen statements\' first'); + throw new Error("No queries found to generate types for, you may need to run 'codegen statements' first"); } const queries = queryFiles.join('\n'); @@ -96,22 +95,30 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false, const introspection = path.extname(schemaPath) === '.json'; try { - const output = await generateTypesHelper({ - schema, - queries, - target, - introspection, - }); - const outputs = Object.entries(output); - - const outputPath = path.join(projectPath, generatedFileName); - if (outputs.length === 1) { - const [[, contents]] = outputs; - fs.outputFileSync(path.resolve(outputPath), contents); + if (target === 'swift' && fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory()) { + generate(queryFilePaths, schemaPath, outputPath, '', target, '', { + addTypename: true, + complexObjectSupport: 'auto', + }); } else { - outputs.forEach(([filepath, contents]) => { - fs.outputFileSync(path.resolve(path.join(outputPath, filepath)), contents); + const output = await generateTypesHelper({ + schema, + queries, + target, + introspection, + multipleSwiftFiles: false, }); + const outputs = Object.entries(output); + + const outputPath = path.join(projectPath, generatedFileName); + if (outputs.length === 1) { + const [[, contents]] = outputs; + fs.outputFileSync(path.resolve(outputPath), contents); + } else { + outputs.forEach(([filepath, contents]) => { + fs.outputFileSync(path.resolve(path.join(outputPath, filepath)), contents); + }); + } } codeGenSpinner.succeed(`${constants.INFO_MESSAGE_CODEGEN_GENERATE_SUCCESS} ${path.relative(path.resolve('.'), outputPath)}`); } catch (err) { diff --git a/packages/amplify-codegen/tests/commands/types.test.js b/packages/amplify-codegen/tests/commands/types.test.js index a9cd3c36f..b492e0d55 100644 --- a/packages/amplify-codegen/tests/commands/types.test.js +++ b/packages/amplify-codegen/tests/commands/types.test.js @@ -1,6 +1,7 @@ const { sync } = require('glob-all'); const path = require('path'); const { generateTypes: generateTypesHelper } = require('@aws-amplify/graphql-generator'); +const { generate: legacyGenerate } = require('@aws-amplify/graphql-types-generator'); const fs = require('fs-extra'); const { loadConfig } = require('../../src/codegen-config'); @@ -20,6 +21,7 @@ const MOCK_CONTEXT = { jest.mock('glob-all'); jest.mock('@aws-amplify/graphql-generator'); +jest.mock('@aws-amplify/graphql-types-generator'); jest.mock('../../src/codegen-config'); jest.mock('../../src/utils'); jest.mock('fs-extra'); @@ -81,9 +83,35 @@ describe('command - types', () => { schema: 'schema', target: 'TYPE_SCRIPT_OR_FLOW_OR_ANY_OTHER_LANGUAGE', introspection: false, + multipleSwiftFiles: false, }); }); + it('should use legacy types generation when generating multiple swift files', async () => { + MOCK_PROJECT.amplifyExtension.codeGenTarget = 'swift'; + MOCK_PROJECT.amplifyExtension.generatedFileName = 'typesDirectory'; + const forceDownload = false; + fs.readFileSync + .mockReturnValueOnce('query 1') + .mockReturnValueOnce('query 2') + .mockReturnValueOnce('schema'); + fs.existsSync.mockReturnValueOnce(true); + fs.statSync.mockReturnValueOnce({ + isDirectory: jest.fn().mockReturnValue(true), + }); + await generateTypes(MOCK_CONTEXT, forceDownload); + expect(generateTypesHelper).not.toHaveBeenCalled(); + expect(legacyGenerate).toHaveBeenCalledWith( + ['q1.gql', 'q2.gql'], + 'MOCK_PROJECT_ROOT/INTROSPECTION_SCHEMA.JSON', + 'MOCK_PROJECT_ROOT/typesDirectory', + '', + 'swift', + '', + { addTypename: true, complexObjectSupport: 'auto' }, + ); + }); + it('should not generate type if the frontend is android', async () => { const forceDownload = false; getFrontEndHandler.mockReturnValue('android');