Class: GraphitiGql::Schema::ResourceType

Inherits:
Object
  • Object
show all
Defined in:
lib/graphiti_gql/schema/resource_type.rb

Defined Under Namespace

Modules: BaseInterface

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, implements: nil) ⇒ ResourceType

Returns a new instance of ResourceType.



82
83
84
85
# File 'lib/graphiti_gql/schema/resource_type.rb', line 82

def initialize(resource, implements: nil)
  @resource = resource
  @implements = implements
end

Class Method Details

.add_fields(type, resource, id: true) ⇒ Object

id: false for edges



18
19
20
21
22
23
24
# File 'lib/graphiti_gql/schema/resource_type.rb', line 18

def self.add_fields(type, resource, id: true) # id: false for edges
  resource.attributes.each_pair do |name, config|
    next if name == :id && id == false

    Fields::Attribute.new(resource, name, config).apply(type) if config[:readable]
  end
end

.add_relationships(resource, type) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/graphiti_gql/schema/resource_type.rb', line 61

def self.add_relationships(resource, type)
  resource.sideloads.each do |_name, sideload|
    next unless sideload.readable?

    registered_sl = if sideload.type == :polymorphic_belongs_to
                      PolymorphicBelongsToInterface
                        .new(resource, sideload)
                        .build
                    else
                      Schema.registry.get(sideload.resource.class)
                    end
    sideload_type = registered_sl[:type]

    if %i[has_many many_to_many has_one].include?(sideload.type)
      Fields::ToMany.new(sideload, sideload_type).apply(type)
    else
      Fields::ToOne.new(sideload, sideload_type).apply(type)
    end
  end
end

.add_value_objects(resource, type) ⇒ Object



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
52
53
54
55
56
57
58
59
# File 'lib/graphiti_gql/schema/resource_type.rb', line 26

def self.add_value_objects(resource, type)
  resource.config[:value_objects].each_pair do |name, vo_association|
    vo_resource_class = vo_association.resource_class
    value_object_type = Schema.registry.get(vo_resource_class)[:type]
    value_object_type = [value_object_type] if vo_association.array?

    _array = vo_association.array?
    opts = { null: vo_association.null }
    opts[:deprecation_reason] = vo_association.deprecation_reason if vo_association.deprecation_reason
    type.field name, value_object_type, **opts
    type.define_method name do
      if (method_name = vo_association.readable) && !vo_association.parent_resource_class.new.send(method_name)
        raise ::Graphiti::Errors::UnreadableAttribute
          .new(vo_association.parent_resource_class, name)
      end

      result = vo_resource_class.all({ parent: object }).to_a
      default_behavior = result == [object]
      result = result.first unless _array
      if default_behavior
        method_name = vo_association.alias.presence || name
        result = object.send(method_name)
        raise Graphiti::Errors::InvalidValueObject.new(resource, name, result) if _array && !result.is_a?(Array)
      end
      # For polymorphic value objects
      if result.is_a?(Array)
        result.each { |r| r.instance_variable_set(:@__parent, object) }
      else
        result.instance_variable_set(:@__parent, object) if result
      end
      result
    end
  end
end

Instance Method Details

#buildObject



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/graphiti_gql/schema/resource_type.rb', line 87

def build
  return registry.get(@resource)[:type] if registry.get(@resource)

  type = build_base_type
  registry_name = registry.key_for(@resource, interface: poly_parent?)
  type.connection_type_class(build_connection_class)
  type.graphql_name(registry_name)
  type.implements(@implements) if @implements
  add_fields(type, @resource)
  registry.set(@resource, type, interface: poly_parent?)
  process_polymorphic_parent(type) if poly_parent?
  type
end