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

Mock object with [String: AnyHashable] data doesn't compile #3264

Closed
Cookiezby opened this issue Oct 25, 2023 · 4 comments
Closed

Mock object with [String: AnyHashable] data doesn't compile #3264

Cookiezby opened this issue Oct 25, 2023 · 4 comments
Labels
bug Generally incorrect behavior needs investigation

Comments

@Cookiezby
Copy link
Contributor

Cookiezby commented Oct 25, 2023

Summary

In the official site, the mock object can be initialized with DataDict type which receive data with type [String: AnyHashable], but it will throw compile error

Cannot convert value of type '[String : Any]' to expected dictionary value type 'String'

Version

1.6.1

Steps to reproduce the behavior

It is not related to specific version of Apollo, but maybe related to Swift language, I am testing with Swift 5.9, the snippet from documentation site can not compile correctly

let data: [String: AnyHashable] = [
    "data": [
        "__typename": "Query",
        "hero": [
            "__typename": "Human",
            "id": "123",
            "name": "Obi-wan Kenobi",
            "friends": [
                [
                    "__typename": "Human",
                    "id": "456",
                    "name": "Padme Amidala"
                ],
                [
                    "__typename": "Droid",
                    "id": "789",
                    "name": "C-3PO"
                ]
            ]
        ]
    ]
]

the error says Cannot convert value of type '[String : Any]' to expected dictionary value type 'String'

If you change the type from [String: AnyHashable] to [String: Any] , it won't throw any issue, but the DataDict can not receive [String: Any]

Logs

No response

Anything else?

No response

@Cookiezby Cookiezby added bug Generally incorrect behavior needs investigation labels Oct 25, 2023
@Cookiezby
Copy link
Contributor Author

This issue is also related to this problem #3179

@Cookiezby
Copy link
Contributor Author

Cookiezby commented Oct 25, 2023

I found out it needs to define every nested dictionary as [String: AnyHashable] so that the data can be [String: AnyHashable]

let frind: [String: AnyHashable] =  [
    "__typename": "Human",
    "id": "456",
    "name": "Padme Amidala"
]

let hero: [String: AnyHashable] = [
    "__typename": "Human",
    "id": "123",
    "name": "Obi-wan Kenobi",
    "friends": [
        frind
    ]
]

let query: [String: AnyHashable] = [
    "__typename": "Query",
    "hero": hero
]

let data: [String: AnyHashable] = [
    "data": query
]

@bdbergeron
Copy link
Contributor

bdbergeron commented Nov 1, 2023

This is a Swift language/compiler issue and not inherent to apollo-ios. Try doing this instead:

let data: [String: AnyHashable] = [
  "data": [
    "__typename": "Query",
    "hero": [
      "__typename": "Human",
      "id": "123",
      "name": "Obi-wan Kenobi",
      "friends": [
        [
          "__typename": "Human",
          "id": "456",
          "name": "Padme Amidala"
        ],
        [
          "__typename": "Droid",
          "id": "789",
          "name": "C-3PO"
        ]
      ]
    ] as [String: AnyHashable]
  ] as [String: AnyHashable]
]

Simply adding those as [String: AnyHashable] instructions helps the compiler's understanding of what the nested dictionary values are. Otherwise, the compiler falls back to the implicit [String: Any] type.

@AnthonyMDev
Copy link
Contributor

We've merged in PR #102 to fix this issue. Will be pushed out in the next release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Generally incorrect behavior needs investigation
Projects
None yet
Development

No branches or pull requests

3 participants