@@ -37,10 +37,10 @@ class UpdateBook(graphene.Mutation):
3737
3838 class Arguments :
3939 book_id = graphene .Int (required = True )
40- title = graphene .String ()
41- author = graphene .String ()
42- description = graphene .String ()
43- url = graphene .String ()
40+ title = graphene .String (required = False )
41+ author = graphene .String (required = False )
42+ description = graphene .String (required = False )
43+ url = graphene .String (required = False )
4444
4545 def mutate (self , info , book_id , title , author , description , url ):
4646 user = info .context .user
@@ -58,6 +58,25 @@ def mutate(self, info, book_id, title, author, description, url):
5858
5959 return UpdateBook (book = book )
6060
61+
62+ class DeleteBook (graphene .Mutation ):
63+ book_id = graphene .Int ()
64+
65+ class Arguments :
66+ book_id = graphene .Int (required = True )
67+
68+ def mutate (self , info , book_id ):
69+ user = info .context .user
70+ book = Book .objects .get (id = book_id )
71+
72+ if book .posted_by != user :
73+ raise Exception ("Not permited to delete this book" )
74+
75+ book .delete ()
76+
77+ return DeleteBook (book_id = book_id )
78+
6179class Mutation (graphene .ObjectType ):
6280 create_book = CreateBook .Field ()
63- update_book = UpdateBook .Field ()
81+ update_book = UpdateBook .Field ()
82+ delete_book = DeleteBook .Field ()
0 commit comments