apollo-ios: GraphQLEnum and test mock crash

Bug report

Creating a test mock with an enum type causes a crash: // Could not cast value of type 'Swift.AnyHashable' to 'ApolloAPI.GraphQLEnum<MySchemaModule.MyEnum>'

Versions

Please fill in the versions you’re currently using:

  • apollo-ios SDK version: 1.0.5
  • Xcode version: 14.1
  • Swift version: 5.7
  • Package manager: 5.7

Steps to reproduce

  • Create a model with an enum
  • Generate test mocks
  • Initialize a model by using Model.from(Mock()) method
  • Get the enum property from the model
  • Crash in the DataDict

Further details

It’s the same crash as #2582

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 15 (13 by maintainers)

Most upvoted comments

Yes, that looks promising. For now we decided to just use the String value of that type and map it to our own enum.

Hi @Gois, I’ve been looking into this issue this week and I don’t have any good resolution to it. I think we’re dealing with an issue in Xcode + SPM dependencies which leads to a kind of Diamond Dependency problem. Is this still an issue for you or have you found a way to resolve it?

I think the key message in the debug output is

objc[97411]: Class _TtCO9issue26868MySchema14GetThingsQuery is implemented in both /Users/calvincestari/Library/Developer/XCTestDevices/59D4379F-CC44-4010-A3BB-2A179AFB310A/data/Containers/Bundle/Application/C1106A64-8760-48FC-8076-E7C670D1FF16/apollo-crash.app/apollo-crash (0x1023ee820) and /Users/calvincestari/Library/Developer/XCTestDevices/59D4379F-CC44-4010-A3BB-2A179AFB310A/data/Containers/Bundle/Application/C1106A64-8760-48FC-8076-E7C670D1FF16/apollo-crash.app/PlugIns/apollo-crashTests.xctest/apollo-crashTests (0x109b78a10). One of the two will be used. Which one is undefined.

I suspect the dependency configuration and this Xcode+SPM issue is causing the schema module to be included twice resulting in two types that are the same but different because they are defined in differently loaded modules, resulting in

Could not cast value of type 'Swift.AnyHashable' (0x1baf23698) to 'ApolloAPI.GraphQLEnum<issue2686.MySchema.Size>' (0x139066a88).

When you update to use the main branch, which includes some unreleased fixes, the error message becomes clearer as

Could not cast value of type 'ApolloAPI.GraphQLEnum<issue2686.MySchema.Size>' (0x12906d920) to 'ApolloAPI.GraphQLEnum<issue2686.MySchema.Size>' (0x129070088).

Creating a purely SPM project and configuring the code the same does not result in this error. From what I’ve found online in the Swift Forums it looks like SPM handles this internally by using a dynamic framework and it’s able to do this because it understands the whole dependency chain when building the binaries. I haven’t build a demo of it yet but I suspect an Xcode-only project, without SPM, would also have a working test case. We tried splitting the generated schema module into two packages which didn’t resolve the issue and it still crashed. The only thing that worked for us in the Xcode + SPM project was to use dynamic packages.

We’ll keep digging to see what else we can do to try resolve but I’m at a bit of a dead end right now.

@calvincestari With the sample project it works for me as well but I’ve included another sample project. If you go to the apollo_crashTests class you’ll see the test testMapGraphQLEnumProperty that one fails for me.

The only time it does work for me is when I also include AppThingMapper in the test target instead of accessing it via the testable import.

apollo-crash.zip

Thanks for creating the issue @Gois. We’ll take a closer look and see what can be done.