Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid type aliases generated on recursive types #3338

Open
jmccance opened this issue Feb 16, 2024 · 4 comments
Open

Invalid type aliases generated on recursive types #3338

jmccance opened this issue Feb 16, 2024 · 4 comments
Assignees
Labels

Comments

@jmccance
Copy link

jmccance commented Feb 16, 2024

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 jmccance added bug Generally incorrect behavior needs investigation labels Feb 16, 2024
@AnthonyMDev AnthonyMDev added this to the Patch Releases (1.x.x) milestone Mar 8, 2024
@AnthonyMDev
Copy link
Contributor

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 AnthonyMDev self-assigned this Mar 8, 2024
@iAmericanBoy
Copy link

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

@AnthonyMDev
Copy link
Contributor

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.

@iAmericanBoy
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants