几天前,我已经成功集成了AWS-Client-App示例,然后从零开始。让我给您我的项目说明:-
Xcode version : Version 9.4 (9F1027a)
Language : Swift 4.0
Database : DynamoDB
And Using AppSync for syncing database
我有一个模式
type Mutation {
#------ User ------#
CreateUser(
FirstName: String,
Username: String,
Password: String,
CreatedOn: Int
): User
UpdateUser(UserId: ID!, FirstName: String!, Username: String!): User
DeleteUser(UserId: ID!): User
}
type Query {
####------ User ------#
GetUser(UserId: ID!): User
CheckUsername(Username: String): [User]
CheckPhoneNumber(Phone: String): [User]
#------ Login ------#
CheckUserLogin(Username: String, Password: String): [User]
}
type User {
UserId: ID!
FirstName: String
Username: String
#Password: String
Bio: String
Phone: String
Email: String
Address: String
WebsiteUrl: String
ProfilePicture: String
CreatedOn: Int
Updated: Int
Status: Boolean
LastLoggedIn: Int
IpAddress: String
}
schema {
query: Query
mutation: Mutation
}
.graphql文件中的简单查询
query CheckUsername($Username:String){
CheckUsername(Username:$Username)
{
Username
}
}
并生成了一个名为check Queries.swift的文件
import AWSAppSync
public final class CheckUsernameQuery: GraphQLQuery {
public static let operationString =
"query CheckUsername($Username: String) {\n CheckUsername(Username: $Username) {\n __typename\n Username\n }\n}"
public var Username: String?
public init(Username: String? = nil) {
self.Username = Username
}
public var variables: GraphQLMap? {
return ["Username": Username]
}
public struct Data: GraphQLSelectionSet {
public static let possibleTypes = ["Query"]
public static let selections: [GraphQLSelection] = [
GraphQLField("CheckUsername", arguments: ["Username": GraphQLVariable("Username")], type: .list(.object(CheckUsername.selections))),
]
public var snapshot: Snapshot
public init(snapshot: Snapshot) {
self.snapshot = snapshot
}
public init(checkUsername: [CheckUsername?]? = nil) {
self.init(snapshot: ["__typename": "Query", "CheckUsername": checkUsername.flatMap { $0.map { $0.flatMap { $0.snapshot } } }])
}
public var checkUsername: [CheckUsername?]? {
get {
return (snapshot["CheckUsername"] as? [Snapshot?]).flatMap { $0.map { $0.flatMap { CheckUsername(snapshot: $0) } } }
}
set {
snapshot.updateValue(newValue.flatMap { $0.map { $0.flatMap { $0.snapshot } } }, forKey: "CheckUsername")
}
}
public struct CheckUsername: GraphQLSelectionSet {
public static let possibleTypes = ["User"]
public static let selections: [GraphQLSelection] = [
GraphQLField("__typename", type: .nonNull(.scalar(String.self))),
GraphQLField("Username", type: .scalar(String.self)),
]
public var snapshot: Snapshot
public init(snapshot: Snapshot) {
self.snapshot = snapshot
}
public init(username: String? = nil) {
self.init(snapshot: ["__typename": "User", "Username": username])
}
public var __typename: String {
get {
return snapshot["__typename"]! as! String
}
set {
snapshot.updateValue(newValue, forKey: "__typename")
}
}
public var username: String? {
get {
return snapshot["Username"] as? String
}
set {
snapshot.updateValue(newValue, forKey: "Username")
}
}
}
}
}
然后我调用了这段代码来运行查询
let query = CheckUsernameQuery(Username:"testuser1")
AppSyncManager.Client().fetch(query: query, cachePolicy: .fetchIgnoringCacheData) {
(result, error) in
if error != nil {
print(error?.localizedDescription ?? "")
return
}
let username = result?.data?.checkUsername![0]?.username
}
我得到以下答复
▿ Optional<GraphQLResult<Data>>
▿ some : GraphQLResult<Data>
- data : nil
- errors : nil
- source : AWSAppSync.GraphQLResult<AppName.CheckUsernameQuery.Data>.Source.server
- dependentKeys : nil
当我在Appsync Api面板中对其进行测试但该查询无法在该应用程序中运行时,该查询效果很好。没有错误,没有数据,什么都没有?如果任何人有一个主意让我知道,我可能会做错什么,这对我有很大的帮助。谢谢。