【问题标题】:Post validation failed: title: Path `title` is required.", in graphql验证后失败:标题:需要路径“标题”。”,在 graphql
【发布时间】:2019-12-27 10:41:18
【问题描述】:

我试图在 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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-09-11
      • 2020-08-20
      • 2022-12-14
      • 1970-01-01
      • 2020-06-10
      • 2022-01-17
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      相关资源
      最近更新 更多