import { inputObjectType, mutationField, arg, idArg } from '@nexus/schema'; import { ForbiddenError, UserInputError } from 'apollo-server-micro'; import { authenticateQuery } from '../../../schema/authenticate-query'; import { IngredientGroupType } from '.'; import { Recipe } from '../../../app/recipe'; export const UpdateIngredientGroupInputType = inputObjectType({ name: 'UpdateIngredientGroupInput', definition(t) { t.id('recipeId', { required: true }); t.id('groupId', { required: true }); t.int('order'); t.string('heading'); }, }); /** * Update an IngredientGroup on a recipe */ export const updateIngredientGroupMF = mutationField('updateIngredientGroup', { type: 'IngredientGroup', args: { input: arg({ type: UpdateIngredientGroupInputType, }), }, resolve: async (root, args, ctx, info) => { const { abilities } = authenticateQuery(ctx); const { order, groupId, recipeId, ...props } = args.input; // find recipe const recipe = await Recipe.findById(recipeId).accessibleBy(abilities); // validate if (recipe == null) { throw new ForbiddenError( 'You do not have access to this recipe, or it does not exist ' ); } if (!recipe.ingredientList) { throw new UserInputError('Invalid ID'); } // udpate const ingredientGroup = recipe.ingredientList.updateGroup(groupId, props, { order, }); await recipe.save(); // TODO: catch errors here return ingredientGroup; }, });