Module: GraphQL::Models
- Defined in:
- lib/graphql/models/reflection.rb,
lib/graphql/activerecord.rb,
lib/graphql/models/definer.rb,
lib/graphql/models/helpers.rb,
lib/graphql/models/mutator.rb,
lib/graphql/models/version.rb,
lib/graphql/models/database_types.rb,
lib/graphql/models/backed_by_model.rb,
lib/graphql/models/relation_loader.rb,
lib/graphql/models/attribute_loader.rb,
lib/graphql/models/definition_helpers.rb,
lib/graphql/models/mutation_field_map.rb,
lib/graphql/models/relation_load_request.rb,
lib/graphql/models/active_record_extension.rb,
lib/graphql/models/association_load_request.rb,
lib/graphql/models/mutation_helpers/validation.rb,
lib/graphql/models/promise_relation_connection.rb,
lib/graphql/models/definition_helpers/attributes.rb,
lib/graphql/models/mutation_helpers/apply_changes.rb,
lib/graphql/models/mutation_helpers/authorization.rb,
lib/graphql/models/definition_helpers/associations.rb,
lib/graphql/models/mutation_helpers/validation_error.rb,
lib/graphql/models/mutation_helpers/print_input_fields.rb
Overview
Exposes utility methods for getting metadata out of active record models
Defined Under Namespace
Modules: ActiveRecordExtension, DatabaseTypes, DefinitionHelpers, HashCombiner, Helpers, MutationHelpers, Reflection Classes: AssociationLoadRequest, AttributeLoader, BackedByModel, Definer, Middleware, MutationFieldMap, Mutator, MutatorDefinition, PromiseRelationConnection, RelationLoadRequest, RelationLoader
Constant Summary collapse
- VERSION =
"0.10.0"
Class Attribute Summary collapse
-
.authorize ⇒ Object
Returns the value of attribute authorize.
-
.id_for_model ⇒ Object
Returns the value of attribute id_for_model.
-
.model_from_id ⇒ Object
Returns the value of attribute model_from_id.
-
.model_to_graphql_type ⇒ Object
Returns the value of attribute model_to_graphql_type.
Class Method Summary collapse
- .authorize!(context, model, action) ⇒ Object
- .define_mutator(definer, model_type, null_behavior:, &block) ⇒ Object
- .field_info(graph_type, field_name) ⇒ Object
- .get_graphql_type(model_class) ⇒ Object
- .get_graphql_type!(model_class) ⇒ Object
-
.load_association(starting_model, path, context) ⇒ Object
Returns a promise that will traverse the associations and resolve to the model at the end of the path.
- .load_relation(relation, fast_query: false) ⇒ Object
Class Attribute Details
.authorize ⇒ Object
Returns the value of attribute authorize.
41 42 43 |
# File 'lib/graphql/activerecord.rb', line 41 def end |
.id_for_model ⇒ Object
Returns the value of attribute id_for_model.
41 42 43 |
# File 'lib/graphql/activerecord.rb', line 41 def id_for_model @id_for_model end |
.model_from_id ⇒ Object
Returns the value of attribute model_from_id.
41 42 43 |
# File 'lib/graphql/activerecord.rb', line 41 def model_from_id @model_from_id end |
.model_to_graphql_type ⇒ Object
Returns the value of attribute model_to_graphql_type.
41 42 43 |
# File 'lib/graphql/activerecord.rb', line 41 def model_to_graphql_type @model_to_graphql_type end |
Class Method Details
.authorize!(context, model, action) ⇒ Object
71 72 73 |
# File 'lib/graphql/activerecord.rb', line 71 def self.(context, model, action) .call(context, model, action) end |
.define_mutator(definer, model_type, null_behavior:, &block) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/graphql/activerecord.rb', line 75 def self.define_mutator(definer, model_type, null_behavior:, &block) # HACK: To get the name of the mutation, to avoid possible collisions with other type names prefix = definer.instance_variable_get(:@target).name mutator_definition = MutatorDefinition.new(model_type, null_behavior: null_behavior) mutator_definition.field_map.instance_exec(&block) MutationHelpers.print_input_fields(mutator_definition.field_map, definer, "#{prefix}Input") mutator_definition end |
.field_info(graph_type, field_name) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/graphql/activerecord.rb', line 62 def self.field_info(graph_type, field_name) field_name = field_name.to_s = graph_type.instance_variable_get(:@field_metadata) return nil unless [field_name] end |
.get_graphql_type(model_class) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/graphql/activerecord.rb', line 85 def self.get_graphql_type(model_class) model_class = model_class.constantize if model_class.is_a?(String) if model_to_graphql_type model_to_graphql_type[model_class] else "#{model_class.name}Type".safe_constantize end end |
.get_graphql_type!(model_class) ⇒ Object
95 96 97 98 99 |
# File 'lib/graphql/activerecord.rb', line 95 def self.get_graphql_type!(model_class) type = get_graphql_type(model_class) fail RuntimeError, "Could not locate GraphQL type for model #{model_class}" if type.nil? type end |
.load_association(starting_model, path, context) ⇒ Object
Returns a promise that will traverse the associations and resolve to the model at the end of the path. You can use this to access associated models inside custom field resolvers, without losing optimization benefits.
47 48 49 50 |
# File 'lib/graphql/activerecord.rb', line 47 def self.load_association(starting_model, path, context) path = Array.wrap(path) GraphQL::Models::DefinitionHelpers.load_and_traverse(starting_model, path, context) end |
.load_relation(relation, fast_query: false) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/graphql/activerecord.rb', line 52 def self.load_relation(relation, fast_query: false) if fast_query request = AttributeLoader::Request.new(relation.where_values_hash, Helpers.orders_to_sql(relation.orders)) AttributeLoader.for(relation.klass).load(request) else request = RelationLoadRequest.new(relation) request.load end end |