如何在部署前从@aws-cdk/aws-appsync 检索 schema.graphql 文件

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

我正在使用 code-first approach in @aws-cdk/aws-appsync 生成我的 graphql 架构。对于 Typescript 代码生成目的,我需要一种在部署之前检索 schema.graphql 的方法(也许以某种方式从 cdk synth 命令中提取它?)。

我不确定我们是否面临同样的问题。基本上我想访问我的客户端反应应用程序中的 GQL 模式,它与定义基础设施的 cdk 应用程序位于不同的存储库中。

我最终使用 aws-cli 通过以下命令提取 appsync 模式:

aws appsync get-introspection-schema --api-id [API-ID] --format SDL --no-include-directives outfile=[OUTFILE]

要在部署前获取自动创建的架构,请使用此脚本:

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;
  });
});