File tree Expand file tree Collapse file tree 2 files changed +58
-0
lines changed
app/graphql/mutations/notifications Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change 1+ module Mutations
2+ module Notifications
3+ class CreateNotification < BaseMutation
4+ graphql_name 'CreateNotification'
5+
6+ # Required arguments for creating a notification.
7+ argument :title , String , required : true
8+ argument :body , String , required : false
9+ argument :user_id , ID , required : true
10+
11+ # Fields returned by the mutation.
12+ field :notification , Types ::NotificationType , null : true
13+ field :errors , [ String ] , null : false
14+
15+ def resolve ( title :, body : nil , user_id :)
16+ notification = Notification . new ( title : title , body : body , user_id : user_id )
17+
18+ if notification . save
19+ {
20+ notification : notification ,
21+ errors : [ ]
22+ }
23+ else
24+ {
25+ notification : nil ,
26+ errors : notification . errors . full_messages
27+ }
28+ end
29+ end
30+ end
31+ end
32+ end
Original file line number Diff line number Diff line change 1+ module Mutations
2+ module Notifications
3+ class MarkNotificationAsRead < BaseMutation
4+ graphql_name 'MarkNotificationAsRead'
5+
6+ # Input argument to indicate which notification to update.
7+ argument :id , ID , required : true
8+
9+ # The response includes the updated notification and any errors.
10+ field :notification , Types ::NotificationType , null : true
11+ field :errors , [ String ] , null : false
12+
13+ def resolve ( id :)
14+ notification = Notification . find_by ( id : id )
15+ return { notification : nil , errors : [ "Notification not found" ] } unless notification
16+
17+ notification . read = true
18+ if notification . save
19+ { notification : notification , errors : [ ] }
20+ else
21+ { notification : nil , errors : notification . errors . full_messages }
22+ end
23+ end
24+ end
25+ end
26+ end
You can’t perform that action at this time.
0 commit comments