代码之家  ›  专栏  ›  技术社区  ›  Daniel

graphql查询返回空值

  •  1
  • Daniel  · 技术社区  · 6 年前

    我运行了这个成功的突变,它被保存到我的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;
            }
        }
    };
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Yegor Zaremba    6 年前

    你有吗 recipes ?

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