Module: GqlSerializer::ActiveRecord

Defined in:
lib/gql_serializer/extensions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.as_gql(query = nil, options = {}) ⇒ Object



21
22
23
# File 'lib/gql_serializer/extensions.rb', line 21

def self.as_gql(query = nil, options = {})
  self.all.as_gql(query, options)
end

Instance Method Details

#as_gql(query = nil, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gql_serializer/extensions.rb', line 25

def as_gql(query = nil, options = {})
  options_with_defaults = GqlSerializer.configuration.to_h.merge(options)
  query_hasharray = query ? GqlSerializer.parse_query(query) : []
  include_hasharray = GqlSerializer.query_include(self.class, query_hasharray)
  if options_with_defaults[:preload]
    GqlSerializer._preload([self], include_hasharray)
    GqlSerializer.serialize(self, query_hasharray, options_with_defaults)
  else
    record = include_hasharray.empty? ? self : self.class.where(id: self).includes(include_hasharray).first
    GqlSerializer.serialize(record, query_hasharray, options_with_defaults)
  end
end