我使用的是xcode 11.4和swift4,目前正在使用AWS GraphQL并正在学习正确的工作流程。我的amplify.xyz
配置设置为
push=true
modelgen=true
profile=default
envName=amplify
这样就可以在创建/编辑模型时生成它们。在schema.graphql
我定义用户:
type User @model {
id: ID!
firstName : String!
lastName : String!
handle : String!
email : String!
}
并构建/运行该应用程序,并能够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
班级并Amplify
显得无关。如果删除blank
,然后清理并重建,则一切恢复正常。这种行为的原因是什么?
供参考,这是我的podfile:
# 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
______________________更新___________________
我amplify push
建议按照Julien S的建议,并确保将其中的所有文件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配置所有内容?您是否正在使用适用于Amazon的新旧iOS SDK?我的工作流程通常是在调整schema.graphql文件以运行放大推送命令时将这些更改实际传播到后端,并确保使其生成API.swift文件。您的graphql操作是否正在通过自动生成的api.swift文件运行?您也可以运行放大代码生成来重新创建api.swift文件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句