在嵌套的AppSync数组中放置/更新/删除项目。 React,用户设计器,Apollo

发布于 2025-01-30 22:18:52 字数 2139 浏览 2 评论 0原文

我的架构中有一个对象,该对象具有类似的数组:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红焚 2025-02-06 22:18:52

对于任何有兴趣的人来说,解决方案很简单。
我必须将架构中的注释类型修改为:

type Comment @aws_cognito_user_pool {        
    id: ID!
    sourceId: ID!
    body: String 
    parent: String 
    createdAt: String  
}   

input CommentInput {        
    body: String 
    parent: String 
    createdAt: String  
} 

然后添加突变和解析器以创建,更新和删除评论。

在FOO评论字段中,需要一个查询解析器。

这也意味着需要一张新表供评论。

该表需要将sourceID作为主键,而id作为sort键,并在sourceID上设置全局索引
我用适当的Apollo突变换了组件中的CRUD功能,然后完成了。

For anyone interested, the solution was simple.
I had to modify the comment type in schema to:

type Comment @aws_cognito_user_pool {        
    id: ID!
    sourceId: ID!
    body: String 
    parent: String 
    createdAt: String  
}   

input CommentInput {        
    body: String 
    parent: String 
    createdAt: String  
} 

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 and ID as sort key, with a global index setup on the sourceId
I swapped out the CRUD functions in my component with the appropriate Apollo mutations and done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文