【问题标题】:GraphQL types: graphqlid or string for referenced objects?GraphQL 类型:被引用对象的 graphqlid 或字符串?
【发布时间】:2018-06-29 02:48:03
【问题描述】:

在我的 GraphQL 应用程序中,有一个参考其他 mongo/mongoose 对象的 mongo 集合:

export const SingerType = new GraphQLObjectType({
    name: "Singer",
    description: "A band´s singer",
    fields: () => ({
        id: {
            type: new GraphQLNonNull(GraphQLID),
        },
        name: {
            type: new GraphQLNonNull(GraphQLString)
        },
        band: {
            type: new GraphQLNonNull(BandType),
            resolve: (source, args, context) => {
                return Band.findOne({
                    _id: source.band_id
                }).exec();
            }
        }
        albuns: {
            type: new GraphQLList(AlbumType),
            resolve: (source, args, context) => {
                return Album.find({
                    _id: source._id
                }).exec();
            }
        }
    })
});

export const SingerInputType = new GraphQLInputObjectType({
    name: "SingerInput",
    description: "Band´s singer input type",
    fields: () => ({
        name: {
            type: new GraphQLNonNull(GraphQLString)
        },
        band_id: {
            type: new GraphQLNonNull(GraphQLString) <<== GraphQLString or GraphQLID here ???
        },
        albuns: {
            type: new GraphQLList(GraphQLString) <<== GraphQLString or GraphQLID here ???
        }       }
    })
});

我的疑问是:

我应该使用GraphQLStringGraphQLID 来引用其他对象 - 单独或在数组中 - 如示例代码中所述?

【问题讨论】:

    标签: mongodb facebook-graph-api mongoose graphql graphql-js


    【解决方案1】:

    如果你使用 GraphQLString 或 GraphQLID 单独引用其他对象,mongodb 中的 JOINS 并不便宜,它会导致查询非常慢,因为它单独查询集合并聚合然后过滤结果。这个概念是不可扩展的。像关系数据库这样 JOIN 它很便宜。

    1. 我的看法是,如果将存储在 band 和 albuns 中的数据不大,则使用数组选项可以更快地检索结果。

    2. 1234563 .

    查看更多 https://docs.mongodb.com/manual/core/data-model-design/

    【讨论】:

    • 我根据我对问题的理解做出了回应。可能你尝试在你的问题中更明确
    猜你喜欢
    • 1970-01-01
    • 2013-09-02
    • 2019-04-10
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 2020-05-29
    相关资源
    最近更新 更多