Sha256: 78533a6f38827e7183dd2976644a8563cbe2647e940595190f1acd1fa56ee53d

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require "active_record"

module GqlSerializer
  module Array
    def as_gql(query = nil)
      map { |v| v.as_gql(query) }
    end
  end

  module Relation
    def as_gql(query = nil, options = {})
      query_hasharray = query ? GqlSerializer.parse_query(query) : []
      include_hasharray = GqlSerializer.query_include(self.model, query_hasharray)
      records = self.includes(include_hasharray).records
      options_with_defaults = GqlSerializer.configuration.to_h.merge(options)
      GqlSerializer.serialize(records, query_hasharray, options_with_defaults)
    end
  end

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

    def as_gql(query = nil, options = {})

      query_hasharray = query ? GqlSerializer.parse_query(query) : []
      include_hasharray = GqlSerializer.query_include(self.class, query_hasharray)
      record = include_hasharray.empty? ? self : self.class.where(id: self).includes(include_hasharray).first
      options_with_defaults = GqlSerializer.configuration.to_h.merge(options)
      GqlSerializer.serialize(record, query_hasharray, options_with_defaults)
    end
  end
end

ActiveRecord::Base.include GqlSerializer::ActiveRecord
ActiveRecord::Relation.include GqlSerializer::Relation
Array.include GqlSerializer::Array

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gql_serializer-2.1.1 lib/gql_serializer/extensions.rb