import { inputObjectType, mutationField, arg, idArg } from '@nexus/schema'; import { ForbiddenError } from 'apollo-server-micro'; import { authenticateQuery } from '../../../schema/authenticate-query'; import { Recipe } from '../../../app/recipe'; export const AddIngredientGroupInputType = inputObjectType({ name: 'AddIngredientGroupInput', definition(t) { t.id('recipeId', { required: true }); t.int('order'); t.string('heading'); }, }); /** * Add an IngredientGroup to a Recipe */ export const addIngredienGrouptMF = mutationField('addIngredientGroup', { type: 'IngredientGroup', args: { input: arg({ type: AddIngredientGroupInputType, }), }, resolve: async (root, args, ctx, info) => { const { abilities } = authenticateQuery(ctx); const { order, recipeId, ...props } = 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 ingredientGroup = recipe.ingredientList.addGroup(props, { order, }); await recipe.save(); return ingredientGroup; }, });