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 Method Summary collapse

Constructor Details

#initialize(graph_type, model_type, base_model_type: model_type, path: [], object_to_model: DEFAULT_OBJECT_TO_MODEL, detect_nulls: true) ⇒ BackedByModel

Returns a new instance of BackedByModel.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/graphql/models/backed_by_model.rb', line 7

def initialize(graph_type, model_type, base_model_type: model_type, path: [], object_to_model: DEFAULT_OBJECT_TO_MODEL, detect_nulls: true)
  model_type = model_type.to_s.classify.constantize unless model_type.is_a?(Class)
  base_model_type = base_model_type.to_s.classify.constantize unless model_type.is_a?(Class)

  @graph_type = graph_type
  @model_type = model_type
  @object_to_model = object_to_model
  @base_model_type = base_model_type
  @path = path
  @detect_nulls = detect_nulls
end

Instance Method Details

#attr(attribute, name: attribute.to_s.camelize(:lower), nullable: nil, description: nil, deprecation_reason: nil, &block) ⇒ Object

Adds a field to the graph type that is resolved to an attribute on the model.

Parameters:

  • attribute

    Symbol with the name of the attribute on the model

  • description (defaults to: nil)

    Description for the field

  • name (defaults to: attribute.to_s.camelize(:lower))

    Name of the field to use. By default, the attribute name is camelized.

  • nullable (defaults to: nil)

    Set to false to force the field to be non-null. By default, nullability is automatically detected.

  • deprecation_reason (defaults to: nil)

    Sets the deprecation reason on the field.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/graphql/models/backed_by_model.rb', line 41

def attr(attribute, name: attribute.to_s.camelize(:lower), nullable: nil, description: nil, deprecation_reason: nil, &block)
  name = name.to_sym unless name.is_a?(Symbol)

  options = {
    name: name,
    nullable: nullable,
    description: description,
    deprecation_reason: deprecation_reason,
  }

  DefinitionHelpers.define_attribute(@graph_type, @base_model_type, @model_type, @path, attribute, @object_to_model, options, @detect_nulls, &block)
end

#detect_nulls(value = nil) ⇒ Object

Allows you to overide the automatic nullability detection. By default, nulls are detected. However, attributes inside of a proxy_to block are assumed to be nullable, unless the association itself has a presence validator.



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

def detect_nulls(value = nil)
  @detect_nulls = value if !value.nil?
  @detect_nulls
end

#field(*args, &block) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/graphql/models/backed_by_model.rb', line 100

def field(*args, &block)
  defined_field = GraphQL::Define::AssignObjectField.call(@graph_type, *args, &block)
  name = defined_field.name
  name = name.to_sym unless name.is_a?(Symbol)

  DefinitionHelpers.(@graph_type, name, {
    macro: :field,
    macro_type: :custom,
    path: @path,
    base_model_type: @base_model_type,
    model_type: @model_type,
    object_to_base_model: @object_to_model,
  })

  defined_field
end

#has_many_array(association, name: association.to_s.camelize(:lower), nullable: nil, description: nil, deprecation_reason: nil, type: nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/graphql/models/backed_by_model.rb', line 86

def has_many_array(association, name: association.to_s.camelize(:lower), nullable: nil, description: nil, deprecation_reason: nil, type: nil)
  name = name.to_sym unless name.is_a?(Symbol)

  options = {
    name: name,
    type: type,
    nullable: nullable,
    description: description,
    deprecation_reason: deprecation_reason,
  }

  DefinitionHelpers.define_has_many_array(@graph_type, @base_model_type, @model_type, @path, association, @object_to_model, options, @detect_nulls)
end

#has_many_connection(association, name: association.to_s.camelize(:lower), nullable: nil, description: nil, deprecation_reason: nil, **goco_options) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/graphql/models/backed_by_model.rb', line 73

def has_many_connection(association, name: association.to_s.camelize(:lower), nullable: nil, description: nil, deprecation_reason: nil, **goco_options)
  name = name.to_sym unless name.is_a?(Symbol)

  options = goco_options.merge({
    name: name,
    nullable: nullable,
    description: description,
    deprecation_reason: deprecation_reason,
  })

  DefinitionHelpers.define_has_many_connection(@graph_type, @base_model_type, @model_type, @path, association, @object_to_model, options, @detect_nulls)
end

#has_one(association, name: association.to_s.camelize(:lower), nullable: nil, description: nil, deprecation_reason: nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/graphql/models/backed_by_model.rb', line 60

def has_one(association, name: association.to_s.camelize(:lower), nullable: nil, description: nil, deprecation_reason: nil)
  name = name.to_sym unless name.is_a?(Symbol)

  options = {
    name: name,
    nullable: nullable,
    description: description,
    deprecation_reason: deprecation_reason,
  }

  DefinitionHelpers.define_has_one(@graph_type, @base_model_type, @model_type, @path, association, @object_to_model, options, @detect_nulls)
end

#object_to_model(value = nil) ⇒ Object



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

def object_to_model(value = nil)
  @object_to_model = value if value
  @object_to_model
end

#proxy_to(association, &block) ⇒ Object

Flattens an associated model into the graph type, allowing to you adds its attributes as if they existed on the parent model.

Parameters:

  • association

    Name of the association to use. Polymorphic belongs_to associations are not supported.



56
57
58
# File 'lib/graphql/models/backed_by_model.rb', line 56

def proxy_to(association, &block)
  DefinitionHelpers.define_proxy(@graph_type, @base_model_type, @model_type, @path, association, @object_to_model, @detect_nulls, &block)
end

#typesObject



19
20
21
# File 'lib/graphql/models/backed_by_model.rb', line 19

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