Nextjs+GraphCMS项目创建评论时如何自动设置为PUBLISHED阶段?
解决方案
你当前使用的createComment mutation默认仅会创建草稿状态的评论,因此需要手动在后台调整为发布状态。要实现创建时自动发布,仅需要调整你的mutation逻辑即可:
方案1:创建时直接发布(推荐)
给createComment添加publish: true参数,无需修改其他逻辑,修改后的代码如下:
const query = gql` mutation CreateComment($name: String!, $email: String!, $comment: String!, $slug: String!) { createComment( data: {name : $name, email : $email, comment: $comment, post: { connect: { slug: $slug}}} publish: true ) { id } } `
请提前确认你使用的GRAPHCMS_TOKEN对应的永久访问令牌,已经开启了评论模型的创建、发布权限,否则会触发权限报错。
方案2:创建+发布合并执行
如果你使用的GraphCMS版本不支持上述publish参数,可以将两个操作合并到同一个mutation中执行:
const query = gql` mutation CreateComment($name: String!, $email: String!, $comment: String!, $slug: String!) { createComment(data: {name : $name, email : $email, comment: $comment, post: { connect: { slug: $slug}}}) { id } publishComment(where: { id: $createComment.id }) { id } } `
内容的提问来源于stack exchange,提问作者Initial D
相关产品推荐
相关产品推荐

