From 4e823e590283aabaf5d8df6301bef06b8d464d6c Mon Sep 17 00:00:00 2001 From: Savy011 <106910589+Savy011@users.noreply.github.com> Date: Thu, 18 May 2023 19:12:09 +0530 Subject: [PATCH] Finished Ex 8.15 --- Part_8/library-graphql/index.js | 41 +++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/Part_8/library-graphql/index.js b/Part_8/library-graphql/index.js index 3430b35..aad1ff0 100644 --- a/Part_8/library-graphql/index.js +++ b/Part_8/library-graphql/index.js @@ -3,6 +3,7 @@ const { startStandaloneServer } = require('@apollo/server/standalone') const mongoose = require('mongoose') const Book = require('./models/book') const Author = require('./models/author') +const { GraphQLError } = require('graphql') require('dotenv').config() const MONGODB_URI = process.env.MONGODB_URI @@ -88,12 +89,33 @@ const resolvers = { if (!authorInDB) { authorInDB = new Author({ name: author }) - await authorInDB.save() + + try { + await authorInDB.save() + } catch (error) { + throw new GraphQLError('Saving Author failed', { + extensions: { + code: 'BAD_USER_INPUT', + invalidArgs: args.author, + error + } + }) + } } const book = new Book({ title, published, genres, author: authorInDB._id }) - - await book.save() + + try { + await book.save() + } catch (error) { + throw new GraphQLError('Saving Book failed', { + extensions: { + code: 'BAD_USER_INPUT', + invalidArgs: args, + error + } + }) + } return book.populate('author') }, editAuthor: async (root, args) => { @@ -104,8 +126,19 @@ const resolvers = { authorToUpdate.born = setBornTo + try { + await authorToUpdate.save() + } catch (error) { + throw new GraphQLError('Updated Author Failed', { + extensions: { + code: 'BAD_USER_INPUT', + invalidArgs: args, + error + } + }) + } - return authorToUpdate.save() + return authorToUpdate } }, Author: { -- 2.51.2