diff --git a/app/graphql/mutations/notifications/create_notification.rb b/app/graphql/mutations/notifications/create_notification.rb new file mode 100644 index 0000000..1050c36 --- /dev/null +++ b/app/graphql/mutations/notifications/create_notification.rb @@ -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 \ No newline at end of file diff --git a/app/graphql/mutations/notifications/update_notification.rb b/app/graphql/mutations/notifications/update_notification.rb new file mode 100644 index 0000000..1774539 --- /dev/null +++ b/app/graphql/mutations/notifications/update_notification.rb @@ -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 \ No newline at end of file