import { inputObjectType, mutationField, arg } from '@nexus/schema'; import { ForbiddenError } from 'apollo-server-micro'; import { authenticateQuery } from '../../../schema/authenticate-query'; import { IngredientItemType } from '.'; import { Recipe } from '../../recipe'; export const RemoveIngredientInputType = inputObjectType({ name: 'RemoveIngredientInput', definition(t) { t.id('recipeId', { required: true }); t.id('ingredientId', { required: true }); }, }); /** * Fetch a single Recipe */ export const removeIngredientMF = mutationField('removeIngredient', { type: 'IngredientItem', args: { input: arg({ type: RemoveIngredientInputType, description: 'Recipe Details', }), }, resolve: async (root, args, ctx, info) => { const { abilities } = authenticateQuery(ctx); const { recipeId, ingredientId } = args.input; // find recipe const recipe = await Recipe.findById(recipeId).accessibleBy(abilities); if (recipe == null) { throw new ForbiddenError( 'You do not have access to this recipe, or it does not exeist ' ); } if (!recipe.ingredientList) { recipe.set('ingredientList', {}); } const ingredient = recipe.ingredientList.removeIngredient(ingredientId); await recipe.save(); return ingredient; }, });