Mutating data with Sequelize
Requesting data from our database via the GraphQL API works. Now comes the tough part: adding a new post to the Posts
table.
Before we start, we must extract the new database model from the db
object at the top of the exported function in our resolvers.js
file:
const { Post, User } = db.models;
Currently, we have no authentication to identify the user that is creating the post. We will fake this step until the authentication is implemented Chapter 6, Authentication with Apollo and React.
We have to edit the GraphQL resolvers to add the new post. Replace the old addPost
function with the new one, as shown in the following code snippet:
addPost(root, { post }, context) { return User.findAll().then((users) => { const usersRow = users[0]; return Post.create({ ...post, }).then((newPost) => { &...