Database AWS AppSync-使用1-M@连接和连接@模型实现多对多连接
遵循AWS文档(>多对多连接),我试图理解它们为多对多连接提供的解决方案示例(Amplify似乎还不支持) 模式是:Database AWS AppSync-使用1-M@连接和连接@模型实现多对多连接,database,amazon-web-services,graphql,aws-amplify,aws-appsync,Database,Amazon Web Services,Graphql,Aws Amplify,Aws Appsync,遵循AWS文档(>多对多连接),我试图理解它们为多对多连接提供的解决方案示例(Amplify似乎还不支持) 模式是: type Post @model { id: ID! title: String! editors: [PostEditor] @connection(name: "PostEditors") } # Create a join model and disable queries as you don't need them # and can query thro
type Post @model {
id: ID!
title: String!
editors: [PostEditor] @connection(name: "PostEditors")
}
# Create a join model and disable queries as you don't need them
# and can query through Post.editors and User.posts
type PostEditor @model(queries: null) {
id: ID!
post: Post! @connection(name: "PostEditors")
editor: User! @connection(name: "UserEditors")
}
type User @model {
id: ID!
username: String!
posts: [PostEditor] @connection(name: "UserEditors")
}
使用AWS AppSync控制台,到目前为止,我能够:
使用此变体创建用户:
mutation {
createUser(input:{
username: "theUserName"
}){
username
}
}
mutation {
createPost(input: {
title: "second post"
}){
title
}
}
使用此变体创建帖子:
mutation {
createUser(input:{
username: "theUserName"
}){
username
}
}
mutation {
createPost(input: {
title: "second post"
}){
title
}
}
但我不明白如何在一篇文章中添加多个编辑器?到目前为止,我能够使用PostEditor join将编辑器添加到帖子中,但在他们的示例中,有一条语句(我不太理解),因此我认为这不是一个好方法:
# Create a join model and disable queries as you don't need them
# and can query through Post.editors and User.posts
所以我猜使用这个连接模型来执行变异不是我必须要做的。然而,为了能够在帖子和编辑器之间建立这种关系,我创建了一个突变(从之前的两个突变中检索“postEditorPostId”和“postEditorEditorId”):
每次我添加一个新编辑器时,我是否需要执行之前的变异(因此变异将保持不变,但“postEditorEditorId”将发生变化?如果UI允许管理员添加50个或更多新编辑器(因此需要50个变异),那么这显然不是一种可伸缩的方法
最后,我可以使用此查询获得所需的信息:
query{
getUser(id: "YYY-YYY-YYY"){
username
posts {
items {
post {
title
}
}
}
}
}
有没有更好的方法(我想)给帖子添加编辑
编辑:
使用promise,我可以为一篇文章添加多个编辑器,但这涉及到在有用户的情况下以突变的形式执行:
const users = [{id: "U1", username: "user1"}, {id: "U2", username: "user2"}];
const post = { id: "P1", title: "Post 1" };
/*
After creating two users and a post using the approriate mutations
Using the CreatePost join below to make user1 and user2 editor on Post 1
*/
function graphqlCreatePostEditor(editorID) {
return new Promise((resolve, reject) => {
resolve(
API.graphql(graphqlOperation(createPostEditor, {
input: {
postID: post.id,
}
}))
)
})
}
let promises = users.map(user=> {
return graphqlCreatePostEditor(user.id)
.then(e => {
console.log(e)
return e;
})
});
Promise.all(promises)
.then(results => {
console.log(results)
})
.catch(e => {
console.error(e);
})
仍然在寻找是否有一种方法可以通过单基因突变的数组。只是好奇你是否能找到一种好的方法?