apollographql / apollo-ios

šŸ“± Ā A strongly-typed, caching GraphQL client for iOS, written in Swift.
https://www.apollographql.com/docs/ios/
MIT License
3.87k stars 718 forks source link

Generated code cannot compile when using include directive on field that is selected on interface and implementation #3424

Closed guillian-balisi closed 1 month ago

guillian-balisi commented 1 month ago

Summary

Apollo is generating some intermediary type If<name_of_boolean_parameter> when selecting fields both on an interface type and an type that implements that interface while using the @include directive. This If<name_of_boolean_parameter> type uses types that are not defined. The generated code does not compile.

Version

1.14.0

Steps to reproduce the behavior

This example is based on the final project of https://github.com/apollographql/iOSTutorial/.

Please download the project on my fork here: https://github.com/guillian-balisi/iOSTutorial/. The differences are in https://github.com/guillian-balisi/iOSTutorial/commit/5075b109e7157b1333a982c66886aea119b6bd95, namely:

  1. Added some new types in schema
  2. Added Foo.graphql query
  3. Ran ./apollo-ios-cli generate

If you download the project and open final/RocketReserver.xcodeproj and try to build, it will fail.

Please see the generated code for Foo.graphql: https://github.com/guillian-balisi/iOSTutorial/blob/main/final/RocketReserverAPI/Sources/Operations/Queries/FooQuery.graphql.swift.

Compilation fails because there is the IfIncludeCurrentUserAwards struct with property awardingByCurrentUser of type AwardingByCurrentUser, but type AwardingByCurrentUser does not exist within this namespace.

The workaround we've been using is to move the @include(if: $includeCurrentUserAwards) directive off the awardings field and on to the awardingByCurrentUser field:

query Foo($includeCurrentUserAwards: Boolean = false) {
    postsInfoByIds {
        ... on Post {
            awardings {
                total
            }
        }
        awardings {
            awardingByCurrentUser @include(if: $includeCurrentUserAwards) {   # <---- moved the directive here
                id
            }
        }
    }
}

With that the generated code compiles. But we should expect the generated code to compile regardless of where we use the @include directive.

Logs

No response

Anything else?

No response

calvincestari commented 1 month ago

Thanks for reporting the issue @guillian-balisi, I'll take a look at the reproduction case and get back to you shortly.

calvincestari commented 1 month ago

Thanks for the reproduction case, I've confirmed it's a valid codegen bug. I'm hoping we can get this into the next release.

github-actions[bot] commented 1 month ago

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.