Module: Hai::GraphQL::Types::ClassMethods
- Defined in:
- lib/hai/graphql/types.rb
Instance Method Summary collapse
- #add_base_type_reflections(name, ref) ⇒ Object
- #add_filter_type_reflections(name, ref) ⇒ Object
- #define_base_type(model) ⇒ Object
- #define_filter_type(model) ⇒ Object
- #define_input_object(model) ⇒ Object
- #hai_types(*models) ⇒ Object
- #name_for_base_type(model) ⇒ Object
- #name_for_filter_type(model) ⇒ Object
Instance Method Details
#add_base_type_reflections(name, ref) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/hai/graphql/types.rb', line 57 def add_base_type_reflections(name, ref) if name == ref.plural_name name_for_base_type(ref.active_record).constantize.send( :field, name, "[#{name_for_base_type(ref.klass)}]" ) else name_for_base_type(ref.active_record).constantize.send( :field, name, name_for_base_type(ref.klass) ) end rescue NoMethodError => e binding.pry end |
#add_filter_type_reflections(name, ref) ⇒ Object
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/hai/graphql/types.rb', line 111 def add_filter_type_reflections(name, ref) name_for_filter_type(ref.active_record).constantize.send( :argument, name, name_for_filter_type(ref.klass), required: false ) rescue NoMethodError, RuntimeError => e binding.pry end |
#define_base_type(model) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/hai/graphql/types.rb', line 43 def define_base_type(model) return if const_defined? name_for_base_type(model) klass = Class.new(::Types::BaseObject) model.attribute_types.each do |attr, type| next if defined?(model.get_restricted_attributes) && model.get_restricted_attributes.include?(attr.to_sym) # add test plz klass.send(:field, attr, Hai::GraphQL::TYPE_CAST[type.class] || Hai::GraphQL::TYPE_CAST[type.class.superclass]) rescue ArgumentError => e binding.pry end ::Types.const_set "#{model}Type", klass end |
#define_filter_type(model) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/hai/graphql/types.rb', line 95 def define_filter_type(model) # Class Filter filter_klass = Class.new(::GraphQL::Schema::InputObject) model.attribute_types.each do |attr, type| filter_klass.send( :argument, attr, AREL_TYPE_CAST[type.class] || AREL_TYPE_CAST[type.class.superclass], required: false ) end Object.const_set name_for_filter_type(model), filter_klass end |
#define_input_object(model) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/hai/graphql/types.rb', line 75 def define_input_object(model) # input objects klass = Class.new(::Types::BaseInputObject) klass.description("Attributes for creating or updating a #{model}.") model.attribute_types.each do |attr, type| next if %w[id created_at updated_at].include?(attr) klass.argument( attr, Hai::GraphQL::TYPE_CAST[type.class]|| Hai::GraphQL::TYPE_CAST[type.class.superclass], required: false ) end ::Types.const_set "#{model}Attributes", klass end |
#hai_types(*models) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/hai/graphql/types.rb', line 17 def hai_types(*models) base_types = models.map(&method(:define_base_type)) models.each do |model| model.reflections.map(&method(:add_base_type_reflections)) end models.map(&method(:define_input_object)) filter_types = models.map(&method(:define_filter_type)) models.each do |model| model.reflections.map(&method(:add_filter_type_reflections)) end filter_types.each do |filter_type| filter_type.send( :argument, :or, "[#{filter_type}]", required: false ) end end |
#name_for_base_type(model) ⇒ Object
39 40 41 |
# File 'lib/hai/graphql/types.rb', line 39 def name_for_base_type(model) "Types::#{model}Type" end |
#name_for_filter_type(model) ⇒ Object
91 92 93 |
# File 'lib/hai/graphql/types.rb', line 91 def name_for_filter_type(model) "#{model}FilterInputType" end |