import { inputObjectType, mutationField, arg, idArg } from '@nexus/schema'; import { ForbiddenError, UserInputError } from 'apollo-server-micro'; import { authenticateQuery } from '../../../schema/authenticate-query'; import { IngredientItemType, IngredientInputType } from '.'; import { Recipe } from '../../../app/recipe/recipe-model'; export const UpdateIngredientInputType = inputObjectType({ name: 'UpdateIngredientInput', definition(t) { t.id('recipeId', { required: true }); t.id('ingredientId', { required: true }); t.int('order'); t.id('groupId'); t.field('ingredient', { type: IngredientInputType }); }, }); /** * Update a single IngredientItem on a recipe */ export const updateIngredientMF = mutationField('updateIngredient', { type: 'IngredientItem', args: { input: arg({ type: UpdateIngredientInputType, description: 'Ingredient Details', }), }, resolve: async (root, args, ctx, info) => { const { abilities } = authenticateQuery(ctx); const { order, groupId, recipeId, ingredientId, ingredient } = 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('Unable to Find Ingredient'); } const ingredientSingle = recipe.ingredientList.updateIngredient( ingredientId, ingredient, { order, groupId, } ); await recipe.save(); return ingredientSingle; }, });