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

如何配置TypeScript版GraphQL Code Generator让枚举为PascalCase而非蛇形命名

解决方案

你需要在typescript插件的同级位置添加config配置块,通过namingConvention参数指定类型命名规则为帕斯卡命名即可,修改后完整配置如下:

overwrite: true
generates:
  src/graphql/generated/graphql.ts:
    schema: ${API_ENDPOINT}
    documents: ['src/graphql/**/*.graphql', 'src/graphql/**/*.gql']
    plugins:
      - 'typescript'
      - 'typescript-operations'
      - 'typescript-react-apollo'
    # 新增配置块
    config:
      namingConvention:
        typeNames: pascal-case
        enumValues: keep
  ./graphql.schema.json:
    schema: ${API_ENDPOINT}
    plugins:
      - 'introspection'
hooks:
  afterAllFileWrite:
    - prettier --write

配置说明

  • typeNames: pascal-case:所有GraphQL类型(包含枚举、对象类型、输入类型等)的名称会自动转换为帕斯卡命名格式,你Schema中定义的TEST_TYPE会被转换为TestType,完全符合预期输出
  • enumValues: keep:保留枚举值的原始命名,避免TEST被自动转换为其他格式,保持和后端Schema的映射一致

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 17:27:02