在嵌套的AppSync数组中放置/更新/删除项目。 React,用户设计器,Apollo
我的架构中有一个对象,该对象具有类似的数组:
type Foo @aws_cognito_user_pool {
id: ID!
name: String
comments: [Comment]
createdAt: AWSDateTime
}
输入和注释类型看起来像:
input FooInput @aws_cognito_user_pool {
name: String
comments: [CommentInput]
}
type Comment @aws_cognito_user_pool {
id: String
body: String
parent: String
createdAt: String
}
input CommentInput {
id: String
body: String
parent: String
createdAt: String
}
在我的应用中,我使用用户defucer来管理状态和阿波罗。
我的foo
还原器看起来像是
const INITIAL_FOO_STATE = () => {
name: "",
comments: [],
}
export const FooReducer = (state, action) => {
switch(action.field) {
case "name":
case "comments":
return {
...state, [action.field]: action.payload,
}
default:
return
}
}
我的组件中,我具有添加,更新和删除功能,例如:
const addComment = (body, parent) => {
CreateComment(
body,
parent,
)
.then((comment) => {
setComments([comment, ...comments]);
setActiveComment(null);
});
};
const updateComment = (body, commentId) => {
UpdateComment(body)
.then(() => {
const updatedComments = comments.map((item) => {
if (item.id === commentId) {
return {
...item,
body: body,
};
}
return item;
})
setComments(updatedComments);
setActiveComments(null);
})
};
const deleteComment = (commentId) => {
if (window.confirm("Are you sure you want to remove this comment?")) {
DeleteComment()
.then(() => {
const updatedComments = comments.filter((comment) => comment.id !== commentId);
setComments(updatedComments)
})
}
};
这在前端可以很好地工作,但是现在想将这些操作派遣到我的foo.com.comments array上后端并寻求一些帮助,以了解最佳方法,而无需其他桌子。 我当时想将调度放置在各自的。
I have an object in my schema which has an array like so:
type Foo @aws_cognito_user_pool {
id: ID!
name: String
comments: [Comment]
createdAt: AWSDateTime
}
the input and Comment Type look like so:
input FooInput @aws_cognito_user_pool {
name: String
comments: [CommentInput]
}
type Comment @aws_cognito_user_pool {
id: String
body: String
parent: String
createdAt: String
}
input CommentInput {
id: String
body: String
parent: String
createdAt: String
}
In my app I am using useReducer to manage state and apollo.
My Foo
reducer looks like so
const INITIAL_FOO_STATE = () => {
name: "",
comments: [],
}
export const FooReducer = (state, action) => {
switch(action.field) {
case "name":
case "comments":
return {
...state, [action.field]: action.payload,
}
default:
return
}
}
in my component i have the add, update and delete functions like so:
const addComment = (body, parent) => {
CreateComment(
body,
parent,
)
.then((comment) => {
setComments([comment, ...comments]);
setActiveComment(null);
});
};
const updateComment = (body, commentId) => {
UpdateComment(body)
.then(() => {
const updatedComments = comments.map((item) => {
if (item.id === commentId) {
return {
...item,
body: body,
};
}
return item;
})
setComments(updatedComments);
setActiveComments(null);
})
};
const deleteComment = (commentId) => {
if (window.confirm("Are you sure you want to remove this comment?")) {
DeleteComment()
.then(() => {
const updatedComments = comments.filter((comment) => comment.id !== commentId);
setComments(updatedComments)
})
}
};
This works just fine on the frontend, but now want to dispatch these actions to my Foo.comments array on the backend and looking for some help as to the best way without the need of another table.
I was thinking of placing the dispatch's within their respective .then()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于任何有兴趣的人来说,解决方案很简单。
我必须将架构中的注释类型修改为:
然后添加突变和解析器以创建,更新和删除评论。
在FOO评论字段中,需要一个查询解析器。
这也意味着需要一张新表供评论。
该表需要将
sourceID
作为主键,而id
作为sort键,并在sourceID上设置全局索引我用适当的Apollo突变换了组件中的CRUD功能,然后完成了。
For anyone interested, the solution was simple.
I had to modify the comment type in schema to:
then add the mutations and resolvers for create, update and delete of the Comment.
In the Foo comment field, a query resolver was needed.
This also meant a new table was needed for comments.
The table needed to have the
sourceId
as primary key andID
as sort key, with a global index setup on the sourceIdI swapped out the CRUD functions in my component with the appropriate Apollo mutations and done.