0

如何使用 Graphql 在 svelte 中提交评论?
我的苗条代码是这样的


<script>
    let commenterMessage = '';
    export let status = false;

    export async function submitComment() {
        $: commenterMessage;
        const graphcms = new GraphQLClient(import.meta.env.VITE_GRAPHCMS_URL, {
            headers: {}
        });

        const query = gql`
            mutation CREATE_COMMENT {
                createComment(
                    input: {
                        commentOn: 115
                        content: ${commenterMessage}
                        author: "Sarah Jason"
                    }
                ) {
                    success
                    comment {
                        id
                        content
                        author {
                            node {
                                name
                            }
                        }
                    }
                }
            }
        `;

        const { createComment } = await graphcms.request(query);
        return {
            props: {
                status: createComment.success
            }
        };
    }
</script>

这是我使用来自用户的绑定值的形式:

<form id="comment_form" method="post">
    <div class="group-val ct-gr">
        <textarea name="message" placeholder="Comment" bind:value={commenterMessage} />
    </div>
    <a
        href="#"
        class="btn fill"
        on:click|preventDefault={submitComment}
        data-text="Add Comment">
        Add Comment
    </a>
</form>

${commenterMessage}在 gql 查询中有错误。
这是我在控制台中看到的错误:

您不能在同一类型上注册重复字段。“类别”字段已存在于“RootQuery”类型上。确保给该字段一个唯一的名称。","field_name":"categories","type_name":"RootQuery","stack":["E:\\wamp64\\www\\simplecv\\cms\\ wp-content\\plugins\\wp-graphql\\src\\Registry\\TypeRegistry.php:1025","E:\\wamp64\\www\\simplecv\\cms\\wp-includes\\class- wp-hook.php:305","E:\\wamp64\\www\\simplecv\\cms\\wp-includes\\plugin.php:189","E:\\wamp64\\www\\simplecv \\cms\\wp-content\\plugins\\wp-graphql\\src\\Type\\WPObjectType.php:226","E:\\wamp64\\www\\simplecv\\cms\\wp-内容\\插件\\wp-graphql\\src\\Type\\WPObjectType.php:126","E: \\wamp64\\www\\simplecv\\cms\\wp-includes\\class-wp.php:750","E:\\wamp64\\www\\simplecv\\cms\\wp-includes\\函数.php:1291","E:\\wamp64\\www\\simplecv\\cms\\wp-blog-header.php:16","E:\\wamp64\\www\\simplecv\\cms \\index.php:17"]},{"type":"DUPLICATE_FIELD","message":"您不能在同一类型上注册重复字段。“RootQuery”类型上已经存在“主题”字段。确保给该字段一个唯一的名称。","field_name":"themes","type_name":"RootQuery","stack":["E:\\wamp64\\www\\simplecv\\cms\\ wp-content\\plugins\\wp-graphql\\src\\Registry\\TypeRegistry.php:1025","E:\\wamp64\\www\\simplecv\\cms\\wp-includes\\class- wp-hook.php:305","E: \\wamp64\\www\\simplecv\\cms\\wp-includes\\class-wp-hook.php:327","E:\\wamp64\\www\\simplecv\\cms\\wp-includes \\plugin.php:518","E:\\wamp64\\www\\simplecv\\cms\\wp-includes\\class-wp.php:388","E:\\wamp64\\www\ \simplecv\\cms\\wp-includes\\class-wp.php:750","E:\\wamp64\\www\\simplecv\\cms\\wp-includes\\functions.php:1291", "E:\\wamp64\\www\\simplecv\\cms\\wp-blog-header.php:16","E:\\wamp64\\www\\simplecv\\cms\\index.php:17 "]}]},"status":200,"headers":{"map":{"content-length":"7395","content-type":"application/json; charset=UTF-8"}}},"request":{"query":"\n\t\t\tmutation CREATE_COMMENT {\n\t\t\t\tcreateComment(\n\t\t\t\ t\tinput: {\n\t\t\t\t\t\tcommentOn: 115\n\t\t\t\t\t\tcontent: 你好。

如何使用用户在表单输入中键入的值?

4

1 回答 1

0

问题是content: ${commenterMessage}我应该这样写:

content: "${commenterMessage}"

由于内容是一个字符串,我们应该用双引号括起来。

于 2021-08-11T12:17:38.227 回答