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 UpdateRecipeCollectionIputType = inputObjectType({ name: 'UpdateRecipeCollectionInput', definition(t) { t.id('id', { description: 'Array of Recipe IDs', required: true }); t.string('name', { description: 'Name of the collection' }); }, }); /** * Update a Recipe Collection */ export const updateRecipeCollection: FieldResolver< 'Mutation', 'updateRecipeCollection' > = async (root, args, ctx, info) => { const { id, ...collectionProps } = args.input; const collection = await RecipeCollection.findById(id) .accessibleBy(ctx.state.abilities, 'update') .populate('nodes.recipe'); // Auth authenticateQuery(ctx); if (collection == null) { throw new AuthenticationError('You do not have RecipeCollection'); } // Execute const updatedCollection = collection.set(collectionProps); await updatedCollection.save(); return updatedCollection; }; /** * Delete RecipeCollection Mutation */ export const updateRecipeCollectionMF = mutationField( 'updateRecipeCollection', { type: 'RecipeCollection', args: { input: arg({ type: UpdateRecipeCollectionIputType, }), }, resolve: updateRecipeCollection, } );