import { inputObjectType, mutationField, arg } from '@nexus/schema'; import { ApolloError, UserInputError, ForbiddenError, } from 'apollo-server-micro'; import { Error, Types } from 'mongoose'; import { authenticateQuery } from '../../../schema/authenticate-query'; import { Recipe } from '../recipe-model'; import { RecipeType } from '.'; export const DeleteRecipeIputType = inputObjectType({ name: 'DeleteRecipeInput', definition(t) { t.id('id', { required: true }); }, }); /** * Fetch a single Recipe */ export const deleteRecipeMF = mutationField('deleteRecipe', { type: 'Recipe', args: { input: arg({ type: DeleteRecipeIputType, description: 'Recipe Details' }), }, resolve: async (root, args, ctx, info) => { // Auth authenticateQuery(ctx); const deletedRecipe = await Recipe.findByIdAndDelete(args.input.id) .accessibleBy(ctx.state.abilities, 'delete') .catch(err => { if (err instanceof Error.CastError) { throw new UserInputError('Invalid Properties'); } throw new ApolloError('Unknown Error'); }); // No recipe was found OR Not permitted if (deletedRecipe === null) { throw new ForbiddenError('Unable to delete recipe'); } return deletedRecipe; }, });