import { inputObjectType, mutationField, arg } from '@nexus/schema'; import { authenticateQuery } from '../../../schema/authenticate-query'; import { Recipe } from '../recipe-model'; import { RecipeType } from '.'; import { ForbiddenError } from '@casl/ability'; export const CreateRecipeIputType = inputObjectType({ name: 'CreateRecipeInput', definition(t) { t.string('title'); t.string('instructions'); }, }); /** * Fetch a single Recipe */ export const createRecipeMF = mutationField('createRecipe', { type: 'Recipe', args: { input: arg({ type: CreateRecipeIputType, description: 'Recipe Details' }), }, resolve: async (root, args, ctx, info) => { // Auth authenticateQuery(ctx); const { ...recipeProps } = args.input; if (ctx.state.abilities.cannot('create', Recipe)) { throw new ForbiddenError('You dont have permission to create recipes'); } // create recipe const recipe = await Recipe.create({ ...recipeProps, ownerId: ctx.state.user.id, }); return recipe; }, });