Class: GraphQL::Models::BackedByModel

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/models/backed_by_model.rb

Constant Summary collapse

DEFAULT_OBJECT_TO_MODEL =
-> (obj) { obj }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph_type, model_type) ⇒ BackedByModel

Returns a new instance of BackedByModel.



8
9
10
11
12
# File 'lib/graphql/models/backed_by_model.rb', line 8

def initialize(graph_type, model_type)
  @graph_type = graph_type
  @model_type = model_type
  @object_to_model = DEFAULT_OBJECT_TO_MODEL
end

Instance Attribute Details

#graph_typeObject

Returns the value of attribute graph_type.



4
5
6
# File 'lib/graphql/models/backed_by_model.rb', line 4

def graph_type
  @graph_type
end

#model_typeObject

Returns the value of attribute model_type.



4
5
6
# File 'lib/graphql/models/backed_by_model.rb', line 4

def model_type
  @model_type
end

#object_to_model(value = nil) ⇒ Object

Returns the value of attribute object_to_model.



4
5
6
# File 'lib/graphql/models/backed_by_model.rb', line 4

def object_to_model
  @object_to_model
end

Instance Method Details

#attr(name, **options) ⇒ Object



23
24
25
# File 'lib/graphql/models/backed_by_model.rb', line 23

def attr(name, **options)
  DefinitionHelpers.define_attribute(graph_type, model_type, model_type, [], name, object_to_model, options)
end

#field(*args, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/graphql/models/backed_by_model.rb', line 43

def field(*args, &block)
  defined_field = GraphQL::Define::AssignObjectField.call(graph_type, *args, &block)

  DefinitionHelpers.(graph_type, defined_field.name, {
    macro: :field,
    macro_type: :custom,
    path: [],
    base_model_type: @model_type.to_s.classify.constantize,
    model_type: @model_type.to_s.classify.constantize,
    object_to_base_model: object_to_model
  })

  defined_field
end

#has_many_array(association, **options) ⇒ Object



39
40
41
# File 'lib/graphql/models/backed_by_model.rb', line 39

def has_many_array(association, **options)
  DefinitionHelpers.define_has_many_array(graph_type, model_type, model_type, [], association, object_to_model, options)
end

#has_many_connection(association, **options) ⇒ Object



35
36
37
# File 'lib/graphql/models/backed_by_model.rb', line 35

def has_many_connection(association, **options)
  DefinitionHelpers.define_has_many_connection(graph_type, model_type, model_type, [], association, object_to_model, options)
end

#has_one(association, **options) ⇒ Object



31
32
33
# File 'lib/graphql/models/backed_by_model.rb', line 31

def has_one(association, **options)
  DefinitionHelpers.define_has_one(graph_type, model_type, model_type, [], association, object_to_model, options)
end

#proxy_to(association, &block) ⇒ Object



27
28
29
# File 'lib/graphql/models/backed_by_model.rb', line 27

def proxy_to(association, &block)
  DefinitionHelpers.define_proxy(graph_type, model_type, model_type, [], association, object_to_model, &block)
end

#typesObject



14
15
16
# File 'lib/graphql/models/backed_by_model.rb', line 14

def types
  GraphQL::Define::TypeDefiner.instance
end