Class: ElasticGraph::SchemaDefinition::Indexing::RelationshipResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_graph/schema_definition/indexing/relationship_resolver.rb

Instance Method Summary collapse

Constructor Details

#initialize(schema_def_state:, object_type:, relationship_name:, sourced_fields:, field_path_resolver:) ⇒ RelationshipResolver

Returns a new instance of RelationshipResolver.



14
15
16
17
18
19
20
# File 'lib/elastic_graph/schema_definition/indexing/relationship_resolver.rb', line 14

def initialize(schema_def_state:, object_type:, relationship_name:, sourced_fields:, field_path_resolver:)
  @schema_def_state = schema_def_state
  @object_type = object_type
  @relationship_name = relationship_name
  @sourced_fields = sourced_fields
  @field_path_resolver = field_path_resolver
end

Instance Method Details

#resolveObject



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
# File 'lib/elastic_graph/schema_definition/indexing/relationship_resolver.rb', line 22

def resolve
  relation_field = object_type.graphql_fields_by_name[relationship_name]

  if relation_field.nil?
    [nil, "#{relationship_error_prefix} is not defined. Is it misspelled?"]
  elsif (relationship = relation_field.relationship).nil?
    [nil, "#{relationship_error_prefix} is not a relationship. It must be defined using `relates_to_one` or `relates_to_many`."]
  elsif (related_type = schema_def_state.object_types_by_name[relationship.related_type.unwrap_non_null.name]).nil?
    issue =
      if schema_def_state.types_by_name.key?(relationship.related_type.fully_unwrapped.name)
        "references a type which is not an object type: `#{relationship.related_type.name}`. Only object types can be used in relations."
      else
        "references an unknown type: `#{relationship.related_type.name}`. Is it misspelled?"
      end

    [nil, "#{relationship_error_prefix} #{issue}"]
  elsif !related_type.indexed?
    [nil, "#{relationship_error_prefix} references a type which is not indexed: `#{related_type.name}`. Only indexed types can be used in relations."]
  else
     = relation_field..relation # : SchemaArtifacts::RuntimeMetadata::Relation
    foreign_key_parent_type = (.direction == :in) ? related_type : object_type

    if (foreign_key_error = validate_foreign_key(foreign_key_parent_type, ))
      [nil, foreign_key_error]
    else
      [ResolvedRelationship.new(relationship_name, relation_field, relationship, related_type, ), nil]
    end
  end
end