Запрос GraphQL, возвращающий ноль - PullRequest
0 голосов
/ 21 января 2019

Я запустил эту успешную мутацию, которая была сохранена в моей базе данных 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 Ответ

0 голосов
/ 21 января 2019

Был ли у вас recipes?

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

        return null;
    }
}
...