Skip to content

Commit

Permalink
Merge pull request #720 from aws-amplify/main
Browse files Browse the repository at this point in the history
Release mult swift files fix and dep fix
  • Loading branch information
dpilch authored Sep 26, 2023
2 parents 348f87c + 1e484af commit fd984bc
Show file tree
Hide file tree
Showing 16 changed files with 330 additions and 721 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ module.exports = {
'@typescript-eslint/interface-name-prefix': 'off',
'no-throw-literal': 'off',
'react/static-property-placement': 'off',
'import/no-extraneous-dependencies': 'off',
'import/no-extraneous-dependencies': ['error', {devDependencies: true}],
'spaced-comment': 'off',
'@typescript-eslint/no-array-constructor': 'off',
'prefer-rest-params': 'off',
Expand Down
4 changes: 4 additions & 0 deletions packages/amplify-codegen-e2e-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@
"clean": "rimraf ./lib"
},
"dependencies": {
"aws-sdk": "^2.1465.0",
"chalk": "^3.0.0",
"dotenv": "^8.6.0",
"execa": "^4.1.0",
"fs-extra": "^8.1.0",
"glob": "^10.3.9",
"ini": "^3.0.1",
"jest-circus": "^27.5.1",
"jest-environment-node": "^26.6.2",
"lodash": "^4.17.19",
"node-pty": "beta",
Expand Down
3 changes: 3 additions & 0 deletions packages/amplify-codegen-e2e-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"dependencies": {
"@aws-amplify/amplify-codegen-e2e-core": "1.6.0",
"@aws-amplify/graphql-schema-test-library": "^1.1.18",
"amazon-cognito-identity-js": "^6.3.6",
"aws-amplify": "^5.3.3",
"aws-appsync": "^4.1.9",
"aws-sdk": "^2.1413.0",
Expand All @@ -33,7 +34,9 @@
"graphql-tag": "^2.10.1",
"js-yaml": "^4.0.0",
"lodash": "^4.17.19",
"node-fetch": "^3.3.2",
"uuid": "^3.4.0",
"ws": "^8.14.2",
"yargs": "^15.1.0"
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions packages/amplify-codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
"dependencies": {
"@aws-amplify/graphql-generator": "0.1.2",
"@aws-amplify/graphql-types-generator": "3.4.1",
"@aws-amplify/graphql-docs-generator": "4.2.0",
"@graphql-codegen/core": "2.6.6",
"aws-sdk": "^2.1465.0",
"chalk": "^3.0.0",
"fs-extra": "^8.1.0",
"glob-all": "^3.1.0",
Expand Down
49 changes: 21 additions & 28 deletions packages/amplify-codegen/src/commands/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ const path = require('path');
const fs = require('fs-extra');
const Ora = require('ora');
const glob = require('glob-all');
const { Source } = require('graphql');

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 { generate, extractDocumentFromJavascript } = require('@aws-amplify/graphql-types-generator');
const { extractDocumentFromJavascript } = require('@aws-amplify/graphql-types-generator');

async function generateTypes(context, forceDownloadSchema, withoutInit = false, decoupleFrontend = '') {
let frontend = decoupleFrontend;
Expand Down Expand Up @@ -61,7 +62,7 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
cwd: projectPath,
absolute: true,
});
const queryFiles = queryFilePaths.map(queryFilePath => {
const queries = queryFilePaths.map(queryFilePath => {
const fileContents = fs.readFileSync(queryFilePath, 'utf8');
if (
queryFilePath.endsWith('.jsx') ||
Expand All @@ -71,12 +72,11 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
) {
return extractDocumentFromJavascript(fileContents, '');
}
return fileContents;
return new Source(fileContents, queryFilePath);
});
if (queryFiles.length === 0) {
if (queries.length === 0) {
throw new Error("No queries found to generate types for, you may need to run 'codegen statements' first");
}
const queries = queryFiles.join('\n');

const schemaPath = path.join(projectPath, cfg.schema);

Expand All @@ -93,32 +93,25 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
codeGenSpinner.start();
const schema = fs.readFileSync(schemaPath, 'utf8');
const introspection = path.extname(schemaPath) === '.json';

const multipleSwiftFiles = target === 'swift' && fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory();
try {
if (target === 'swift' && fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory()) {
generate(queryFilePaths, schemaPath, outputPath, '', target, '', {
addTypename: true,
complexObjectSupport: 'auto',
});
const output = await generateTypesHelper({
schema,
queries,
target,
introspection,
multipleSwiftFiles,
});
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 {
const output = await generateTypesHelper({
schema,
queries,
target,
introspection,
multipleSwiftFiles: false,
outputs.forEach(([filepath, contents]) => {
fs.outputFileSync(path.resolve(path.join(outputPath, filepath)), contents);
});
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) {
Expand Down
21 changes: 8 additions & 13 deletions packages/amplify-codegen/tests/commands/types.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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 { Source } = require('graphql');

const { loadConfig } = require('../../src/codegen-config');
const generateTypes = require('../../src/commands/types');
Expand Down Expand Up @@ -60,6 +60,9 @@ describe('command - types', () => {
beforeEach(() => {
jest.clearAllMocks();
fs.existsSync.mockReturnValue(true);
fs.statSync.mockReturnValue({
isDirectory: jest.fn().mockReturnValue(false),
});
getFrontEndHandler.mockReturnValue('javascript');
loadConfig.mockReturnValue({
getProjects: jest.fn().mockReturnValue([MOCK_PROJECT]),
Expand All @@ -79,15 +82,15 @@ describe('command - types', () => {
expect(loadConfig).toHaveBeenCalledWith(MOCK_CONTEXT, false);
expect(sync).toHaveBeenCalledWith([MOCK_INCLUDE_PATH, `!${MOCK_EXCLUDE_PATH}`], { cwd: MOCK_PROJECT_ROOT, absolute: true });
expect(generateTypesHelper).toHaveBeenCalledWith({
queries: 'query 1\nquery 2',
queries: [new Source('query 1', 'q1.gql'), new Source('query 2', 'q2.gql')],
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 () => {
it('should use generate multiple swift files', async () => {
MOCK_PROJECT.amplifyExtension.codeGenTarget = 'swift';
MOCK_PROJECT.amplifyExtension.generatedFileName = 'typesDirectory';
const forceDownload = false;
Expand All @@ -100,16 +103,6 @@ describe('command - types', () => {
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 () => {
Expand All @@ -121,6 +114,7 @@ describe('command - types', () => {

it('should download the schema if forceDownload flag is passed', async () => {
const forceDownload = true;
fs.readFileSync.mockReturnValueOnce('query 1').mockReturnValueOnce('query 2');
await generateTypes(MOCK_CONTEXT, forceDownload);
expect(ensureIntrospectionSchema).toHaveBeenCalledWith(
MOCK_CONTEXT,
Expand All @@ -134,6 +128,7 @@ describe('command - types', () => {
it('should download the schema if the schema file is missing', async () => {
fs.existsSync.mockReturnValue(false);
const forceDownload = false;
fs.readFileSync.mockReturnValueOnce('query 1').mockReturnValueOnce('query 2');
await generateTypes(MOCK_CONTEXT, forceDownload);
expect(ensureIntrospectionSchema).toHaveBeenCalledWith(
MOCK_CONTEXT,
Expand Down
10 changes: 7 additions & 3 deletions packages/appsync-modelgen-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,25 @@
"@graphql-codegen/plugin-helpers": "^1.18.8",
"@graphql-codegen/visitor-plugin-common": "^1.22.0",
"@graphql-tools/utils": "^6.0.18",
"ajv": "^6.10.0",
"chalk": "^3.0.0",
"change-case": "^4.1.1",
"graphql-transformer-common": "^4.25.1",
"lower-case-first": "^2.0.1",
"pluralize": "^8.0.0",
"strip-indent": "^3.0.0",
"ts-dedent": "^1.1.0"
"ts-dedent": "^1.1.0",
"ts-json-schema-generator": "1.0.0"
},
"devDependencies": {
"@graphql-codegen/testing": "^1.17.7",
"@graphql-codegen/typescript": "^2.8.3",
"@types/fs-extra": "^8.1.2",
"@types/node": "^12.12.6",
"@types/pluralize": "0.0.29",

"graphql": "^15.5.0",
"java-ast": "^0.3.0",
"ts-json-schema-generator": "1.0.0"
"java-ast": "^0.3.0"
},
"peerDependencies": {
"graphql": "^15.5.0"
Expand Down
3 changes: 2 additions & 1 deletion packages/graphql-generator/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
```ts

import { Source } from 'graphql';
import { Target } from '@aws-amplify/appsync-modelgen-plugin';
import { Target as Target_2 } from '@aws-amplify/graphql-types-generator';

Expand Down Expand Up @@ -49,7 +50,7 @@ export function generateTypes(options: GenerateTypesOptions): Promise<GeneratedO
export type GenerateTypesOptions = {
schema: string;
target: TypesTarget;
queries: string;
queries: string | Source[];
introspection?: boolean;
multipleSwiftFiles?: boolean;
};
Expand Down
3 changes: 3 additions & 0 deletions packages/graphql-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
"@graphql-tools/apollo-engine-loader": "^8.0.0",
"graphql": "^15.5.0"
},
"devDependencies": {
"@types/prettier": "^1.0.0"
},
"typescript": {
"definition": "lib/index.d.ts"
},
Expand Down
Loading

0 comments on commit fd984bc

Please sign in to comment.