Skip to content

Instantly share code, notes, and snippets.

@kcwinner
Created May 7, 2020 14:17
Show Gist options
  • Save kcwinner/9ef3545701b78ca3024a51641633270c to your computer and use it in GitHub Desktop.
Save kcwinner/9ef3545701b78ca3024a51641633270c to your computer and use it in GitHub Desktop.
Simple example emulating custom model gen outside of amplify
const path = require('path');
const { readFileSync, writeFileSync, ensureFileSync, pathExistsSync } = require('fs-extra');
const { parse } = require('graphql');
const gqlCodeGen = require('@graphql-codegen/core');
const appSyncDataStoreCodeGen = require('amplify-codegen-appsync-model-plugin');
generate();
async function generate() {
const schemaContent = loadSchema('./schema.graphql');
const outputPath = path.join('./src');
const schema = parse(schemaContent);
const appsyncLocalConfig = await appSyncDataStoreCodeGen.preset.buildGeneratesSection({
baseOutputDir: outputPath,
schema,
config: {
target: 'typescript',
// directives: directiveDefinitions,
},
});
const codeGenPromises = appsyncLocalConfig.map(cfg => {
return gqlCodeGen.codegen({
...cfg,
plugins: [
{
appSyncLocalCodeGen: {},
},
],
pluginMap: {
appSyncLocalCodeGen: appSyncDataStoreCodeGen,
},
});
});
const generatedCode = await Promise.all(codeGenPromises);
appsyncLocalConfig.forEach((cfg, idx) => {
const outPutPath = cfg.filename;
ensureFileSync(outPutPath);
writeFileSync(outPutPath, generatedCode[idx]);
});
console.log(`Successfully generated models. Generated models can be found ${outputPath}`);
}
function loadSchema(schemaFilePath) {
if (pathExistsSync(schemaFilePath)) {
return readFileSync(schemaFilePath, 'utf8');
}
throw new Error('Could not load the schema');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment