Skip to content

Instantly share code, notes, and snippets.

@hisham
Created February 28, 2020 17:59
Show Gist options
  • Save hisham/93ac6fcbe66f4a346d32681dc83e5ce2 to your computer and use it in GitHub Desktop.
Save hisham/93ac6fcbe66f4a346d32681dc83e5ce2 to your computer and use it in GitHub Desktop.
Modify Amplify CLI Generated Cloudformation File to Support a GraphQL Interface
/**
* Example script to change amplify cli-generated cloudformation stack to support a graphql interface. See https://github.com/aws-amplify/amplify-cli/issues/202.
* Run this before every amplify push command (e.g. create your own bash script that does something like: amplify api gql-compile && node ./scripts/modifyAmplifyCF.js && ./amplify/backend/api/<your_app_name>/build/cloudformtion-template.json && amplify push --no-gql-override)
* Everytime you run amplify api gql-compile, the cloudformation files will be overwritten and this script would need to be run again.
*
* As an example for this script, it assumes you have a custom User table built (through custom stacks - User.json for the AppSync resolvers and UserTable.json for the dynamodb table) and a Doctor and Patient model that implement this interface, e.g.
*
* interface User {
* id: ID!
*. email: AWSEmail!
* }
*
* type Doctor implements User @model {
* id: ID!
*. email: AWSEmail
*. licenseNumber: Int
* }
*
* type Patient implements User @model {
*. id: ID!
*. email: AWSEmail
*. healthcardNumber: Int
* }
*/
const AWS = require('aws-sdk');
AWS.config.update({ region: '<your_region>' });
const fs = require('fs');
const path = require('path');
const fileName = process.argv[2];
const stacksDir = path.join(path.dirname(fileName), 'stacks');
run();
async function run() {
changeDependencies(fileName);
changeConnectionStack(path.join(stacksDir, 'ConnectionStack.json'));
changeModelStack(path.join(stacksDir, 'Patient.json'));
changeModelStack(path.join(stacksDir, 'Doctor.json'));
}
function changeDependencies(cfFileName) {
const jsonFileContent = fs.readFileSync(cfFileName);
const jsonContent = JSON.parse(jsonFileContent);
jsonContent.Resources.UserTablejson.DependsOn = ['GraphQLSchema'];
jsonContent.Resources.Userjson.DependsOn = ['GraphQLSchema', 'UserTablejson'];
jsonContent.Resources.Doctor.DependsOn = ['GraphQLSchema', 'Userjson'];
jsonContent.Resources.Patient.DependsOn = ['GraphQLSchema', 'Userjson'];
delete jsonContent.Resources.Doctorjson;
fs.writeFileSync(fileName, JSON.stringify(jsonContent, null, 2));
console.log(`Updated ${cfFileName}`);
}
function changeConnectionStack(stackFileName) {
let stackFileContent = fs.readFileSync(stackFileName, 'utf8');
stackFileContent = stackFileContent.replace(
/DoctorDataSource|PatientDataSource/gi,
'UserDataSource'
);
fs.writeFileSync(stackFileName, stackFileContent);
console.log(`Updated ${stackFileName}`);
}
function changeModelStack(stackFileName) {
const stackFileContent = fs.readFileSync(stackFileName, 'utf8');
const jsonContent = JSON.parse(stackFileContent);
Object.keys(jsonContent.Resources).forEach(key => {
if (key.endsWith('Resolver')) {
jsonContent.Resources[key].Properties.DataSourceName = 'UserTable';
} else {
delete jsonContent.Resources[key];
}
});
delete jsonContent.Outputs;
fs.writeFileSync(stackFileName, JSON.stringify(jsonContent, null, 2));
console.log(`Updated ${stackFileName}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment