Cannot find type 'AnyCancellable' in scope from amplify code snippit

Viewed 3717
import Foundation
import SwiftUI
import Amplify


class MovesAPI: ObservableObject {
    
    
    @Published var todoLIst: [MoveType?] = [nil]
    
    
    init() {
        listTodos()
    }
    
    func listTodos() -> AnyCancellable { //cannot find type 'AnyCancellable' in scope
        let moveType = MoveType.keys
        let sink = Amplify.API.query(request: .paginatedList(MoveType.self)) //Generic parameter 'R' could not be inferred //Type 'GraphQLRequest<_>' has no member 'paginatedList'
            .resultPublisher
            .sink {
                if case let .failure(error) = $0 {
                    print("Got failed event with error \(error)")
                }
            }
            receiveValue: { result in
            switch result {
                case .success(let todos):
                    print("Successfully retrieved list of todos: \(todos)")
                case .failure(let error):
                    print("Got failed result with \(error.errorDescription)")
                }
            }
        return sink
    }

}

Ultimately my goal is to fetch all the models of type MoveType from the amplify GraphQL API. This is a code snippet pasted directly into my project and I'm getting these type errors. any idea why? From amplify, docs (https://docs.amplify.aws/lib/graphqlapi/query-data/q/platform/ios#query-by-id) all I did was change the model names to match my backend and remove query params.

3 Answers

I think you need:

import Combine

I faced this problem of paginatedList code snippet from amplify docs not working as I downloaded Amplify using Cocoapods. When I followed the instructions to download Amplify from Swift Package Manager (SPM) instead at Amplify iOS Github , the paginatedList not working problem is solved. Importing the Combine framework into my Xcode project did not solve the paginatedList problem.

okay got it working, just had to go way simpler, I switched off using the combined mode

import Foundation
import SwiftUI
import Amplify


class MovesAPI: ObservableObject {
    
    
    @Published var todoLIst: [MoveType?] = [nil]
    
    
    init() {
    }
    
    func listTodos() {
        print("listing todos")
    
        Amplify.API.query(request: .list(MoveType.self)) { event in
            switch event {
               case .success(let result):
                   switch result {
                   case .success(let todos):
                       print("Successfully retrieved list of todos: \(todos)")
                   case .failure(let error):
                       print("Got failed result with \(error.errorDescription)")
                   }
               case .failure(let error):
                   print("Got failed event with error \(error)")
               }
           }
    
    }

}

still, if anyone knows why the code snippets from amplify docs weren't working I would love to know.

Related