在 GRAPHQL 上创建评论时将阶段添加到 PUBLISHED

Adding stage to PUBLISHED when created a comment on GRAPHQL

我有一个用 Nextjs、GraphQL 和 GraphCMS 创建的小博客站点,并且创建评论功能已经可以使用,但我必须手动登台以在 CMS 上发布。我想要评论在创建时已经发布到 PUBLISHED。

有变异代码


   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 }
   }
 `

这就是我的全部代码


    import { GraphQLClient, gql } from 'graphql-request';

    const graphqlAPI = process.env.NEXT_PUBLIC_GRAPHCMS_ENDPOINT
    const graphcmsToken = process.env.GRAPHCMS_TOKEN

    export default async function comments(req, res) {
  
    const graphQLClient = new GraphQLClient(graphqlAPI, {
    headers: {
      authorization: `Bearer ${graphcmsToken}`
    }
    })

    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 }
    }
    `
 
      try {
      const result = await graphQLClient.request(query, req.body);

       return res.status(200).send(result);
       } catch (error) {
       console.log(error);
      return res.status(500).send(error);
      }
    }


已编辑

好的,经过一番研究,我发现 GraphCMS 中的每个 model 都有一些预定义的解析器函数。因此,要直接 PUBLISH 您的内容,您需要像这样更新您的突变函数:

  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 : {slug:$slug}) {
            title
        }
    }

    `

如果 slug 不是您的唯一字段,您必须将 publishComment 解析器中的 slug 字段替换为您的唯一字段。

Publishing Content