首页 > 解决方案 > 验证后失败:标题:需要路径“标题”。”,在 graphql

问题描述

我试图在 graphql-yoga 中添加突变,但每次我尝试突变时,我都会收到错误消息

验证后失败:标题:title需要路径。",

我不知道为什么。

这是我的代码

解析器

Mutation: {
        createPost: async(root, args, ctx) => {

            console.log(args)

            try {

                const post = new Post({
                    title: args.title,
                    description: args.description,
                    content: args.content
                });
                const result = await post.save();
                console.log(result);
                return result
            }catch(err) {
                throw err
            }

        }
    }

图式

input postInput{
        title: String!
        description: String!
        content: String!
    }

    type Mutation {
        createPost(input: postInput): Post!
    }

如果我删除输入类型并直接这样做,这很好用

type Mutation {
        createPost(title: String!,description: String!,content: String!): Post!
    }

记录结果

{ input:
   [Object: null prototype] {
     title: 'with input',
     description: 'this is de',
     content: 'this is conte' } }

为什么我会在这里[Object: null prototype]

标签: graphql

解决方案


如果您在架构上提供这样的输入类型,则必须像这样在解析器中发送数据:

const post = new Post({
  title: args.input.title,
  description: args.input.description,
  content: args.input.content
});

这意味着,在 args 中,我们需要一个名为input的参数,它的类型为Post

在 graphql gui 上提供数据时,发送数据如下:

mutation {
  createPost(input:{
     title: 'with input',
     description: 'this is de',
     content: 'this is conte'}) {
   //return your id or others
  }
}


推荐阅读