Skip to content

Commit 36927ca

Browse files
authored
Merge pull request #498 from aws-amplify/main
Release Codegen Repo
2 parents 00ba754 + c930769 commit 36927ca

File tree

6 files changed

+95
-15
lines changed

6 files changed

+95
-15
lines changed

packages/amplify-codegen/commands/codegen/models.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
name: featureName,
99
run: async context => {
1010
try {
11-
await codeGen.generateModels(context, getOutputDirParam(context, false));
11+
await codeGen.generateModels(context, { overrideOutputDir: getOutputDirParam(context, false) });
1212
} catch (ex) {
1313
context.print.info(ex.message);
1414
console.log(ex.stack);
Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
11
const generateModels = require('./models');
22
const getOutputDirParam = require('../utils/getOutputDirParam');
33

4+
/**
5+
* Generate the model introspection file for the input schema, and write to disk based on
6+
* the output-dir flag passed in by the customer.
7+
*/
48
async function generateModelIntrospection(context) {
5-
await generateModels(context, getOutputDirParam(context, true), true);
9+
await generateModels(context, {
10+
overrideOutputDir: getOutputDirParam(context, true),
11+
isIntrospection: true,
12+
});
613
}
714

8-
module.exports = generateModelIntrospection;
15+
/**
16+
* Compute the model introspection schema and return in-memory.
17+
* @param context the CLI context, necessary for file locations and feature flags.
18+
* @returns the latest version of the model introspection schema shape.
19+
*/
20+
async function getModelIntrospection(context) {
21+
const generatedCode = await generateModels(context, {
22+
overrideOutputDir: '.', // Needed to get output working, not used, since writeToDisk is disabled.
23+
isIntrospection: true,
24+
writeToDisk: false,
25+
});
26+
27+
if (generatedCode.length !== 1) {
28+
throw new Error('Expected a single output to be generated for model introspection.');
29+
}
30+
31+
return JSON.parse(generatedCode[0]);
32+
}
33+
34+
module.exports = {
35+
generateModelIntrospection,
36+
getModelIntrospection,
37+
};

packages/amplify-codegen/src/commands/models.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,23 @@ const readNumericFeatureFlag = key => {
4545
}
4646
};
4747

48-
async function generateModels(context, overrideOutputDir = null, isIntrospection = false) {
48+
// type GenerateModelsOptions = {
49+
// overrideOutputDir?: String;
50+
// isIntrospection: Boolean;
51+
// writeToDisk: Boolean;
52+
// }
53+
54+
const defaultGenerateModelsOption = {
55+
overrideOutputDir: null,
56+
isIntrospection: false,
57+
writeToDisk: true,
58+
};
59+
60+
async function generateModels(context, generateOptions = null) {
61+
const { overrideOutputDir, isIntrospection, writeToDisk } = generateOptions
62+
? { ...defaultGenerateModelsOption, ...generateOptions }
63+
: defaultGenerateModelsOption;
64+
4965
// steps:
5066
// 1. Load the schema and validate using transformer
5167
// 2. get all the directives supported by transformer
@@ -151,15 +167,19 @@ async function generateModels(context, overrideOutputDir = null, isIntrospection
151167

152168
const generatedCode = await Promise.all(codeGenPromises);
153169

154-
appsyncLocalConfig.forEach((cfg, idx) => {
155-
const outPutPath = cfg.filename;
156-
fs.ensureFileSync(outPutPath);
157-
fs.writeFileSync(outPutPath, generatedCode[idx]);
158-
});
170+
if (writeToDisk) {
171+
appsyncLocalConfig.forEach((cfg, idx) => {
172+
const outPutPath = cfg.filename;
173+
fs.ensureFileSync(outPutPath);
174+
fs.writeFileSync(outPutPath, generatedCode[idx]);
175+
});
176+
177+
generateEslintIgnore(context);
159178

160-
generateEslintIgnore(context);
179+
context.print.info(`Successfully generated models. Generated models can be found in ${overrideOutputDir ?? baseOutputDir}`);
180+
}
161181

162-
context.print.info(`Successfully generated models. Generated models can be found in ${overrideOutputDir ?? baseOutputDir}`);
182+
return generatedCode;
163183
}
164184

165185
async function validateSchema(context) {

packages/amplify-codegen/src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const add = require('./commands/add');
55
const remove = require('./commands/remove');
66
const configure = require('./commands/configure');
77
const generateModels = require('./commands/models');
8-
const generateModelIntrospection = require('./commands/model-intropection');
8+
const { generateModelIntrospection, getModelIntrospection } = require('./commands/model-intropection');
99
const { isCodegenConfigured, switchToSDLSchema } = require('./utils');
1010
const prePushAddGraphQLCodegenHook = require('./callbacks/prePushAddCallback');
1111
const prePushUpdateGraphQLCodegenHook = require('./callbacks/prePushUpdateCallback');
@@ -28,4 +28,5 @@ module.exports = {
2828
handleAmplifyEvent,
2929
generateModels,
3030
generateModelIntrospection,
31+
getModelIntrospection,
3132
};

packages/amplify-codegen/tests/commands/model-introspection.test.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const generateModelIntrospection = require('../../src/commands/model-intropection');
1+
const { generateModelIntrospection, getModelIntrospection } = require('../../src/commands/model-intropection');
22
const graphqlCodegen = require('@graphql-codegen/core');
33
const mockFs = require('mock-fs');
44
const path = require('path');
@@ -10,7 +10,8 @@ const MOCK_OUTPUT_DIR = 'output';
1010
const MOCK_PROJECT_ROOT = 'project';
1111
const MOCK_PROJECT_NAME = 'myapp';
1212
const MOCK_BACKEND_DIRECTORY = 'backend';
13-
const MOCK_GENERATED_CODE = 'This code is auto-generated!';
13+
const MOCK_GENERATED_INTROSPECTION = { schemaVersion: 1 }
14+
const MOCK_GENERATED_CODE = JSON.stringify(MOCK_GENERATED_INTROSPECTION);
1415
const MOCK_CONTEXT = {
1516
print: {
1617
info: jest.fn(),
@@ -39,7 +40,7 @@ const MOCK_CONTEXT = {
3940

4041

4142

42-
describe('model-intropection command test', () => {
43+
describe('generateModelIntrospection', () => {
4344
graphqlCodegen.codegen.mockReturnValue(MOCK_GENERATED_CODE);
4445
const schemaFilePath = path.join(MOCK_BACKEND_DIRECTORY, 'api', MOCK_PROJECT_NAME);
4546
const outputDirectory = path.join(MOCK_PROJECT_ROOT, MOCK_OUTPUT_DIR);
@@ -81,4 +82,32 @@ describe('model-intropection command test', () => {
8182
});
8283

8384
afterEach(mockFs.restore);
85+
});
86+
87+
describe('getModelIntrospection', () => {
88+
graphqlCodegen.codegen.mockReturnValue(MOCK_GENERATED_CODE);
89+
const schemaFilePath = path.join(MOCK_BACKEND_DIRECTORY, 'api', MOCK_PROJECT_NAME);
90+
const outputDirectory = path.join(MOCK_PROJECT_ROOT, MOCK_OUTPUT_DIR);
91+
const mockedFiles = {};
92+
mockedFiles[schemaFilePath] = {
93+
'schema.graphql': ' type SimpleModel { id: ID! status: String } ',
94+
};
95+
mockedFiles[outputDirectory] = {};
96+
97+
beforeEach(() => {
98+
jest.clearAllMocks();
99+
});
100+
101+
it('should return model intropection schema', async () => {
102+
mockFs(mockedFiles);
103+
// assert empty folder before generation
104+
expect(fs.readdirSync(outputDirectory).length).toEqual(0);
105+
106+
const responseObject = await getModelIntrospection(MOCK_CONTEXT);
107+
expect(responseObject).toEqual(MOCK_GENERATED_INTROSPECTION);
108+
109+
// assert model generation succeeds with no file written
110+
expect(graphqlCodegen.codegen).toBeCalled();
111+
expect(fs.readdirSync(outputDirectory).length).toEqual(0);
112+
});
84113
});

packages/appsync-modelgen-plugin/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface AppSyncModelPluginConfig extends RawDocumentsConfig {
66

77
export * from './plugin';
88
export * from './preset';
9+
export * from './interfaces/introspection';
910

1011
export const addToSchema = (config: AppSyncModelPluginConfig) => {
1112
const result: string[] = [];

0 commit comments

Comments
 (0)