Search code examples
amazon-web-servicesgraphqlaws-appsyncaws-cdk

How to retrieve the schema.graphql file from @aws-cdk/aws-appsync before deployment


I am using the code-first approach in @aws-cdk/aws-appsync for generating my graphql schema. For Typescript code generation purposes I need a way to retrieve the schema.graphql before deployment (maybe somehow extracting it from the cdk synth command?).


Solution

  • For getting the automatically created schema before deployment, use this script:

    readFile('cdk.out/<YOUR-APP-NAME>.template.json', 'utf8', (err, data) => {
      if (err) {
        throw err;
      }
      const definition = JSON.parse(data)
        .Resources
        .<YOUR-SCHEMA-ID> // replace with your schema id
        .Properties
        .Definition;
      writeFile('lib/domain/generated.graphql', definition, (error) => {
        if (error) throw error;
      });
    });