【问题标题】:Node.js Mongodb GraphQL - mutations and queryNode.js Mongodb GraphQL - 突变和查询
【发布时间】:2021-09-09 16:22:15
【问题描述】:

我的代码的某些部分有问题:

const BookType = new GraphQLObjectType({
    name: "Book",
    fields: () => ({
        id: {type: GraphQLID},
        title: {type: GraphQLString},
        author: {type: GraphQLString},
    })
})


const fQuery = new GraphQLObjectType({
    name: "firstQuery",
    fields: {
        books: {
            type: new GraphQLList(BookType),
            resolve(parent, args){
                return Book.find({});
            }
        },
        book: {
            type: BookType,
            args: {id: {type: GraphQLID}},
            resolve(parent, args){
                return Book.findById(args.id);
            }
        },
        author: {
            type: BookType,
            args: {author: {type: GraphQLString}},
            resolve(parent, args){
                return ??????????
            }
        },
    }
})

我不知道如何按作者查找书籍。 接下来 - 突变:

const Mutation = new GraphQLObjectType({
    name: "Mutation",
    fields: {
        add: {
            type: BookType,
            args: {
                title: {type: GraphQLString},
                author: {type: GraphQLString},
            },
            resolve(parent,args){
                let book = new Book({
                    title:args.title,
                    author:args.author,
                })
                return book.save()
            }
        },
        update: {
            type: BookType,
            args: {
                title: {type: GraphQLString},
                author: {type: GraphQLString},
            },
            resolve(parent, args){
                return Book.findByIdAndUpdate(args.id, {
                    title: args.title,
                    author: args.author
                })
            }
        },
        del: {
            type: BookType,
            args: {
                id: {type: GraphQLID},
            },
            resolve(parent,args){
                return Book.findByIdAndDelete(args.id)
            }
        }
    }
});

更新不起作用。删除删除第一个项目,而不是按 ID 选择的项目。这是我的家庭作业。我已经坐了几个小时了,似乎无法正确处理。 你们中有人知道如何解决这个问题吗? 提前致谢!

更新:按作者搜索不起作用。我能够解决其余的问题。

【问题讨论】:

    标签: node.js mongodb graphql pug


    【解决方案1】:

    您还需要一个作者 GraphQLObjectType,如果您将作者的 id 存储在您的书籍中,您可以在作者中添加一个新字段

    编辑:您也可以尝试按名称查找(但它必须是唯一的,否则您将得到冲突的结果)

      booksByAuthor: {
          type: GraphQLList(BookType),
          async resolve(parent, args) {
            return Books.find( { id:parent.id } )
    
          },
    }
    

    所以会是这样的

    const AuthorType = new GraphQLObjectType({
      name: 'Author',
      fields: () => ({
        id: { type: GraphQLID },
        name: { type: GraphQLString },
        booksByAuthor: {
          type: GraphQLList(BookType),
          async resolve(parent, args) {
            return Books.find({ id: parent.id });
          },
        },
      }),
    });
    

    我没有将 id 视为您的突变中的参数。您需要传递 id。

       update: {
                type: BookType,
                args: {
                    id:{type: GraphQLID}
                    title: {type: GraphQLString},
                    author: {type: GraphQLString},
                },
                resolve(parent, args){
                    return Book.findByIdAndUpdate(args.id, {
                        title: args.title,
                        author: args.author
                    })
                }
            },
    

    我也是新手,希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2017-08-24
      • 2020-11-04
      • 2016-01-14
      • 2018-09-17
      • 2022-01-13
      • 2018-11-10
      • 2017-09-23
      • 2019-11-18
      • 2017-10-18
      相关资源
      最近更新 更多