import { idArg, queryField } from '@nexus/schema'; import { ApolloError, UserInputError, ForbiddenError, } from 'apollo-server-micro'; import { Error, Types } from 'mongoose'; import { authenticateQuery } from '../../../schema/authenticate-query'; import { Recipe } from '../recipe-model'; import { RecipeType } from '.'; /** * Fetch a single Recipe */ export const recipeQF = queryField('recipe', { type: 'Recipe', args: { id: idArg({ description: 'id of the post' }) }, resolve: async (root, args, ctx, info) => { // Auth const { abilities } = authenticateQuery(ctx); // Resolve const recipe = await Recipe.findById(args.id) .accessibleBy(abilities) .catch(err => { if (err instanceof Error.CastError) { throw new UserInputError('Invalid Properties'); } throw new ApolloError('Unknown Error'); }); if (recipe === null) throw new ForbiddenError('Recipe Not Found'); return recipe; }, });