import { inputObjectType, mutationField, arg } from '@nexus/schema'; import { ForbiddenError, UserInputError } from 'apollo-server-micro'; import { authenticateQuery } from '../../../schema/authenticate-query'; import { IngredientGroupType } from '.'; import { Recipe } from '../../recipe'; export const RemoveIngredientGroupInputType = inputObjectType({ name: 'RemoveIngredientGroupInput', definition(t) { t.id('recipeId', { required: true }); t.id('groupId', { required: true }); }, }); /** * Remove an IngredientGroup */ export const removeIngredientGroupMF = mutationField('removeIngredientGroup', { type: 'IngredientGroup', args: { input: arg({ type: RemoveIngredientGroupInputType, }), }, resolve: async (root, args, ctx, info) => { const { abilities } = authenticateQuery(ctx); const { recipeId, groupId } = 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) { throw new UserInputError('IngredientGroup Not Found'); } const ingredientGroup = recipe.ingredientList.removeGroup(groupId); await recipe.save(); return ingredientGroup; }, });