module HQ::GraphQL::Ext::InputObjectExtensions::ClassMethods

Public Instance Methods

nested_attributes() click to toggle source
# File lib/hq/graphql/ext/input_object_extensions.rb, line 65
def nested_attributes
  @nested_attributes ||= Set.new
end
with_model(model_name, attributes: true, associations: false, enums: true, excluded_inputs: []) click to toggle source

Class Methods ####

# File lib/hq/graphql/ext/input_object_extensions.rb, line 44
def with_model(model_name, attributes: true, associations: false, enums: true, excluded_inputs: [])
  self.model_name = model_name
  self.auto_load_attributes = attributes
  self.auto_load_associations = associations
  self.auto_load_enums = enums

  lazy_load do
    excluded_inputs += ::HQ::GraphQL.excluded_inputs

    model_columns.each do |column|
      argument_from_column(column) unless excluded_inputs.include?(column.name.to_sym)
    end

    model_associations.each do |association|
      argument_from_association(association) unless excluded_inputs.include?(association.name.to_sym)
    end

    argument :X, String, required: false
  end
end

Private Instance Methods

argument_exists?(name) click to toggle source
# File lib/hq/graphql/ext/input_object_extensions.rb, line 101
def argument_exists?(name)
  !!arguments[camelize(name)]
end
argument_from_association(association) click to toggle source
# File lib/hq/graphql/ext/input_object_extensions.rb, line 71
def argument_from_association(association)
  is_enum = is_enum?(association)
  input_or_type = is_enum ? ::HQ::GraphQL::Types[association.klass] : ::HQ::GraphQL::Inputs[association.klass]
  name = association.name
  return if argument_exists?(name)

  case association.macro
  when :has_many
    argument name, [input_or_type], required: false
  else
    argument name, input_or_type, required: false
  end

  return if is_enum

  if !model_klass.nested_attributes_options.key?(name.to_sym)
    model_klass.accepts_nested_attributes_for name, allow_destroy: true
  end

  nested_attributes << name.to_s
rescue ::HQ::GraphQL::Inputs::Error
  nil
end
argument_from_column(column) click to toggle source
# File lib/hq/graphql/ext/input_object_extensions.rb, line 95
def argument_from_column(column)
  name = column.name
  return if argument_exists?(name)
  argument name, ::HQ::GraphQL::Types.type_from_column(column), required: false
end