Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Swift: Cannot convert Type<Subtype> to Type<Any>

I am looking to create a function to handle potential errors in GraphQL mutation results, using the Apollo SDK. Apollo can have errors as part of its success data.

To keep it as generic as possible, I define my function as:

    func alertResult(result: Result<GraphQLResult<Any>, Error>) {
        print(result)
        switch result {
        case let .failure(error):
            self.presentAlert(title: "Operation Error", message: "API says: \(error)")
        case let .success(data):
            if let errors = data.errors {
                self.presentAlert(title: "Operation Error", message: "API says: \(errors)")
            }
        }
    }

When used with a specific Apollo mutation, InviteToCommunityMutation, the compiler rejects it with the following error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Cannot convert value of type 'Result<GraphQLResult<InviteToCommunityMutation.Data>, any Error>' to expected argument type 'Result<GraphQLResult<Any>, any Error>'

I find this bizarre, as surely InviteToCommunityMutation.Data should be convertible to Any.

If I try to define my function more broadly with:

func alertResult(result: Result<Any, Error>)

then I get Type of expression is ambiguous without more context when trying to extract data.errors from .success(data).

Would love help! Also, I’m new to Stackoverflow, so tried to include only relevant data but happy to provide more.

>Solution :

Make your function generic:

func alertResult<T>(result: Result<GraphQLResult<T>, Error>) {

By using T as a generic placeholder type, you allow Swift to synthesize an appropriate function for any subtype of GraphQLResult.

You can read about Swift Generics here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading