在 GraphQL 中编辑 class 时的正确工作流程

The right workflow when editing class in GraphQL

我正在使用 xcode 11.4 和 swift4,我目前正在使用 AWS GraphQL 并学习正确的工作流程。我的 amplify.xyz 配置设置为

push=true
modelgen=true
profile=default
envName=amplify

这样就可以按原样生成模型 created/edited。在 schema.graphql 我定义用户:

type User @model {
    id: ID!
    firstName  : String!
    lastName   : String!
    handle     : String!
    email      : String!
}

和 build/run 应用程序,并且能够按预期 create/read user 的实例。然后假设我添加了一个简单的新字段 User @model 这样我就有了:

type User @model {
    id: ID!
    firstName  : String!
    lastName   : String!
    handle     : String!
    email      : String!
    blank      : String!
}

然后清理构建文件夹,并重建应用程序。然后我得到莫名其妙的错误

No such module 'Amplify' HomeController.swift

即使更改 Model class 和 Amplify 似乎无关。如果我删除 blank,然后清理并重建,一切又恢复正常了。这种行为的原因是什么?

作为参考,这是我的播客文件:

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'alpha' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for alpha
    pod 'amplify-tools'

    pod 'Amplify'
    pod 'AWSPluginsCore'
    pod 'AmplifyPlugins/AWSAPIPlugin'

    pod 'AWSMobileClient', '~> 2.13.0'      # Required dependency
    pod 'AWSUserPoolsSignIn', '~> 2.13.0'

    pod 'AWSAppSync', '~> 3.1.0'
    pod 'AWSMobileClient', '~> 2.13.0'
    pod 'AWSAuthUI', '~> 2.13.0'
    pod 'AWSUserPoolsSignIn', '~> 2.13.0'

end

______________________更新___________________

我按照 Julien S 的建议 amplify push,并确保 amplify/generated/models 中的所有文件都移动到每个 (https://aws-amplify.github.io/docs/ios/start?ref=amplify-iOS-btn) 的顶级目录。现在这个问题No such module 'Amplify' HomeController.swift 已经解决了。但是我再也找不到模型更新前保存的数据了。作为参考,当用户创建帐户时,我会访问用户的令牌并将其与用户的电子邮件一起保存。然后下次用户打开应用程序时,我再次获取令牌并通过令牌查询用户数据库。相关代码:

class CognitoPoolProvider : AWSCognitoUserPoolsAuthProviderAsync {

    func getLatestAuthToken(_ callback: @escaping (String?, Error?) -> Void) {

        AWSMobileClient.default().getTokens { (token, error) in
            if let error = error {
                callback(nil,error)
            }
            callback(token?.accessToken?.tokenString, error)
        }
    }
}

在MainController.swift中:

override func viewDidLoad() {
    super.viewDidLoad()

    // get user token
    let pool = CognitoPoolProvider();

    pool.getLatestAuthToken { (token, error) in

        if let error = error {

            print("error: \(error)")

        } else {
            self.getUserData(token: token!)
        }
    }
}

func getUserData(token:String){

    print("token >>>> \(token)")

   // this is successful. you got all the user stuff
   // when you change the user model, you can no longer query the user
   let _ = Amplify.API.query(from: User.self, byId: token) { (event) in
        switch event {
            case .completed(let result):
                switch result {
                    case .success(let note):
                        guard let note = note else {
                            print("API Query completed but missing user")
                            return
                        }
                        print("API Query successful, got user: \(note)")
                case .failure(let error):
                    print("Completed with error: \(error.errorDescription)")
                    }
            case .failed(let error):
                print("Failed with error \(error.errorDescription)")
            default:
                print("Unexpected event")
        }
    }

}

我假设您正在通过 Amplify CLI 配置所有内容?您使用的是新的还是旧的 iOS 亚马逊 SDK?我的工作流程通常是将我的 schema.graphql 文件调整为 运行 放大推送命令以实际将这些更改传播到后端并确保它生成 API.swift 文件。您的 graphql 操作是通过自动生成的 api.swift 文件进行的 运行 吗?您还可以 运行 放大 codegen 以重新创建 api.swift 文件。