Module: GraphqlModelMapper::MapperType
- Defined in:
- lib/graphql_model_mapper/mapper_type.rb
Class Method Summary collapse
-
.convert_type(db_type, db_sql_type = "", nullable = true) ⇒ GraphQL::ScalarType
convert a database type to a GraphQL type.
- .get_ar_object(name, required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: false, primary_keys: false, validation_keys: false, association_macro: nil, source_nulls: true, type_key: nil, type_sub_key: nil) ⇒ Object
- .get_ar_object_with_params(name, type_key: nil, type_sub_key: nil) ⇒ Object
- .get_type_params(name, type_key: nil, type_sub_key: nil) ⇒ Object
- .graphql_default_types(query: { input_type: { required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: true, primary_keys: true, validation_keys: false, association_macro: nil, source_nulls: false, type_key: :query, type_sub_key: :input_type }, output_type: { required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: true, primary_keys: true, validation_keys: false, association_macro: nil, source_nulls: false, type_key: :query, type_sub_key: :output_type } }, update: { input_type: { required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: true, primary_keys: true, validation_keys: false, association_macro: nil, source_nulls: false, type_key: :update, type_sub_key: :input_type }, output_type: { required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: true, primary_keys: true, validation_keys: false, association_macro: nil, source_nulls: true, type_key: :update, type_sub_key: :output_type } }, delete: { input_type: { required_attributes: [:id], excluded_attributes: [], allowed_attributes: [:id], foreign_keys: false, primary_keys: true, validation_keys: false, association_macro: nil, source_nulls: false, type_key: :delete, type_sub_key: :input_type }, output_type: { required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: false, primary_keys: true, validation_keys: false, association_macro: nil, source_nulls: true, type_key: :delete, type_sub_key: :output_type } }, create: { input_type: { required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: true, primary_keys: false, validation_keys: false, association_macro: :has_many, source_nulls: false, type_key: :create, type_sub_key: :input_type }, output_type: { required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: true, primary_keys: true, validation_keys: false, association_macro: nil, source_nulls: true, type_key: :create, type_sub_key: :output_type } }) ⇒ Object
- .graphql_types(name: self.name, query: {}, update: {}, delete: {}, create: {}) ⇒ Object
- .model_validation_keys(name) ⇒ Object
Class Method Details
.convert_type(db_type, db_sql_type = "", nullable = true) ⇒ GraphQL::ScalarType
convert a database type to a GraphQL type
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/graphql_model_mapper/mapper_type.rb', line 321 def self.convert_type db_type, db_sql_type="", nullable=true # because we are outside of a GraphQL define block we cannot use the types helper # we must refer directly to the built-in GraphQL scalar types case db_type when :integer nullable ? GraphQL::INT_TYPE : !GraphQL::INT_TYPE when :decimal, :float nullable ? GraphQL::FLOAT_TYPE : !GraphQL::FLOAT_TYPE when :boolean nullable ? GraphQL::BOOLEAN_TYPE : !GraphQL::BOOLEAN_TYPE when :date, :datetime nullable ? GraphqlModelMapper::DATE_TYPE : !GraphqlModelMapper::DATE_TYPE else case db_sql_type.to_sym #these are strings not symbols when :geometry, :multipolygon, :polygon case db_type when :string nullable ? GraphqlModelMapper::GEOMETRY_OBJECT_TYPE : !GraphqlModelMapper::GEOMETRY_OBJECT_TYPE else nullable ? GraphqlModelMapper::GEOMETRY_STRING_TYPE : !GraphqlModelMapper::GEOMETRY_STRING_TYPE end else nullable ? GraphQL::STRING_TYPE : !GraphQL::STRING_TYPE end end end |
.get_ar_object(name, required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: false, primary_keys: false, validation_keys: false, association_macro: nil, source_nulls: true, type_key: nil, type_sub_key: nil) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/graphql_model_mapper/mapper_type.rb', line 30 def self.get_ar_object(name, required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: false, primary_keys: false, validation_keys: false, association_macro: nil, source_nulls: true, type_key: nil, type_sub_key: nil) #typesuffix = method(__method__).parameters.map { |arg| eval arg[1].to_s }.hash.abs.to_i.to_s typesuffix = "#{type_key.to_s.classify}#{GraphqlModelMapper.underscore(type_sub_key.to_s)}".camelize typename = "#{GraphqlModelMapper.get_type_name(name)}#{typesuffix}" return GraphqlModelMapper.get_constant(typename) if GraphqlModelMapper.defined_constant?(typename) model = name.classify.constantize required_attributes = required_attributes.map(&:to_sym) | (validation_keys ? self.model_validation_keys(name) : []) columns = model.columns_hash # figure out which association fields we are exposing association_includes = (model.reflect_on_all_associations(association_macro).map(&:name)).map(&:to_sym) - excluded_attributes # find all relations for this model, skip ones where the association klass is invalid, as well as polymorphic associations, be cognizant of include/exclude arrays similar to dbfields associations = model.reflect_on_all_associations(association_macro).select{|t| begin t.klass rescue next end}.select{|t| !t.[:polymorphic] && association_includes.include?(t.name.to_sym) } # never show foreign keys for defined associations db_fields_never = foreign_keys ? [] : ( associations.map(&:association_foreign_key) + associations.map(&:options).select{|v| v.key?(:foreign_key) }.map {|x| x[:foreign_key]} ).uniq.map(&:to_sym) # figure out which database fields we are exposing allowed_attributes = allowed_attributes.count > 0 ? allowed_attributes.map(&:to_sym) : associations.map(&:name) + columns.keys.map(&:to_sym) allowed_associations = (associations.map(&:name) - excluded_attributes - db_fields_never) & allowed_attributes db_fields = (columns.keys.map(&:to_sym) - excluded_attributes - db_fields_never) & allowed_attributes associations = associations.select{|m| allowed_associations.include?(m.name)} ret_type = GraphQL::InputObjectType.define do #ensure type name is unique so it does not collide with known types name typename description "an input interface for the #{name} ActiveRecord model" # create GraphQL fields for each exposed database field db_fields.select{|s| (primary_keys && s.to_sym == :id)}.each do |f| argument f.to_sym, -> {GraphqlModelMapper::MapperType.convert_type(columns[f.to_s].type, columns[f.to_s].sql_type, (source_nulls ? columns[f.to_s].null : true))} end db_fields.select{|s| required_attributes.include?(s)}.each do |f| argument f.to_sym, -> {GraphqlModelMapper.convert_type(columns[f.to_s].type, columns[f.to_s].sql_type, false)} end # create GraphQL fields for each association associations.sort_by(&:name).each do |reflection| begin klass = reflection.klass rescue GraphqlModelMapper.logger.warning("invalid relation #{reflection.name} specified on the #{name} model, the relation class does not exist") next # most likely an invalid association without a class name, skip if other errors are encountered end if reflection.macro == :has_many argument reflection.name.to_sym, -> {GraphqlModelMapper::MapperType.get_ar_object_with_params(klass.name, type_key: type_key, type_sub_key: type_sub_key).to_list_type} do if GraphqlModelMapper. ->(ctx, model_name, access_type) { GraphqlModelMapper.(ctx, model_name, access_type.to_sym) } model_name klass.name access_type type_key.to_s end end else argument reflection.name.to_sym, -> {GraphqlModelMapper::MapperType.get_ar_object_with_params(klass.name, type_key: type_key, type_sub_key: type_sub_key)} do if GraphqlModelMapper. ->(ctx, model_name, access_type) { GraphqlModelMapper.(ctx, model_name, access_type.to_sym) } model_name klass.name access_type type_key.to_s end end end end db_fields.reject{|s| (primary_keys && s.to_sym == :id) || required_attributes.include?(s)}.sort.each do |f| argument f.to_sym, -> {GraphqlModelMapper::MapperType.convert_type(columns[f.to_s].type, columns[f.to_s].sql_type, (source_nulls ? columns[f.to_s].null : true))} end end if type_sub_key == :input_type ret_type = GraphQL::ObjectType.define do #ensure type name is unique so it does not collide with known types name typename description "an output interface for the #{name} ActiveRecord model" # create GraphQL fields for each exposed database field db_fields.select{|s| (primary_keys && s.to_sym == :id)}.each do |f| #puts "source null #{f} #{source_nulls ? columns[f.to_s].null : true}" field f.to_sym, -> {GraphqlModelMapper::MapperType.convert_type(columns[f.to_s].type, columns[f.to_s].sql_type, (source_nulls ? columns[f.to_s].null : true))} end db_fields.select{|s| required_attributes.include?(s)}.sort.each do |f| field f.to_sym, -> {GraphqlModelMapper::MapperType.convert_type(columns[f.to_s].type, columns[f.to_s].sql_type, false)} end # create GraphQL fields for each association associations.sort_by(&:name).each do |reflection| begin klass = reflection.klass rescue GraphqlModelMapper.logger.warning("invalid relation #{reflection.name} specified on the #{name} model, the relation class does not exist") next # most likely an invalid association without a class name, skip if other errors are encountered end if reflection.macro == :has_many if [:deep].include?(GraphqlModelMapper.nesting_strategy) && type_key == :query connection reflection.name.to_sym, -> {GraphqlModelMapper::MapperType.get_ar_object_with_params(klass.name, type_key: type_key, type_sub_key: type_sub_key).connection_type} do if GraphqlModelMapper. ->(ctx, model_name, access_type) { GraphqlModelMapper.(ctx, model_name, access_type.to_sym) } model_name klass.name access_type :read.to_s end end else field reflection.name.to_sym, -> {GraphqlModelMapper::MapperType.get_ar_object_with_params(klass.name, type_key: type_key, type_sub_key: type_sub_key).to_list_type} do if GraphqlModelMapper. ->(ctx, model_name, access_type) { GraphqlModelMapper.(ctx, model_name, access_type.to_sym) } model_name klass.name access_type :read.to_s end end end else field reflection.name.to_sym, -> {GraphqlModelMapper::MapperType.get_ar_object_with_params(klass.name, type_key: type_key, type_sub_key: type_sub_key)} do if GraphqlModelMapper. ->(ctx, model_name, access_type) { GraphqlModelMapper.(ctx, model_name, access_type.to_sym) } model_name klass.name access_type :read.to_s end end end end db_fields.reject{|s| (primary_keys && s.to_sym == :id) || required_attributes.include?(s)}.sort.each do |f| #puts "source null #{f} #{source_nulls ? columns[f.to_s].null : true}" field f.to_sym, -> {GraphqlModelMapper::MapperType.convert_type(columns[f.to_s].type, columns[f.to_s].sql_type, (source_nulls ? columns[f.to_s].null : true))} end end if type_sub_key == :output_type GraphqlModelMapper.set_constant(typename, ret_type) if !GraphqlModelMapper.defined_constant?(typename) ret_type end |
.get_ar_object_with_params(name, type_key: nil, type_sub_key: nil) ⇒ Object
26 27 28 |
# File 'lib/graphql_model_mapper/mapper_type.rb', line 26 def self.get_ar_object_with_params(name, type_key: nil, type_sub_key: nil) self.get_ar_object(name, self.get_type_params(name, type_key: type_key, type_sub_key: type_sub_key)) end |
.get_type_params(name, type_key: nil, type_sub_key: nil) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/graphql_model_mapper/mapper_type.rb', line 169 def self.get_type_params(name, type_key: nil, type_sub_key: nil) model = name.classify.constantize if model.public_methods.include?(:graphql_types) params = model.graphql_types else params = self.graphql_default_types end #puts params if !type_key.nil? if params.keys.include?(type_key.to_sym) params = params[type_key.to_sym] if !type_sub_key.nil? if params.keys.include?(type_sub_key.to_sym) params = params[type_sub_key.to_sym] else params = nil end end else params = nil end end params end |
.graphql_default_types(query: { input_type: { required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: true, primary_keys: true, validation_keys: false, association_macro: nil, source_nulls: false, type_key: :query, type_sub_key: :input_type }, output_type: { required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: true, primary_keys: true, validation_keys: false, association_macro: nil, source_nulls: false, type_key: :query, type_sub_key: :output_type } }, update: { input_type: { required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: true, primary_keys: true, validation_keys: false, association_macro: nil, source_nulls: false, type_key: :update, type_sub_key: :input_type }, output_type: { required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: true, primary_keys: true, validation_keys: false, association_macro: nil, source_nulls: true, type_key: :update, type_sub_key: :output_type } }, delete: { input_type: { required_attributes: [:id], excluded_attributes: [], allowed_attributes: [:id], foreign_keys: false, primary_keys: true, validation_keys: false, association_macro: nil, source_nulls: false, type_key: :delete, type_sub_key: :input_type }, output_type: { required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: false, primary_keys: true, validation_keys: false, association_macro: nil, source_nulls: true, type_key: :delete, type_sub_key: :output_type } }, create: { input_type: { required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: true, primary_keys: false, validation_keys: false, association_macro: :has_many, source_nulls: false, type_key: :create, type_sub_key: :input_type }, output_type: { required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: true, primary_keys: true, validation_keys: false, association_macro: nil, source_nulls: true, type_key: :create, type_sub_key: :output_type } }) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/graphql_model_mapper/mapper_type.rb', line 194 def self.graphql_default_types( query: { input_type: { required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: true, primary_keys: true, validation_keys: false, association_macro: nil, source_nulls: false, type_key: :query, type_sub_key: :input_type }, output_type: { required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: true, primary_keys: true, validation_keys: false, association_macro: nil, source_nulls: false, type_key: :query, type_sub_key: :output_type } }, update: { input_type: { required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: true, primary_keys: true, validation_keys: false, association_macro: nil, source_nulls: false, type_key: :update, type_sub_key: :input_type }, output_type: { required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: true, primary_keys: true, validation_keys: false, association_macro: nil, source_nulls: true, type_key: :update, type_sub_key: :output_type } }, delete: { input_type: { required_attributes: [:id], excluded_attributes: [], allowed_attributes: [:id], foreign_keys: false, primary_keys: true, validation_keys: false, association_macro: nil, source_nulls: false, type_key: :delete, type_sub_key: :input_type }, output_type: { required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: false, primary_keys: true, validation_keys: false, association_macro: nil, source_nulls: true, type_key: :delete, type_sub_key: :output_type } }, create: { input_type: { required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: true, primary_keys: false, validation_keys: false, association_macro: :has_many, source_nulls: false, type_key: :create, type_sub_key: :input_type }, output_type: { required_attributes: [], excluded_attributes: [], allowed_attributes: [], foreign_keys: true, primary_keys: true, validation_keys: false, association_macro: nil, source_nulls: true, type_key: :create, type_sub_key: :output_type } }) return GraphqlModelMapper.get_constant("GRAPHQL_DEFAULT_TYPES") if GraphqlModelMapper.const_defined?("GRAPHQL_DEFAULT_TYPES") graphql_type = {} graphql_type[:query] = query graphql_type[:update] = update graphql_type[:delete] = delete graphql_type[:create] = create GraphqlModelMapper.set_constant("GRAPHQL_DEFAULT_TYPES", graphql_type) graphql_type end |
.graphql_types(name: self.name, query: {}, update: {}, delete: {}, create: {}) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/graphql_model_mapper/mapper_type.rb', line 3 def self.graphql_types( name: self.name, query: {}, update: {}, delete: {}, create: {} ) #typesuffix = method(__method__).parameters.map { |arg| eval arg[1].to_s }.hash.abs.to_i.to_s GraphqlModelMapper.logger.info("#{name.upcase}") if GraphqlModelMapper.const_defined?("#{name.upcase}_GRAPHQL_DEFAULT_TYPES") return GraphqlModelMapper.get_constant("#{name.upcase}_GRAPHQL_DEFAULT_TYPES") if GraphqlModelMapper.const_defined?("#{name.upcase}_GRAPHQL_DEFAULT_TYPES") #GraphqlModelMapper.logger.info "#{name.upcase}_GRAPHQL_DEFAULT_TYPES" graphql_type = {} graphql_type[:query] = query graphql_type[:update] = update graphql_type[:delete] = delete graphql_type[:create] = create merged_graphql_type = self.graphql_default_types.deep_merge(graphql_type) GraphqlModelMapper.set_constant("#{name.upcase}_GRAPHQL_DEFAULT_TYPES", merged_graphql_type) merged_graphql_type end |
.model_validation_keys(name) ⇒ Object
311 312 313 314 315 |
# File 'lib/graphql_model_mapper/mapper_type.rb', line 311 def self.model_validation_keys(name) model = name.classify.constantize validation_attributes = model.validators.select{|m| m.is_a?(ActiveModel::Validations::PresenceValidator) && !m.[:if]}.map(&:attributes).flatten model.reflect_on_all_associations.select{|p| validation_attributes.include?(p.name) }.map(&:foreign_key).map(&:to_sym) | validation_attributes & model.columns_hash.keys.map(&:to_sym) end |