我有一个graphql模型如下(一个用户可以有很多帖子1:m)
type User @model {
id: ID!
fullName: String!
emailAddress: String!
posts: [Post] @connection(name: "UserPostConnection")
}
type Post @model {
id: ID!
text: String!
user: User @connection(name: "UserPostConnection")
createdAt: String
updatedAt: String
}
我正在尝试返回特定用户的所有帖子,如下所示:
let queryInput = GetUserQuery(id: "879db4f2-1dad-47b9-b70d-8bca15bf2549")
appSyncClient?.fetch(query: queryInput, cachePolicy: .fetchIgnoringCacheData) { (result, error) in
print(result?.data?.getUser?.posts)
}
}
但是结果不是返回一个数组,而是返回一个可选的:
Optional(AWSStackSocial.GetUserQuery.Data.GetUser.Post(snapshot: ["__typename": Optional("ModelPostConnection"), "nextToken": nil]))
我已经确认 a) 用户存在,并且 b) 帖子也存在。
在 AWS 上:
query {
getUser(id: "879db4f2-1dad-47b9-b70d-8bca15bf2549") {
id
posts {
items {
id
}
}
}
}
输出:
{
"data": {
"getUser": {
"id": "879db4f2-1dad-47b9-b70d-8bca15bf2549",
"posts": {
"items": [
{
"id": "04c4b571-6e92-4745-9a40-1a67a9d49d77"
},
{
"id": "0f44fe6e-4e15-4a8c-b591-555e8fa1f60e"
},
{
"id": "60c18c40-fea3-4ff3-8b21-df6857703dd2"
}
]
}
}
}
}