Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: graphql generator #690

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ module.exports = {
"utils",
"aws",
"sdk",
"javascript",
"graphql",
"scala",
],
"skipIfMatch": [
"http://[^s]*",
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package-lock.json
.vscode/*
!.vscode/settings.json
!.vscode/extensions.json
packages/graphql-generator/lib
packages/appsync-modelgen-plugin/lib
packages/graphql-docs-generator/lib
packages/graphql-types-generator/lib
Expand Down
11 changes: 5 additions & 6 deletions packages/amplify-codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
"extract-api": "ts-node ../../scripts/extract-api.ts"
},
"dependencies": {
"@aws-amplify/appsync-modelgen-plugin": "2.6.0",
"@aws-amplify/graphql-docs-generator": "4.1.0",
"@aws-amplify/graphql-types-generator": "3.3.0",
"@aws-amplify/graphql-generator": "^0.0.1",
"@aws-amplify/graphql-types-generator": "^3.2.0",
"@graphql-codegen/core": "2.6.6",
"chalk": "^3.0.0",
"fs-extra": "^8.1.0",
Expand All @@ -46,9 +45,9 @@
"collectCoverage": true,
"coverageThreshold": {
"global": {
"branches": 56,
"functions": 68,
"lines": 73
"branches": 54,
"functions": 65,
"lines": 72
}
},
"testURL": "http://localhost",
Expand Down
80 changes: 29 additions & 51 deletions packages/amplify-codegen/src/commands/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ const fs = require('fs-extra');
const { parse } = require('graphql');
const glob = require('glob-all');
const { FeatureFlags, pathManager } = require('@aws-amplify/amplify-cli-core');
const gqlCodeGen = require('@graphql-codegen/core');
const appSyncDataStoreCodeGen = require('@aws-amplify/appsync-modelgen-plugin');
const { version: packageVersion } = require('../../package.json');
const { generateModels: generateModelsHelper } = require('@aws-amplify/graphql-generator');
const { validateAmplifyFlutterMinSupportedVersion } = require('../utils/validateAmplifyFlutterMinSupportedVersion');

const platformToLanguageMap = {
android: 'java',
ios: 'swift',
flutter: 'dart',
javascript: 'javascript',
typescript: 'typescript',
};

/**
Expand Down Expand Up @@ -91,9 +90,7 @@ async function generateModels(context, generateOptions = null) {
});

const schemaContent = loadSchema(apiResourcePath);

const baseOutputDir = path.join(projectRoot, getModelOutputPath(context));
const schema = parse(schemaContent);
const baseOutputDir = overrideOutputDir || path.join(projectRoot, getModelOutputPath(context));
const projectConfig = context.amplify.getProjectConfig();

if (!isIntrospection && projectConfig.frontend === 'flutter' && !validateAmplifyFlutterMinSupportedVersion(projectRoot)) {
Expand All @@ -104,64 +101,42 @@ Amplify Flutter versions prior to 0.6.0 are no longer supported by codegen. Plea

const generateIndexRules = readFeatureFlag('codegen.generateIndexRules');
const emitAuthProvider = readFeatureFlag('codegen.emitAuthProvider');
const usePipelinedTransformer = readFeatureFlag('graphQLTransformer.useExperimentalPipelinedTransformer');
const useExperimentalPipelinedTransformer = readFeatureFlag('graphQLTransformer.useExperimentalPipelinedTransformer');
const transformerVersion = readNumericFeatureFlag('graphQLTransformer.transformerVersion');
const respectPrimaryKeyAttributesOnConnectionField = readFeatureFlag('graphQLTransformer.respectPrimaryKeyAttributesOnConnectionField');
const generateModelsForLazyLoadAndCustomSelectionSet = readFeatureFlag('codegen.generateModelsForLazyLoadAndCustomSelectionSet');
const improvePluralization = readFeatureFlag('graphQLTransformer.improvePluralization');

let isTimestampFieldsAdded = readFeatureFlag('codegen.addTimestampFields');
const addTimestampFields = readFeatureFlag('codegen.addTimestampFields');

const handleListNullabilityTransparently = readFeatureFlag('codegen.handleListNullabilityTransparently');
const appsyncLocalConfig = await appSyncDataStoreCodeGen.preset.buildGeneratesSection({
baseOutputDir,
schema,
config: {
target: isIntrospection ? 'introspection' : platformToLanguageMap[projectConfig.frontend] || projectConfig.frontend,
directives: directiveDefinitions,
isTimestampFieldsAdded,
emitAuthProvider,
generateIndexRules,
handleListNullabilityTransparently,
usePipelinedTransformer,
transformerVersion,
respectPrimaryKeyAttributesOnConnectionField,
improvePluralization,
generateModelsForLazyLoadAndCustomSelectionSet,
codegenVersion: packageVersion,
overrideOutputDir, // This needs to live under `config` in order for the GraphQL types to work out.
},
});

const codeGenPromises = appsyncLocalConfig.map(cfg => {
return gqlCodeGen.codegen({
...cfg,
plugins: [
{
appSyncLocalCodeGen: {},
},
],
pluginMap: {
appSyncLocalCodeGen: appSyncDataStoreCodeGen,
},
});
const generatedCode = await generateModelsHelper({
schema: schemaContent,
directives: directiveDefinitions,
target: isIntrospection ? 'introspection' : platformToLanguageMap[projectConfig.frontend],
generateIndexRules,
emitAuthProvider,
useExperimentalPipelinedTransformer,
transformerVersion,
respectPrimaryKeyAttributesOnConnectionField,
improvePluralization,
generateModelsForLazyLoadAndCustomSelectionSet,
addTimestampFields,
handleListNullabilityTransparently,
});

const generatedCode = await Promise.all(codeGenPromises);

if (writeToDisk) {
appsyncLocalConfig.forEach((cfg, idx) => {
const outPutPath = cfg.filename;
fs.ensureFileSync(outPutPath);
fs.writeFileSync(outPutPath, generatedCode[idx]);
Object.entries(generatedCode).forEach(([filepath, contents]) => {
fs.outputFileSync(path.resolve(path.join(baseOutputDir, filepath)), contents);
});

// TODO: move to @aws-amplify/graphql-generator
generateEslintIgnore(context);

context.print.info(`Successfully generated models. Generated models can be found in ${overrideOutputDir ?? baseOutputDir}`);
context.print.info(`Successfully generated models. Generated models can be found in ${baseOutputDir}`);
}

return generatedCode;
return Object.values(generatedCode);
}

async function validateSchema(context) {
Expand Down Expand Up @@ -196,9 +171,12 @@ function getModelOutputPath(context) {
const projectConfig = context.amplify.getProjectConfig();
switch (projectConfig.frontend) {
case 'javascript':
return projectConfig.javascript && projectConfig.javascript.config && projectConfig.javascript.config.SourceDir
? path.normalize(projectConfig.javascript.config.SourceDir)
: 'src';
return path.join(
projectConfig.javascript && projectConfig.javascript.config && projectConfig.javascript.config.SourceDir
? path.normalize(projectConfig.javascript.config.SourceDir)
: 'src',
'models',
);
case 'android':
return projectConfig.android && projectConfig.android.config && projectConfig.android.config.ResDir
? path.normalize(path.join(projectConfig.android.config.ResDir, '..', 'java'))
Expand Down
58 changes: 13 additions & 45 deletions packages/amplify-codegen/src/commands/statements.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@ const Ora = require('ora');

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

async function generateStatements(context, forceDownloadSchema, maxDepth, withoutInit = false, decoupleFrontend = '') {
try {
Expand Down Expand Up @@ -56,31 +51,29 @@ async function generateStatements(context, forceDownloadSchema, maxDepth, withou
frontend = decoupleFrontend;
}
const language = frontend === 'javascript' ? cfg.amplifyExtension.codeGenTarget : 'graphql';
if (!Object.keys(FILE_EXTENSION_MAP).includes(language)) {
throw new Error(`Language ${language} not supported`);
}

const opsGenSpinner = new Ora(constants.INFO_MESSAGE_OPS_GEN);
opsGenSpinner.start();

try {
fs.ensureDirSync(opsGenDirectory);
const schemaData = readSchemaFromFile(schemaPath);
const generatedOps = generateGraphQLDocuments(schemaData, {
const relativeTypesPath = cfg.amplifyExtension.generatedFileName
? path.relative(opsGenDirectory, cfg.amplifyExtension.generatedFileName)
: null;
const generatedOps = generateStatementsHelper({
schema: schemaData,
target: language,
maxDepth: maxDepth || cfg.amplifyExtension.maxDepth,
useExternalFragmentForS3Object: language === 'graphql',
// default typenameIntrospection to true when not set
typenameIntrospection:
cfg.amplifyExtension.typenameIntrospection === undefined ? true : !!cfg.amplifyExtension.typenameIntrospection,
includeMetaData: true,
relativeTypesPath,
});
if (!generatedOps) {
context.print.warning('No GraphQL statements are generated. Check if the introspection schema has GraphQL operations defined.');
} else {
const relativeTypesPath = cfg.amplifyExtension.generatedFileName
? path.relative(opsGenDirectory, cfg.amplifyExtension.generatedFileName)
: null;
await writeGeneratedDocuments(language, generatedOps, opsGenDirectory, relativeTypesPath);
await writeGeneratedDocuments(language, generatedOps, opsGenDirectory);
opsGenSpinner.succeed(constants.INFO_MESSAGE_OPS_GEN_SUCCESS + path.relative(path.resolve('.'), opsGenDirectory));
}
} finally {
Expand All @@ -89,35 +82,10 @@ async function generateStatements(context, forceDownloadSchema, maxDepth, withou
}
}

async function writeGeneratedDocuments(language, generatedStatements, outputPath, relativeTypesPath) {
const fileExtension = FILE_EXTENSION_MAP[language];

['queries', 'mutations', 'subscriptions'].forEach(op => {
const ops = generatedStatements[op];
if (ops && ops.size) {
const formattedStatements = new GraphQLStatementsFormatter(language, op, relativeTypesPath).format(ops);
const outputFile = path.resolve(path.join(outputPath, `${op}.${fileExtension}`));
fs.writeFileSync(outputFile, formattedStatements);
}
function writeGeneratedDocuments(language, generatedStatements, outputPath) {
Object.entries(generatedStatements).forEach(([filepath, contents]) => {
fs.outputFileSync(path.resolve(path.join(outputPath, filepath)), contents);
});

if (fileExtension === 'graphql') {
// External Fragments are rendered only for GraphQL targets
const fragments = generatedStatements['fragments'];
if (fragments.size) {
const formattedStatements = new GraphQLStatementsFormatter(language).format(fragments);
const outputFile = path.resolve(path.join(outputPath, `fragments.${fileExtension}`));
fs.writeFileSync(outputFile, formattedStatements);
}
}
}

const FILE_EXTENSION_MAP = {
javascript: 'js',
graphql: 'graphql',
flow: 'js',
typescript: 'ts',
angular: 'graphql',
};

module.exports = generateStatements;
59 changes: 45 additions & 14 deletions packages/amplify-codegen/src/commands/types.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const glob = require('glob-all');
const path = require('path');
const fs = require('fs-extra');
const Ora = require('ora');
const glob = require('glob-all');

const constants = require('../constants');
const { loadConfig } = require('../codegen-config');
const { ensureIntrospectionSchema, getFrontEndHandler, getAppSyncAPIDetails } = require('../utils');
const { generate } = require('@aws-amplify/graphql-types-generator');
const { generateTypes: generateTypesHelper } = require('@aws-amplify/graphql-generator');
const { extractDocumentFromJavascript } = require('@aws-amplify/graphql-types-generator');

async function generateTypes(context, forceDownloadSchema, withoutInit = false, decoupleFrontend = '') {
let frontend = decoupleFrontend;
Expand Down Expand Up @@ -37,39 +39,68 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
}

try {
projects.forEach(async cfg => {
for (const cfg of projects) {
const { generatedFileName } = cfg.amplifyExtension || {};
const includeFiles = cfg.includes;
if (!generatedFileName || generatedFileName === '' || includeFiles.length === 0) {
return;
}
const target = cfg.amplifyExtension.codeGenTarget;

const excludes = cfg.excludes.map(pattern => `!${pattern}`);
const queries = glob.sync([...includeFiles, ...excludes], {
cwd: projectPath,
absolute: true,
});
const schemaPath = path.join(projectPath, cfg.schema);
const target = cfg.amplifyExtension.codeGenTarget;
const queries = 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;
})
.join('\n');

const outputPath = path.join(projectPath, generatedFileName);
const schemaPath = path.join(projectPath, cfg.schema);
let region;
if (!withoutInit) {
({ region } = cfg.amplifyExtension);
await ensureIntrospectionSchema(context, schemaPath, apis[0], region, forceDownloadSchema);
}
const codeGenSpinner = new Ora(constants.INFO_MESSAGE_CODEGEN_GENERATE_STARTED);
codeGenSpinner.start();
const schema = fs.readFileSync(schemaPath, 'utf8');
const introspection = path.extname(schemaPath) === '.json';

try {
generate(queries, schemaPath, path.join(projectPath, generatedFileName), '', target, '', {
addTypename: true,
complexObjectSupport: 'auto',
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);
} 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) {
codeGenSpinner.fail(err.message);
}
});
}
} catch (err) {
throw Error(err.message);
}
Expand Down
29 changes: 0 additions & 29 deletions packages/amplify-codegen/src/utils/getOutputFileName.js

This file was deleted.

Loading