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, Instrumentation, MutationFieldMap, Mutator, MutatorDefinition, PromiseRelationConnection, RelationLoadRequest, RelationLoader

Constant Summary collapse

VERSION =
"0.13.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.authorizeObject

Returns the value of attribute authorize.



42
43
44
# File 'lib/graphql/activerecord.rb', line 42

def authorize
  @authorize
end

.id_for_modelObject

Returns the value of attribute id_for_model.



42
43
44
# File 'lib/graphql/activerecord.rb', line 42

def id_for_model
  @id_for_model
end

.legacy_nullsObject

Returns the value of attribute legacy_nulls.



43
44
45
# File 'lib/graphql/activerecord.rb', line 43

def legacy_nulls
  @legacy_nulls
end

.model_from_idObject

Returns the value of attribute model_from_id.



42
43
44
# File 'lib/graphql/activerecord.rb', line 42

def model_from_id
  @model_from_id
end

.model_to_graphql_typeObject

Returns the value of attribute model_to_graphql_type.



42
43
44
# File 'lib/graphql/activerecord.rb', line 42

def model_to_graphql_type
  @model_to_graphql_type
end

.unknown_scalarObject

Returns the value of attribute unknown_scalar.



42
43
44
# File 'lib/graphql/activerecord.rb', line 42

def unknown_scalar
  @unknown_scalar
end

Class Method Details

.authorize!(context, model, action) ⇒ Object



73
74
75
# File 'lib/graphql/activerecord.rb', line 73

def self.authorize!(context, model, action)
  authorize.call(context, model, action)
end

.define_mutator(definer, model_type, null_behavior: :leave_unchanged, legacy_nulls: GraphQL::Models.legacy_nulls, &block) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/graphql/activerecord.rb', line 77

def self.define_mutator(definer, model_type, null_behavior: :leave_unchanged, legacy_nulls: GraphQL::Models.legacy_nulls, &block)
  legacy_nulls ||= false

  # 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, legacy_nulls: legacy_nulls)
  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



64
65
66
67
68
69
70
71
# File 'lib/graphql/activerecord.rb', line 64

def self.field_info(graph_type, field_name)
  field_name = field_name.to_s

  meta = graph_type.instance_variable_get(:@field_metadata)
  return nil unless meta

  meta[field_name]
end

.get_graphql_type(model_class) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/graphql/activerecord.rb', line 89

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



99
100
101
102
103
# File 'lib/graphql/activerecord.rb', line 99

def self.get_graphql_type!(model_class)
  type = get_graphql_type(model_class)
  raise "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.



49
50
51
52
# File 'lib/graphql/activerecord.rb', line 49

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



54
55
56
57
58
59
60
61
62
# File 'lib/graphql/activerecord.rb', line 54

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