Class: GraphitiGql::Schema::Fields::Attribute
- Inherits:
-
Object
- Object
- GraphitiGql::Schema::Fields::Attribute
- Defined in:
- lib/graphiti_gql/schema/fields/attribute.rb
Instance Method Summary collapse
- #apply(type) ⇒ Object
-
#initialize(resource, name, config) ⇒ Attribute
constructor
If sideload is present, we’re applying m2m metadata to an edge.
Constructor Details
#initialize(resource, name, config) ⇒ Attribute
If sideload is present, we’re applying m2m metadata to an edge
6 7 8 9 10 11 |
# File 'lib/graphiti_gql/schema/fields/attribute.rb', line 6 def initialize(resource, name, config) @resource = resource @config = config @name = name @alias = config[:alias] end |
Instance Method Details
#apply(type) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/graphiti_gql/schema/fields/attribute.rb', line 13 def apply(type) is_nullable = !!@config[:null] _config = @config _name = @name _alias = @alias opts = @config.slice(:null, :deprecation_reason) type.field(_name, field_type, **opts) type.define_method _name do if (readable = _config[:readable]).is_a?(Symbol) obj = object resource = obj.instance_variable_get(:@__graphiti_resource) unless resource.send(readable) path = Graphiti.context[:object][:current_path].join(".") raise Errors::UnauthorizedField.new(path) end end value = if _config[:proc] instance_eval(&_config[:proc]) else if object.is_a?(Hash) object[_name] || object[_name.to_s] else object.send(_alias || _name) end end return if value.nil? caster = Graphiti::Types[_config[:type]][:read] # Dry::Types can't be instance_exec'd # This dependency has probably served it's purpose and can be # refactored away if caster.is_a?(Proc) instance_exec(value, &caster) else caster.call(value) end end end |