Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions app/graphql/mutations/notifications/create_notification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Mutations
module Notifications
class CreateNotification < BaseMutation
graphql_name 'CreateNotification'

# Required arguments for creating a notification.
argument :title, String, required: true
argument :body, String, required: false
argument :user_id, ID, required: true

# Fields returned by the mutation.
field :notification, Types::NotificationType, null: true
field :errors, [String], null: false

def resolve(title:, body: nil, user_id:)
notification = Notification.new(title: title, body: body, user_id: user_id)

if notification.save
{
notification: notification,
errors: []
}
else
{
notification: nil,
errors: notification.errors.full_messages
}
end
end
end
end
end
26 changes: 26 additions & 0 deletions app/graphql/mutations/notifications/update_notification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Mutations
module Notifications
class MarkNotificationAsRead < BaseMutation
graphql_name 'MarkNotificationAsRead'

# Input argument to indicate which notification to update.
argument :id, ID, required: true

# The response includes the updated notification and any errors.
field :notification, Types::NotificationType, null: true
field :errors, [String], null: false

def resolve(id:)
notification = Notification.find_by(id: id)
return { notification: nil, errors: ["Notification not found"] } unless notification

notification.read = true
if notification.save
{ notification: notification, errors: [] }
else
{ notification: nil, errors: notification.errors.full_messages }
end
end
end
end
end