解决猫鼬和GraphQL中的关系

解决猫鼬和GraphQL中的关系

本文介绍了解决猫鼬和GraphQL中的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个正在启动的应用程序,并且正在使用Apollo使用MERNG堆栈.

So, I have this app that I'm starting and I'm going with MERNG stack using Apollo.

我通过阅读博客文章和观看视频进行研究,以便更好地了解GraphQL的工作原理(从未实际使用过).在某个时候,我开始看到数据库模型之间的关系示例,例如来自用户的帖子".

I was doing research by reading blog posts and watching videos in order to have a better understanding of how GraphQL works (never actually worked with it). At some point, I started to see examples of relationships between db models such as "posts from a user".

例如:

    import { model, Schema } from 'mongoose';

    const postSchema = new Schema(
    {
      title: String,
      createdBy {
        type: Schema.Types.ObjectId,
        ref: 'User',
      }
    };

    export default model('Post', postSchema);

    import { model, Schema } from 'mongoose';

    const userSchema = new Schema(
    {
      email: String,
      password: String,
      posts [{
        type: Schema.Types.ObjectId,
        ref: 'Post',
      }]
    }

    export default model('User', userSchema);

我想知道哪种方法是查询查询帖子中用户数据时填充"相关字段的最佳方法.

I was wondering which will be the best approach to "populate" related fields when doing queries to retrieve the user data in the posts.

如我所见,有两个选择:

As I saw there's two options:

1-使用Mongoose随附的填充.这样做的缺点是,即使我在GraphQL查询中不要求它,也总是会填充该字段.2-使用将使用findById检索相关对象然后手动分配它的函数.这没有填充的问题,因为只有在需要该字段时才会填充它,而是增加了我查询数据库的次数.

1 - Using populate which comes with Mongoose. This has the disadvantage that it will always populate the field even if I don't ask for it in the GraphQL query.2 - Using a function that will use a findById to retrieve the related object and then manually assign it. This does not have the issue of the populate as it will only get it when the field is needed, instead it increases the number of times I have to query the database.

我了解优点和缺点,但是我不确定哪一个是最好的解决方案.我还有其他选择吗?

I understand both pros and cons but I'm not sure which would be the best solution for this. Is there any other option that I'm missing?

推荐答案

您的第二个选项对数据库不利,因为如果您从数据库中获得数百万条记录,则当您调用特定字段时,又有几百万条命中项自动起作用.

Your 2nd option was not good for database because if you get millions of record from a database another millions of hit automatically worked when you call the particular field.

您的第一个选项也不符合您的期望.因为您是否需要特定的字段,它会自动被调用.

Your 1st option also not good for your expectation. because you want particular field or not it's automatically invoked.

因此,我希望它是对您更好的解决方案.您可以获取客户端要求的字段(可以使用自定义装饰器获取这些字段).然后,您应该使用聚合来获取所需的内容.然后,您可以根据自己的字段决定是否调用 $ lookup (仅在if条件下使用).

So I hope it's better solution for you. Your can get what the fields asking from client side (you can use custom decorator for get those fields). Then you should use aggregation for get a what you want. Then you decide $lookup is invoke or not with the help of your fields(just use if condition).

注意:解析属性不应访问您的数据库,因为您已经知道原因.

note: Resolve property should not hit your database because the reason was you already know.

我希望它可以为您提供帮助

I hope its may help you

这篇关于解决猫鼬和GraphQL中的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:28