You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何生成不含@aws-amplify/datastore定义的TypeScript模型

完全可行,给你几个实用方案:
  • 用Amplify CLI直接生成纯TS模型
    不用DataStore的话,直接执行命令:
    amplify codegen models --model-target typescript
    这个命令会跳过DataStore相关的代码,只生成纯TypeScript的接口和类型定义,生成的文件默认在src/models目录下,没有任何初始化逻辑或DataStore依赖,直接就能在Lambda里导入使用。

  • 用GraphQL Code Generator自定义生成
    这是更灵活的方案,完全脱离Amplify DataStore的约束:

    1. 先安装依赖:
      npm install -D @graphql-codegen/cli @graphql-codegen/typescript @graphql-codegen/typescript-resolvers
    2. 项目根目录创建codegen.yml配置文件,示例如下:
      schema: ./path/to/your/schema.graphql # 替换成你的GraphQL schema路径
      generates:
        ./src/generated/graphql-types.ts:
          plugins:
            - typescript
            - typescript-resolvers
          config:
            avoidOptionals: true # 可根据需求调整,比如是否强制非空类型
      
    3. 运行npx graphql-codegen就能生成纯TS类型,完全和DataStore无关,适合自定义程度高的场景。
  • 手动定义类型(适合简单Schema)
    如果你的GraphQL Schema比较简单,直接对照Schema手动写TypeScript接口就行,比如GraphQL定义了Post类型,对应的TS接口可以这么写:

    interface Post {
      id: string;
      title: string;
      content?: string;
    }
    

    这种方式最直接,不需要任何工具依赖。

内容的提问来源于stack exchange,提问作者Charles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 09:23:07