【问题标题】:How to Save Relation In MongoDB with GraphQL args如何使用 GraphQL args 在 MongoDB 中保存关系
【发布时间】:2018-07-07 15:23:19
【问题描述】:

我不完全了解 MongoDB 关系的工作方式,但我相信专辑与艺术家的关系是 艺术家(我称他们为用户)在数组,可能称为albums: [],所有专辑都有一个createdBy: {id: 12345}

所以我想创建一个 updateAlbum 调用,但我不知道如何从 GraphQL 将 ID 保存到 createdBy:

这是架构:

var AlbumSchema = new Schema({
    name: String,
    dateCreated: { type: Date, default: Date.now },
    dateUpdated: { type: Date, default: Date.now },
    users: [{ type: Schema.Types.ObjectId, ref: 'User' }],
    createdBy: { type: Schema.Types.ObjectId, ref: 'User'}
});
var UserSchema = new Schema({
    fName: String,
    lName: String,
    albums: [{ type: Schema.Types.ObjectId, ref: 'Album'}]
});

这里是 GraphQL 类型:

const UserType = new GraphQLObjectType({
    name: 'User',
    fields: () => ({
        id: {type: GraphQLID},
        fName: {type: GraphQLString},
        lName: {type: GraphQLString},
        albums: {type: new GraphQLList(AlbumType)}
    })
})
const AlbumType = new GraphQLObjectType({
    name: 'Album',
    fields: () => ({
        id: {type:GraphQLID},
        name: {type:GraphQLString},
        dateCreated: {type: GraphQLString},
        dateUpdated: {type: GraphQLString},
        users: {type: new GraphQLList(UserType)},
        createdBy: {type: UserType}
    })
})

这里是 updateAlbum 调用:

updateAlbum: {
            type: AlbumType,
            args: {
                id: {type: new GraphQLNonNull(GraphQLString)},
                name: {type: GraphQLString},
                createdById: {type: GraphQLString}
            },
            resolve(parentValue, args){
                return new Promise((resolve, reject) => {
                    console.log(args.createdById)
                    const date = Date().toString()
                    ALBUM.findOneAndUpdate(
                        {"_id": args.id},
                        { "$set":{name: args.name,
                                dateUpdated: date,
                                createdBy: args.createdById}},
                        {"new": true} //returns new document
                    ).exec((err, res) => {
                        if(err) reject(err)
                        else resolve(res)
                    })
                })
            }
        }

如何在 updateAlbum 函数中保存关系?

这就是我得到的 - 如果我保存 createdBy: THE_ID_STRING 它不会正确执行,当我查询时我得到一个奇怪的未知字符字符串“Zj�����\b�\u001e�(” ID 字符串 console.log() 为 "5a6ac1afac97c908dc1edf28"

【问题讨论】:

    标签: node.js mongodb mongoose graphql


    【解决方案1】:

    根据您的 Mongo Schema 定义,我想说您对引用的理解是正确的,看看您如何通过使用 Schema.Types.ObjectId 正确使用文档引用。

    现在您只需将GraphQLID 映射为createdBy 的GraphQL type
    我喜欢为此使用别名,例如MongoId,原因有两个。

    1) 因此,我仅通过查看 GraphQL 类型定义就可以明确知道如果我忘记了我将其分配为什么数据类型,则无需返回 Mongo Schema 会发生什么行为:@987654326 @ || String.

    2) 最重要的是,为了在视觉上强调标准 String 类型与 Object 类型之间的重要区别,因为 Mongo 使用 Object.prototype 作为文档索引 _ids 即使如此它们在视觉上看起来像是来自String.prototype

    如:

    import {
      GraphQLList,
      GraphQLID as MongoId,
      GraphQLString as StringType,
      GraphQLObjectType as ObjectType,
    } from 'graphql';
    
    const AlbumType = new ObjectType({
        name: 'Album',
        fields: () => ({
            id: { type: MongoId },
            name: { type: StringType },
            dateCreated: { type: StringType },
            dateUpdated:  {type: StringType },
            users: { type: new GraphQLList(UserType)},
            createdBy: { type: MongoId}
        })
    })
    

    顺便说一句,您应该验证您的 id 不应该是 _id

    如果您需要进一步说明,可以参考我刚刚完成的一个项目,涉及这个问题。 GraphQL 类型为here

    【讨论】:

    • @KevinDanikowski 太棒了。很高兴我能帮上忙!
    猜你喜欢
    • 2020-03-13
    • 2018-01-28
    • 2019-03-26
    • 1970-01-01
    • 2020-03-07
    • 2019-05-24
    • 2019-05-12
    • 2011-07-07
    • 1970-01-01
    相关资源
    最近更新 更多