解决 Mongoose 和 GraphQL 中的关系 [英] Solving relationships in Mongoose and GraphQL

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

问题描述

所以,我有一个我正在启动的应用程序,我将使用 Apollo 使用 MERNG 堆栈.

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

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

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 附带的 populate.这有一个缺点,即使我没有在 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.

希望对你有帮助

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆