import { inputObjectType, mutationField, arg, FieldResolver } from '@nexus/schema'; import { ApolloError, AuthenticationError } from 'apollo-server-micro'; import { authenticateQuery } from '../../../schema/authenticate-query'; import { RecipeCollection } from '../collection-model'; export const DeleteRecipeCollectionIputType = inputObjectType({ name: 'DeleteRecipeCollectionInput', definition(t) { t.id('id', { description: 'Array of Recipe IDs' }); }, }); /** * Delete a new RecipeCollection */ export const deleteRecipeCollection: FieldResolver< 'Mutation', 'deleteRecipeCollection' > = async (root, args, ctx, info) => { // Auth authenticateQuery(ctx); const { id } = args.input; // Execute const deletedCollection = await RecipeCollection.findByIdAndDelete(id) .accessibleBy(ctx.state.abilities, 'delete') .populate('nodes.recipe') .catch(err => { throw new ApolloError('Unknown Error'); }); if (deletedCollection === null) { throw new AuthenticationError('Unable to Delete RecpeCollection'); } return deletedCollection; }; /** * Delete RecipeCollection Mutation */ export const deleteRecipeCollectionMF = mutationField( 'deleteRecipeCollection', { type: 'RecipeCollection', args: { input: arg({ type: DeleteRecipeCollectionIputType, }), }, resolve: deleteRecipeCollection, } );