GraphQL查询返回null

丹尼尔

我运行了这个成功的突变,并将其保存到我的MongoDB数据库中:

mutation {
  addRecipe(name: "Chole Dhania Masala", category: "Indian", description: "A tasty dish", instructions: "Cook it") {
    name
    category
    description
    instructions
  }
}

但是,当我对其进行查询时:

query {
  getAllRecipes {
    _id
    name
    category
    likes
    createdDate
  }
}

我不清楚为什么会得到:

{
  "data": {
    "getAllRecipes": null
  }
}

这是我的resolvers.js文件:

exports.resolvers = {
    Query: {
        getAllRecipes: async (root, args, { Recipe }) => {
            const allRecipes = await Recipe.find();
            return allRecipes;
        }
    },
    Mutation: {
        addRecipe: async (root, { name, description, category, instructions, username }, { Recipe }) => {
            const newRecipe = await new Recipe({
                name,
                description,
                category,
                instructions,
                username
            }).save();
            return newRecipe;
        }
    }
};
叶戈尔·扎伦巴(Yegor zaremba)

你有recipes吗?

Query: {
    getAllRecipes: async (root, args, { Recipe }) => {
        const allRecipes = await Recipe.find();
        if (allRecipes) {
          return allRecipes;
        }

        return null;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章