Skip to content

Commit

Permalink
fix: include statements command file
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilch committed Sep 25, 2023
1 parent bce102f commit a66b1be
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
6 changes: 2 additions & 4 deletions packages/amplify-codegen/src/commands/statements.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
getAppSyncAPIDetails,
readSchemaFromFile,
getAppSyncAPIInfoFromProject,
getRelativeTypesPath,
} = require('../utils');
const { generateGraphQLDocuments } = require('@aws-amplify/graphql-docs-generator');
const { generateStatements: generateStatementsHelper } = require('@aws-amplify/graphql-generator');
Expand Down Expand Up @@ -64,9 +65,6 @@ async function generateStatements(context, forceDownloadSchema, maxDepth, withou

try {
const schemaData = readSchemaFromFile(schemaPath);
const relativeTypesPath = cfg.amplifyExtension.generatedFileName
? path.relative(opsGenDirectory, cfg.amplifyExtension.generatedFileName)
: null;
const generatedOps = generateStatementsHelper({
schema: schemaData,
target: language,
Expand All @@ -75,7 +73,7 @@ async function generateStatements(context, forceDownloadSchema, maxDepth, withou
// default typenameIntrospection to true when not set
typenameIntrospection:
cfg.amplifyExtension.typenameIntrospection === undefined ? true : !!cfg.amplifyExtension.typenameIntrospection,
relativeTypesPath,
relativeTypesPath: getRelativeTypesPath(opsGenDirectory, cfg.amplifyExtension.generatedFileName),
});
if (!generatedOps) {
context.print.warning('No GraphQL statements are generated. Check if the introspection schema has GraphQL operations defined.');
Expand Down
27 changes: 23 additions & 4 deletions packages/amplify-codegen/tests/commands/statements.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ const fs = require('fs-extra');

const { loadConfig } = require('../../src/codegen-config');
const generateStatements = require('../../src/commands/statements');
const { generateStatements: generateStatementsHelper } = require('@aws-amplify/graphql-generator');
const constants = require('../../src/constants');
const { ensureIntrospectionSchema, getFrontEndHandler, getAppSyncAPIDetails, readSchemaFromFile } = require('../../src/utils');
const {
ensureIntrospectionSchema,
getFrontEndHandler,
getAppSyncAPIDetails,
readSchemaFromFile,
getRelativeTypesPath,
} = require('../../src/utils');

const MOCK_CONTEXT = {
print: {
Expand All @@ -16,6 +23,7 @@ const MOCK_CONTEXT = {
},
};

jest.mock('@aws-amplify/graphql-generator');
jest.mock('../../src/codegen-config');
jest.mock('../../src/utils');
jest.mock('fs-extra');
Expand Down Expand Up @@ -67,21 +75,32 @@ describe('command - statements', () => {
MOCK_CONTEXT.amplify.getEnvInfo.mockReturnValue({ projectPath: MOCK_PROJECT_ROOT });
getAppSyncAPIDetails.mockReturnValue(MOCK_APIS);
readSchemaFromFile.mockReturnValue(MOCK_SCHEMA);
generateStatementsHelper.mockReturnValue({
'queries.js': 'queries',
});
});

it('should generate statements', async () => {
const forceDownload = false;
const relativePath = './relative_path';
getRelativeTypesPath.mockReturnValueOnce(relativePath);
await generateStatements(MOCK_CONTEXT, forceDownload);
expect(getFrontEndHandler).toHaveBeenCalledWith(MOCK_CONTEXT);
expect(loadConfig).toHaveBeenCalledWith(MOCK_CONTEXT, false);
expect(getRelativeTypesPath).toHaveBeenCalledWith('MOCK_PROJECT_ROOT/MOCK_STATEMENTS_PATH', 'API.TS');
expect(generateStatementsHelper).toHaveBeenCalledWith({
relativeTypesPath: relativePath,
schema: MOCK_SCHEMA,
target: MOCK_TARGET_LANGUAGE,
typenameIntrospection: true,
useExternalFragmentForS3Object: false,
});
});

it('should generate graphql statements for non JS projects', async () => {
getFrontEndHandler.mockReturnValue('ios');
loadConfig.mockReturnValue({
getProjects: jest.fn().mockReturnValue([
{ ...MOCK_PROJECT, ...{ amplifyExtension: { codeGenTarget: 'javascript' }} }
]),
getProjects: jest.fn().mockReturnValue([{ ...MOCK_PROJECT, ...{ amplifyExtension: { codeGenTarget: 'javascript' } } }]),
});
const forceDownload = false;
await generateStatements(MOCK_CONTEXT, forceDownload);
Expand Down

0 comments on commit a66b1be

Please sign in to comment.