GraphQL类型:graphqlid或引用对象的字符串?

时间:2022-02-05 02:33:27

In my GraphQL application, there is a mongo collection with reference to other mongo/mongoose objects:

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

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 ???
        }       }
    })
});

My doubt is:

我的疑问是:

Should I use GraphQLString or GraphQLID to reference other objects - individually or in arrays - as commented in the example code?

我应该使用GraphQLString或GraphQLID来引用其他对象 - 单独或在数组中 - 如示例代码中所评论的那样?

1 个解决方案

#1


0  

If you use GraphQLString or GraphQLID to reference other objects individually, JOINS in mongodb is not cheap, it leads to very slow query because it queries the collections individually and aggregate then filter the result. This concept is not scalable. on like Relational Database that JOIN it is cheap.

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

  1. My take is, if the data that will be stored in band and albuns are not large go with array option you can retrieve your result faster.

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

  2. But if the values that will be stored in band and albuns is large and you are designing you app to scale, i will advise you use GraphQLID to reference band and albuns in another collection then use populate to load the content.

    但是如果将存储在band和albuns中的值很大并且您正在设计应用程序以进行扩展,我将建议您使用GraphQLID来引用另一个集合中的band和albuns,然后使用populate来加载内容。

Check out this for more https://docs.mongodb.com/manual/core/data-model-design/

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

#1


0  

If you use GraphQLString or GraphQLID to reference other objects individually, JOINS in mongodb is not cheap, it leads to very slow query because it queries the collections individually and aggregate then filter the result. This concept is not scalable. on like Relational Database that JOIN it is cheap.

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

  1. My take is, if the data that will be stored in band and albuns are not large go with array option you can retrieve your result faster.

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

  2. But if the values that will be stored in band and albuns is large and you are designing you app to scale, i will advise you use GraphQLID to reference band and albuns in another collection then use populate to load the content.

    但是如果将存储在band和albuns中的值很大并且您正在设计应用程序以进行扩展,我将建议您使用GraphQLID来引用另一个集合中的band和albuns,然后使用populate来加载内容。

Check out this for more https://docs.mongodb.com/manual/core/data-model-design/

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