import { inputObjectType, mutationField, arg } from '@nexus/schema'; import { Error, Types } from 'mongoose'; import { ApolloError, UserInputError, ForbiddenError, } from 'apollo-server-micro'; import { authenticateQuery } from '../../../schema/authenticate-query'; import { Recipe } from '../recipe-model'; export const UpdateRecipeIputType = inputObjectType({ name: 'UpdateRecipeInput', definition(t) { t.id('id', { required: true }); t.string('title'); t.string('instructions'); }, }); /** * Fetch a single Recipe */ export const updateRecipeMF = mutationField('updateRecipe', { type: 'Recipe', args: { input: arg({ type: UpdateRecipeIputType, description: 'Recipe Details' }), }, resolve: async (root, args, ctx, info) => { // Auth authenticateQuery(ctx); // Resolve const recipe = await Recipe.findByIdAndUpdate(args.input.id, args.input, { runValidators: true, new: true, }) .accessibleBy(ctx.state.abilities, 'update') .catch(err => { if (err instanceof Error.CastError) { throw new UserInputError('Invalid Properties'); } throw new ApolloError('Unknown Error'); }); if (recipe === null) { throw new ForbiddenError('You do not have access to update this recipe'); } return recipe; }, });