I have the following code that runs a mutation to update a post to "published" the mutation works great! It updates the data as expected. However, the data property is always undefined in the useMutation hook. This is odd because I can see the response has data in it in the network tab. I'm quite stumped on this issue. Help would be appreciated. Here is the react code:
import { gql, useMutation } from "@apollo/client";
import React from "react";
import { Spinner } from "react-bootstrap";
import "./Post.css";
const PUBLISH_POST = gql`
mutation PublishPost($id: ID!) {
postPublish(id: $id) {
userErrors {
message
}
post {
title
}
}
}
`;
const UNPUBLISH_POST = gql`
mutation UnPublishPost($id: ID!) {
postUnpublish(id: $id) {
userErrors {
message
}
post {
title
}
}
}
`;
export default function Post({
title,
content,
date,
user,
published,
id,
isMyProfile
}) {
const [publishPost, { data, loading, error }] = useMutation(PUBLISH_POST);
console.log("data", data);
const [UnpublishPost, { data: unpublishData, loading: unpublishLoading }] = useMutation(UNPUBLISH_POST);
const formatedDate = new Date(Number(date)).toDateString();
if (loading || unpublishLoading) {
return <Spinner animation="border" />;
}
if (data?.userErrors?.length) {
return (
<div>
{data.userErrors.map(e => {
return <p>{e?.message}</p>;
})}
</div>
);
}
if (unpublishData?.userErrors?.length) {
return (
<div>
{unpublishData.userErrors.map(e => {
return <p>{e?.message}</p>;
})}
</div>
);
}
return (
<div
className="Post"
style={published === false ? { backgroundColor: "hotpink" } : {}}
>
<div>ID: {id}</div>
{isMyProfile && published === false && (
<p
className="Post__publish"
onClick={() => {
publishPost({
variables: {
id
}
});
}}
>
publish
</p>
)}
{isMyProfile && published === true && (
<p
className="Post__publish"
onClick={() => {
UnpublishPost({
variables: {
id
}
});
}}
>
unpublish
</p>
)}
<div className="Post__header-container">
<h2>{title}</h2>
<h4>
Created At {formatedDate} by {user}
</h4>
</div>
<p>{content}</p>
</div>
);
}
And here is the graphql code that runs on the server (I doubt this section is the problem but you never know)
postPublish: async (
_: any,
{ id }: { id: string },
{ prisma, userInfo }: Context
): Promise<PostPayloadType> => {
const payLoad = new PayLoad();
if (!userInfo) {
payLoad.addError("you must be logged in");
return payLoad;
}
const error = await canUserMutatePost(userInfo.userId, Number(id), prisma);
if (error.userErrors.length) {
return error;
}
payLoad.post = await prisma.post.update({
where: {
id: Number(id)
},
data: {
published: true
}
});
return payLoad;
}
also, I've noticed if I await publishPost I do eventually get data however I think there's a problem in the setup as I should just be able to use useMutation as above.
<Post>
to be re-mounted when (un)published? – Sarong{ data: unpublishData, loading: unpublishLoading, error: unpublishError }
– Spiros