apollo-ios: Crash when accessing inner objects

Summary

I’m facing this crash Thread 1: Fatal error: Address expected DataDict for entity, got Optional<AnyHashable>. whenever I try to access any object from a fragment generated through the initializer passing a dictionary or the mock library.

Version

1.2.0

Steps to reproduce the behavior

let json: [String: AnyHashable] = ["name": "ABC", "innerObject": ["name": "innerABC"]]
let fragment = MyFragmentForExample(_dataDict: DataDict(data: json, fulfilledFragments: []))
let mock = Mock<MyObject>(name: "Abc", innerObject: .init(name: "innerABC"))
let fragment = MyFragmentForExample(from: mock)
print(fragment.innerObject.name) < The crash will happen here 

Logs

No response

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 2
  • Comments: 19 (9 by maintainers)

Most upvoted comments

@PedrinhoDS Thanks for providing a reproduction case! It looks like the crash here is related to some other issues like #2686 and #2813 where the issue has to do with how the SPM dependencies are handled in an Xcode project resulting in duplicate symbols for some code causing the crash.

I did notice in the sample project you provided if I link against the Apollo-Dynamic library instead of the Apollo library in the ApolloIssue target the crash goes away, not sure if that would work for your actual project or not, but it sounds as if you may have already worked around the issue for now.

@calvincestari thanks so much for linking that! I created an extension

extension RootSelectionSet {
    init(data: JSONObject) {
        // swiftlint:disable:next force_try
        try! self.init(data: data, variables: nil)
    }
}

which seems like it will not be needed now in place of the selection set initializers.

Yea I have to say taking away the ability to init the object the old way and forcing us to use a try with a json object is making migration a PAIN! We do a ton of swiftUI Previews which show filler data for developing and testing and this is pretty much breaking it.

I totally agree with jostster, not having normal init method (like we had on 0.51.X) is very counter productive

@ZsoltMolnarMBH - have you tried the selection set initializers from my comment earlier?

Thanks for the bug report @PedrinhoDS!

Using the SelectionSet.init(_dataDict:) is not officially supported for external use, and the way you are using it would definitely result in this crash. It’s an unsafe API. Instead you should be using MyFragmentForExample(data: json) which is a public API that will validate and transform your json object into the correct format for your model. I’m putting up a PR to add some more inline documentation on that internal initializer to point others in the right direction going forward.

The test mock version not working seems like a bug. I’ll add it to the list and we will take a look!