Module: GraphQL::Models::ObjectType

Defined in:
lib/graphql/models/object_type.rb

Constant Summary collapse

DEFAULT_OBJECT_TO_MODEL =
-> (object) { object }

Class Method Summary collapse

Class Method Details

.attr(graph_type, name, **options, &block) ⇒ Object



69
70
71
72
73
# File 'lib/graphql/models/object_type.rb', line 69

def attr(graph_type, name, **options, &block)
  ensure_has_model_type(graph_type, __method__)
  object_to_model = graph_type.instance_variable_get(:@unscoped_object_to_model) || DEFAULT_OBJECT_TO_MODEL
  DefinitionHelpers.define_attribute(graph_type, resolve_model_type(graph_type), resolve_model_type(graph_type), [], name, object_to_model, options, &block)
end

.backed_by_model(graph_type, model_type, &block) ⇒ Object



93
94
95
96
97
98
# File 'lib/graphql/models/object_type.rb', line 93

def backed_by_model(graph_type, model_type, &block)
  model_type = model_type.to_s.classify.constantize unless model_type.is_a?(Class)

  backer = GraphQL::Models::BackedByModel.new(graph_type, model_type)
  backer.instance_exec(&block)
end

.has_many_array(graph_type, association, **options) ⇒ Object



87
88
89
90
91
# File 'lib/graphql/models/object_type.rb', line 87

def has_many_array(graph_type, association, **options)
  ensure_has_model_type(graph_type, __method__)
  object_to_model = graph_type.instance_variable_get(:@unscoped_object_to_model) || DEFAULT_OBJECT_TO_MODEL
  DefinitionHelpers.define_has_many_array(graph_type, resolve_model_type(graph_type), resolve_model_type(graph_type), [], association, object_to_model, options)
end

.has_many_connection(graph_type, association, **options) ⇒ Object



81
82
83
84
85
# File 'lib/graphql/models/object_type.rb', line 81

def has_many_connection(graph_type, association, **options)
  ensure_has_model_type(graph_type, __method__)
  object_to_model = graph_type.instance_variable_get(:@unscoped_object_to_model) || DEFAULT_OBJECT_TO_MODEL
  DefinitionHelpers.define_has_many_connection(graph_type, resolve_model_type(graph_type), resolve_model_type(graph_type), [], association, object_to_model, options)
end

.has_one(graph_type, association, **options) ⇒ Object



75
76
77
78
79
# File 'lib/graphql/models/object_type.rb', line 75

def has_one(graph_type, association, **options)
  ensure_has_model_type(graph_type, __method__)
  object_to_model = graph_type.instance_variable_get(:@unscoped_object_to_model) || DEFAULT_OBJECT_TO_MODEL
  DefinitionHelpers.define_has_one(graph_type, resolve_model_type(graph_type), resolve_model_type(graph_type), [], association, object_to_model, options)
end

.model_type(graph_type, model_type) ⇒ Object



11
12
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
52
53
54
55
56
57
58
59
60
61
# File 'lib/graphql/models/object_type.rb', line 11

def model_type(graph_type, model_type)
  model_type = model_type.to_s.classify.constantize unless model_type.is_a?(Class)

  object_to_model = -> (object) do
    model_proc = graph_type.instance_variable_get(:@unscoped_object_to_model)
    if model_proc
      model_proc.call(object)
    else
      DEFAULT_OBJECT_TO_MODEL.call(object)
    end
  end

  graph_type.instance_variable_set(:@unscoped_model_type, model_type)

  graph_type.fields['id'] = GraphQL::Field.define do
    name 'id'
    type !types.ID
    resolve -> (object, args, context) { object.gid }
  end

  if GraphQL::Models.node_interface_proc
    node_interface = GraphQL::Models.node_interface_proc.call
    graph_type.interfaces = [*graph_type.interfaces, node_interface].uniq
  end

  graph_type.fields['rid'] = GraphQL::Field.define do
    name 'rid'
    type !types.String
    resolve -> (object, args, context) do
      model = object_to_model.call(object)
      model.id
    end
  end

  graph_type.fields['rtype'] = GraphQL::Field.define do
    name 'rtype'
    type !types.String
    resolve -> (object, args, context) do
      model = object_to_model.call(object)
      model.class.name
    end
  end

  if model_type.columns.detect { |c| c.name == 'created_at'}
    DefinitionHelpers.define_attribute(graph_type, model_type, model_type, [], :created_at, object_to_model, {})
  end

  if model_type.columns.detect { |c| c.name == 'updated_at'}
    DefinitionHelpers.define_attribute(graph_type, model_type, model_type, [], :updated_at, object_to_model, {})
  end
end

.object_to_model(graph_type, model_proc) ⇒ Object



7
8
9
# File 'lib/graphql/models/object_type.rb', line 7

def object_to_model(graph_type, model_proc)
  graph_type.instance_variable_set(:@unscoped_object_to_model, model_proc)
end

.proxy_to(graph_type, association, &block) ⇒ Object



63
64
65
66
67
# File 'lib/graphql/models/object_type.rb', line 63

def proxy_to(graph_type, association, &block)
  ensure_has_model_type(graph_type, __method__)
  object_to_model = graph_type.instance_variable_get(:@unscoped_object_to_model) || DEFAULT_OBJECT_TO_MODEL
  DefinitionHelpers.define_proxy(graph_type, resolve_model_type(graph_type), resolve_model_type(graph_type), [], association, object_to_model, &block)
end