Skip to content

Instantly share code, notes, and snippets.

@gfx
Created June 5, 2018 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gfx/695914ec209798a11b2b11d2e495196e to your computer and use it in GitHub Desktop.
Save gfx/695914ec209798a11b2b11d2e495196e to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
module Types
class CountableObject < BaseObject
class << self
def define_connection(**args, &block)
super(**args) do
block&.call
# this type must be GraphQL::INT_TYPE (not Integer), may be a bug.
field :totalCount, GraphQL::INT_TYPE, resolve: ->(object, _args, _ctx) {
relation = object.nodes
if relation.respond_to?(:total_count)
relation.total_count # kaminari-zed relation instance
else
relation.count
end
}
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment