简体   繁体   English

GraphQL 嵌套文档在突变时返回 null

[英]GraphQL nested document returns null on mutation

I am using MongoDB with Mongoose and GraphQL for a class project.我将 MongoDB 与 Mongoose 和 GraphQL 一起用于 class 项目。 I am stuck on an issue with GraphQL returning null on fields within a nested document reference (postedBy which references the User schema).我遇到了一个问题,GraphQL 在嵌套文档引用(引用用户模式的发布者)中的字段上返回 null。 I expect the fields to be populated by the referenced object data, but only the ID returns.我希望这些字段由引用的 object 数据填充,但仅返回 ID。

Model Model

const postSchema = new Schema(
    {
        postText: {
            type: String,
            required: 'You need add text to your post',
            minlength: 1,
            maxlength: 10000,
        },
        createdAt:{
            type: Date,
            default: Date.now,
            get: createdAtVal => dateFormat(createdAtVal)
        },
        postedBy: {
            type: Schema.Types.ObjectId, ref: "User",
            required: true
        },
        comments: [commentSchema]
    },
    {
        toJSON: {
          virtuals: true,
          getters: true
        },
    }
)

postSchema.virtual('commentCount').get(function() {
    return this.comments.length;
  });

const Post = model('Post', postSchema);
module.exports = Post;

TypeDef类型定义

type Post {
    _id: ID
    postText: String
    createdAt: String
    postedBy: User
    comments: [Comment]
    commentCount: Int
}

Resolver解析器

addPost: async (parent, args, context) => {
            if (context.user) {
              const post = await Post.create({ ...args, postedBy: context.user._id });
              
              await User.findByIdAndUpdate(
                { _id: context.user._id },
                { $push: { posts: post._id } },
                { new: true }
              );

              return post;
            }
          
            throw new AuthenticationError('You need to be logged in!');
          }

I am able to successfully query the post and have the referenced field populated with the user's _id, username, and image(url).我能够成功查询帖子并使用用户的_id、用户名和图像(url)填充引用的字段。 When I run the mutation, the username and image return null.当我运行突变时,用户名和图像返回 null。

Here is my mutation:这是我的突变:

mutation addPost($PostText: String!) {
    addPost(postText: $postText) {
      _id
      postText
      createdAt
      postedBy {
        _id
        username
        image
      }
      commentCount
      comments {
        _id
      }
    }
  }

And here is the response it gets:这是它得到的响应:

{
  "data": {
    "addPost": {
      "_id": "60612871bd89e52ca08d3ea1",
      "postText": "This is an example of a post.",
      "createdAt": "Mar 28th, 2021 at 21:08 pm",
      "postedBy": {
        "_id": "6060a868d856f01738f45185",
        "username": null,
        "image": null
      },
      "commentCount": 0,
      "comments": []
    }
  }
}

What am I doing wrong?我究竟做错了什么?

Since you're only getting the ref and no additional data, I think you just forgot to populate the user field.由于您只获得 ref 而没有其他数据,我认为您只是忘记填充用户字段。

Try:尝试:

return await post.populate('postedBy').execPopulate();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM