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

Apollo iOS Codegen CLI生成重复Swift文件报错求助

解决Apollo Codegen生成Swift文件重名冲突问题

常见解决方法:

1. 修改Apollo配置文件,给fragment生成文件添加命名前缀/后缀

找到你的Apollo配置文件(通常是apollo.config.js或apollo-codegen-config.json),通过配置模板给fragment文件设置独特命名,避免和schema对象文件重名。示例配置:

module.exports = {
  client: {
    service: {
      name: "your-service-name",
      url: "your-graphql-endpoint-url"
    },
    output: {
      // 给fragment文件名添加固定前缀
      fragmentFileName: "Fragment_$fragmentName.graphql.swift",
      // 或者直接指定fragment的输出路径模板,确保和schema对象目录完全区分
      target: "./path/customer/Fragments/Fragment_$fragmentName.graphql.swift"
    },
    schemaDownload: {
      output: "./path/customer/Schema/schema.graphql"
    }
  }
};

2. 清理重复文件后重新生成

删除path/customer/Fragments/和path/customer/Schema/Objects/下的重复XXX.graphql.swift文件,然后重新执行生成命令:

apollo codegen generate --config apollo.config.js

3. 修改GraphQL定义的命名

如果你的fragment名称和schema中的对象类型重名(比如fragment叫User,schema里也有User对象),直接修改fragment的名称(比如改成UserProfileFragment),从根源避免生成重名文件。

4. 调整Xcode项目文件引用(不推荐,仅临时应急)

如果暂时无法修改配置或GraphQL定义,可以在Xcode中移除其中一份重复文件的引用(注意:必须确认保留的文件包含所需代码,schema对象文件是基础类型定义,fragment是查询片段,二者功能不同,优先通过前三种方法解决)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 22:10:30