Created
May 7, 2020 14:17
-
-
Save kcwinner/9ef3545701b78ca3024a51641633270c to your computer and use it in GitHub Desktop.
Simple example emulating custom model gen outside of amplify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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