System Information
- Strapi Version: 4.7.1
- Operating System: Ubuntu 18
- Database: Postgres
- Node Version: 18.3
- NPM Version: 8.19.3
- Yarn Version:
How to update one value – without deleting everything else.
I’m using a modified version of the backend Corporate Template, with Global, Pages, and “Component Sections” like Hero and LargeVideo.
The case is simple, the u in crud: update one value in one of the of ComponentSections. – To support an inline-editing feature of the frontend, where content is wrapped in a CKEditor (working, ready to go) that a user can make changes to then send back to Strapi. (The ultimate goal is for content authors to avoid the admin for some changes).
What is the best approach? Is it a GraphQL Page mutation, or do I need a custom Endpoint so I can update directly. I’m just looking for general direction.
Looking in the db, a lot of the content I want to update from the client is one or two tables, eg. components_sections_rich_texts, which has a PK id – that I know on the client. But I can’t see anyway to exploit that in gql directly. So I guess the custom endpoint direction makes sense.
But there should be a way in gql, right? I’m kind of close. I figure I need to use the … on Component syntax in the update part but I can’t work it out. When I run the following, if there is a RichText component on the page, it gets updated. If there are two, only the first is updated. If there is anything else on the page running this deletes it all except other RichText components.
I’m about to go down the custom endpoint path but I thought I’d see if anyone would show me how this could work:
mutation UpdatePage($id: ID!, $newVal: String!) {
updatePage(
id: $id
data: {
contentSections: [
{ __typename: "ComponentSectionsRichText", content: $newVal }
]
}
) {
data {
id
attributes {
updatedAt
contentSections {
... on ComponentSectionsRichText {
id
content
__typename
}
}
}
}
}
}