apollographql / apollo-ios

📱  A strongly-typed, caching GraphQL client for iOS, written in Swift.
https://www.apollographql.com/docs/ios/
MIT License
3.86k stars 714 forks source link

CodegenCLI - File name collision if Fragment has the same name as Type #2598

Open p4checo opened 1 year ago

p4checo commented 1 year ago

Bug report

In our setup we define fragments with the same name as the schema types, e.g.:

fragment ProductImage on ProductImage {
    extraLarge
    large
    medium
    small
}

While this wasn't an issue with the previous single-file codegen, in the current multi-file codegen of Apollo 1.0.x this is causing an issue because two files with the same ProductImage.graphql.swift name are generated (one in /Schema/Objects and another in /Fragments). This causes compilation fail with an error like this:

error: filename "ProductImage.graphql.swift" used twice: '(...)/GraphQL/Generated/Schema/Objects/ProductImage.graphql.swift' and '(...)/GraphQL/Generated/Fragments/ProductImage.graphql.swift'
note: filenames are used to distinguish private declarations with the same name

The generated types themselves are properly namespaced so they don't collide, it's only the filenames themselves cause the problem:

Fragment

public extension GraphQL {
  struct ProductImage: GraphQL.SelectionSet, Fragment {
(...)

Object

public extension GraphQL.Objects {
  static let ProductImage = Object(
    typename: "ProductImage",
    implementedInterfaces: []
  )
}

While we can rename all our fragments to not have the same name as the type, we feel that this could also be fixed at the codegen phase, by adding a Fragment suffix to the file (e.g. Foo.Fragment.graphql.swift) instead of relying only on the folder structure (which can possibly cause other similar collisions). Would it make sense? Could also be added as a parameter of the config.

Versions

Please fill in the versions you're currently using:

Steps to reproduce

calvincestari commented 1 year ago

Thanks for the bug report @p4checo. I'd prefer not to add yet another configuration option, there are already too many. I'd rather opt for a consistent "Fragment" suffix on the filename. I'll see if there are any other options.

SzymonMatysik commented 1 year ago

Hey, I've also run into this issue. Is "Fragment" suffix solution available in configuration? Cheers

calvincestari commented 1 year ago

@SzymonMatysik, no fix available yet. The work is slated for the 1.1 release, we're currently working on issues for the next patch release - 1.0.6.

jostster commented 1 year ago

I am also having this issue after migrating to the new codegen cli now that we switched from cocoapods to SPM. Is there any work around so we don't have to rename 50+ files after generating them?

calvincestari commented 1 year ago

Hi @jostster - the only options currently are:

  1. rename the fragments, I realize this may not always be possible in larger orgs where the named fragments may not be under your direct control.
  2. rename the generated files, since all named fragments are generated into a single folder within the schema module output location a script to rename every file in that folder may ease the burden and become something that could be automated.
jostster commented 1 year ago

Yea for now I created a bash script to generate then rename all the files. Is there a flag or option to have all the generated files be combined into a single file? Since they are generated and shouldn't be edited this could also ease the burden and help larger projects not have a bunch of different generated files.

calvincestari commented 1 year ago

Is there a flag or option to have all the generated files be combined into a single file?

No, the legacy (0.x) versions used to have that option but it was removed in 1.0 to support modular architectures. It was also a poor performance option re. Xcode when it was an unwieldily large file.

AnthonyMDev commented 3 months ago

This will be fixed by #3283.