import { ApolloError, UserInputError, ForbiddenError, } from 'apollo-server-micro'; import { Error } from 'mongoose'; import { idArg, mutationField } from '@nexus/schema'; import { authenticateQuery } from '../../../schema/authenticate-query'; import { User } from '../user-model'; import { UserType } from './index'; export const deleteUserMF = mutationField('deleteUser', { type: 'User', args: { id: idArg({ description: 'The ID of the User' }), }, resolve: async (root, args, ctx) => { authenticateQuery(ctx); const deletedRecipe = await User.findByIdAndDelete(args.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; }, });