apollo-ios icon indicating copy to clipboard operation
apollo-ios copied to clipboard

Invalid type aliases generated on recursive types

Open jmccance opened this issue 1 year ago • 7 comments

Summary

When fragments have a recursive structure, the generated type alias can become self-referencing and the module can't compile.

/Users/jmccance/Development/apollographql-typealias-references-itself/MySchema/Sources/Operations/Queries/GetLinksQuery.graphql.swift:123:28: error: type alias 'Link' references itself
          public typealias Link = Link.To.Link

Version

1.9.0

Steps to reproduce the behavior

I've confirmed this behavior on 1.8.0 and 1.9.0. It seems to occur when using interfaces and multiple fragments on that interface.

schema.graphqls
type Query {
  links: [Link!]!
}

interface Linkable {
  id: ID!
  links: [Link!]!
}

interface Link implements Linkable {
  id: ID!
  to: Linkable!
}

type ChildLink implements Link & Linkable {
  id: ID!
  to: Linkable!
}
GetLinks.gql
query GetLinks {
  links {
    to {
      id
      links {
        id
      }
    }
  }

  links {
    ...MyLink
    ...MyChildLink
  }
}

fragment MyLink on Link {
  to {
    id
  }
}

fragment MyChildLink on ChildLink {
  id
}

These two together generate this offending block:

        /// Link.AsChildLink.To
        ///
        /// Parent Type: `Linkable`
        public struct To: MySchema.SelectionSet {
          public let __data: DataDict
          public init(_dataDict: DataDict) { __data = _dataDict }

          public static var __parentType: ApolloAPI.ParentType { MySchema.Interfaces.Linkable }

          public var id: MySchema.ID { __data["id"] }
          public var links: [Link] { __data["links"] }

          // !!! error: type alias 'Link' references itself
          public typealias Link = Link.To.Link
        }

For a complete reproduction of the bug, take a look at https://github.com/jmccance/apollographql-typealias-references-itself.

Logs

$ ./apollo-ios-cli generate && (cd MySchema && swift build)
Building for debugging...
error: emit-module command failed with exit code 1 (use -v to see invocation)
/Users/jmccance/Development/apollographql-typealias-references-itself/MySchema/Sources/Operations/Queries/GetLinksQuery.graphql.swift:123:28: error: type alias 'Link' references itself
          public typealias Link = Link.To.Link
                           ^
/Users/jmccance/Development/apollographql-typealias-references-itself/MySchema/Sources/Operations/Queries/GetLinksQuery.graphql.swift:123:43: note: while resolving type 'Link.To.Link'
          public typealias Link = Link.To.Link
                                          ^
/Users/jmccance/Development/apollographql-typealias-references-itself/MySchema/Sources/Operations/Queries/GetLinksQuery.graphql.swift:123:28: error: type alias 'Link' references itself
          public typealias Link = Link.To.Link
                           ^
/Users/jmccance/Development/apollographql-typealias-references-itself/MySchema/Sources/Operations/Queries/GetLinksQuery.graphql.swift:123:43: note: while resolving type 'Link.To.Link'
          public typealias Link = Link.To.Link
                                          ^
error: fatalError

Anything else?

No response

jmccance avatar Feb 16 '24 20:02 jmccance

Thanks for the detailed report with reproduction case! I see what's going on here. We'll look into getting a fix out for this soon.

AnthonyMDev avatar Mar 08 '24 18:03 AnthonyMDev

Hello Anthony, any updates on this issue? This is still blocking us from updating apollo

iAmericanBoy avatar Apr 03 '24 13:04 iAmericanBoy

Hi Dominic. Thanks for letting us know that this is also blocking you. I'm working on a complex, high priority feature right now, so I'm not going to be able to get to this just yet, but I've marked this issue as high priority also and hope to dig into in as soon as I'm finished with what I'm working on. Can't give too great of an ETA until I get to research how to solve this.

AnthonyMDev avatar Apr 04 '24 16:04 AnthonyMDev

Sorry Anthony, Joel and I work together at M1. I was just trying to follow up on the status thank you for the update. And thank you for keeping an eye on this

iAmericanBoy avatar Apr 04 '24 17:04 iAmericanBoy

@AnthonyMDev Just checking in: is investigating this still on the roadmap? We're still stuck on 1.7.1 because of this issue.

jmccance avatar Jun 07 '24 14:06 jmccance

Hey there. Sorry that we haven't had the chance to get to this one yet. I'm not sure when we'll get there, but I'm thinking about it again and realizing you should be able to workaround this with field aliases.

Using a field alias will change the name of the generated types, so you can avoid the naming collision. You could change this part of your query definition to something like this:

outerLinks: links {
  to {
    innerLinks: links {
      ...
    }
  }
}

This would cause the generated code to look like this:

 /// OuterLink.AsChildLink.To
        ///
        /// Parent Type: `Linkable`
        public struct To: MySchema.SelectionSet {
          public let __data: DataDict
          public init(_dataDict: DataDict) { __data = _dataDict }

          public static var __parentType: ApolloAPI.ParentType { MySchema.Interfaces.Linkable }

          public var id: MySchema.ID { __data["id"] }
          public var innerLinks: [InnerLink] { __data["links"] }

          // !!! error: type alias 'Link' references itself
          public typealias Link = OuterLink.To.InnerLink
        }

Would something like this work for you in the interim?

AnthonyMDev avatar Jun 07 '24 19:06 AnthonyMDev

@AnthonyMDev Reviewing the code again, we found we were actually able to make some changes to remove the self-referencing field entirely. Appreciate the idea on the aliases, though! Hopefully that will help anyone else who runs into this until it can be fixed.

jmccance avatar Jun 14 '24 19:06 jmccance

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo iOS usage and allow us to serve you better.

github-actions[bot] avatar Aug 27 '24 17:08 github-actions[bot]