【问题标题】:What should you check when graphQL's query return's empty data from mongodb?当graphQL的查询从mongodb返回空数据时,你应该检查什么?
【发布时间】:2021-06-18 01:11:17
【问题描述】:

我正在尝试将 Flask api 转换为 graphql 服务器。我可以让示例代码与测试 mongodb 集合一起使用,但我无法让我改编的代码在真正的 mongodb 服务器上工作。查询始终返回空数据响应。调试此代码的步骤是什么?

const Express = require("express");
const { graphqlHTTP } = require('express-graphql');
const Mongoose = require("mongoose");
const {
    GraphQLID,
    GraphQLString,
    GraphQLList,
    GraphQLNonNull,
    GraphQLObjectType,
    GraphQLSchema
} = require("graphql");

var app = Express();

Mongoose.connect("mongodb://localhost/treasure-chess");

//"persons" is the collection 
const GameModel = Mongoose.model("game", {
    black: String,
    white: String
});

const GameType = new GraphQLObjectType({
    //the name field here doesn't matter I guess...
    name: "Game",
    fields: {
        id: { type: GraphQLID },
        black: { type: GraphQLString },
        white: { type: GraphQLString }
    }
});

const schema = new GraphQLSchema({
    query: new GraphQLObjectType({
        name: "Query",
        fields: {
            games: {
                type: GraphQLList(GameType),
                resolve: (root, args, context, info) => {
                    return GameModel.find().exec();
                }
            },
            game: {
                type: GameType,
                args: {
                    id: { type: GraphQLNonNull(GraphQLID) }
                },
                resolve: (root, args, context, info) => {
                    return GameModel.findById(args.id).exec();
                }
            }
        }
    }),
    mutation: new GraphQLObjectType({
        name: "Mutation",
        fields: {
            game: {
                type: GameType,
                args: {
                    firstname: { type: GraphQLNonNull(GraphQLString) },
                    lastname: { type: GraphQLNonNull(GraphQLString) }
                },
                resolve: (root, args, context, info) => {
                    var game = new GameModel(args);
                    return game.save();
                }
            }
        }
    })
});

app.use("/graphql", graphqlHTTP({
    schema: schema,
    graphiql: true
}));


app.listen(3000, () => {
    console.log("Listening at :3000...");
});

证明我正在连接到正确的 mongodb 文档:

更多证据:

我确实尝试了评论建议,结果发现结果是空的……:

                    var results = GameModel.find().exec()
                    results.then(game_info =>{
                        console.log(game_info)
                    })

【问题讨论】:

  • 你应该检查数据库响应...console.log( GameModel.find().exec() );
  • 您好,谢谢您的建议。似乎 db 响应为空?

标签: node.js mongodb api graphql


【解决方案1】:

我在 stackoverflow 上尝试了一堆不同的搜索后找到了答案。事实证明, mongoose.model 假定您传递的任何参数都是单数的,并且默认情况下是复数形式。所以我实际上不得不通过:

const GameModel = Mongoose.model("game", gameSchema, "game");

当然,大多数人可能不会遇到此错误,因为他们可能会记住这一点来命名他们的收藏,可能会有一个奇怪的人想要为他们的收藏使用不同的名称,或者收藏的复数是相同的单数。我会把它留给其他人,祝你编码愉快!

【讨论】:

    猜你喜欢
    • 2017-10-31
    • 2017-11-19
    • 2022-12-29
    • 2020-10-01
    • 2018-08-02
    • 2020-03-01
    相关资源
    最近更新 更多