Class: GraphQL::Models::BackedByModel
- Inherits:
-
Object
- Object
- GraphQL::Models::BackedByModel
- Defined in:
- lib/graphql/models/backed_by_model.rb
Constant Summary collapse
- DEFAULT_OBJECT_TO_MODEL =
-> (obj) { obj }
Instance Method Summary collapse
-
#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.
-
#detect_nulls(value = nil) ⇒ Object
Allows you to overide the automatic nullability detection.
- #field(*args, &block) ⇒ Object
- #has_many_array(association, name: association.to_s.camelize(:lower), nullable: nil, description: nil, deprecation_reason: nil, type: nil) ⇒ Object
- #has_many_connection(association, name: association.to_s.camelize(:lower), nullable: nil, description: nil, deprecation_reason: nil, **goco_options) ⇒ Object
- #has_one(association, name: association.to_s.camelize(:lower), nullable: nil, description: nil, deprecation_reason: nil) ⇒ Object
-
#initialize(graph_type, model_type, base_model_type: model_type, path: [], object_to_model: DEFAULT_OBJECT_TO_MODEL, detect_nulls: true) ⇒ BackedByModel
constructor
A new instance of BackedByModel.
- #object_to_model(value = nil) ⇒ Object
-
#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.
- #types ⇒ Object
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.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# 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) # Get the description from the column, if it's not provided. Doesn't work in Rails 4 :( unless description column = @model_type.columns_hash[attribute.to_s] description = column.comment if column&.respond_to?(:comment) end = { 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, , @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
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/graphql/models/backed_by_model.rb', line 106 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
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/graphql/models/backed_by_model.rb', line 92 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) = { 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, , @detect_nulls) end |
#has_many_connection(association, name: association.to_s.camelize(:lower), nullable: nil, description: nil, deprecation_reason: nil, **goco_options) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/graphql/models/backed_by_model.rb', line 79 def has_many_connection(association, name: association.to_s.camelize(:lower), nullable: nil, description: nil, deprecation_reason: nil, **) name = name.to_sym unless name.is_a?(Symbol) = .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, , @detect_nulls) end |
#has_one(association, name: association.to_s.camelize(:lower), nullable: nil, description: nil, deprecation_reason: nil) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/graphql/models/backed_by_model.rb', line 66 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) = { 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, , @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.
62 63 64 |
# File 'lib/graphql/models/backed_by_model.rb', line 62 def proxy_to(association, &block) DefinitionHelpers.define_proxy(@graph_type, @base_model_type, @model_type, @path, association, @object_to_model, @detect_nulls, &block) end |
#types ⇒ Object
19 20 21 |
# File 'lib/graphql/models/backed_by_model.rb', line 19 def types GraphQL::Define::TypeDefiner.instance end |