I use graphql gem 1.8.11.
My UserType
has notifications
connection field and if it is queried I'd like to perform some operation (read!
in example below).
Example below performs the operation for all of association field.
Even if notifications
field has pagination parameter and not all of notifications
are queried, the operation is performed for all notifications
.
How can I perform the operation only for queried nodes?
module Types
class UserType < Types::BaseObject
implements GraphQL::Relay::Node.interface
field :id, ID, null: false
field :notifications, NotificationType.connection_type, null: true
global_id_field :id
def notifications
object.notifications.tap { |o| o.read! }
end
end
end
I ended up to customize connection class.
app/graphql/types/notification_type.rb:
module Types
class NotificationType < Types::BaseObject
#
# existing code
#
class << self
def connection_type_class
@connection_type_class ||= Types::NotificationConnectionType
end
end
end
end
app/graphql/types/notificaiton_connection_type.rb:
module Types
class NotificationConnectionType < GraphQL::Types::Relay::BaseConnection
def nodes
super.map(&:read!)
end
end
end