import { ApolloError, UserInputError, ForbiddenError, } from 'apollo-server-micro'; import { Error } from 'mongoose'; import { inputObjectType, mutationField, arg } from '@nexus/schema'; import { User } from '../user-model'; import { authenticateQuery } from '../../../schema/authenticate-query'; import { UserType } from './index'; export const UpdateUserInputType = inputObjectType({ name: 'UpdateUserInput', definition(t) { t.id('id', { required: true }); t.string('email'); t.string('firstName'); t.string('lastName'); t.string('password'); }, }); /** * Resolver for updating a new Recipe */ export const updateUserMF = mutationField('updateUser', { type: 'User', args: { input: arg({ type: UpdateUserInputType, description: 'User Details' }), }, resolve: async (root, args, ctx) => { authenticateQuery(ctx); const user = await User.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 (user === null) throw new ForbiddenError('Invalid Recipe ID'); return user; }, });