Class: ElasticGraph::GraphQL::Filtering::FieldPath

Inherits:
Data
  • Object
show all
Defined in:
lib/elastic_graph/graphql/filtering/field_path.rb

Overview

Tracks state related to field paths as we traverse our filtering data structure in order to translate it to its Elasticsearch/OpenSearch form.

Instances of this class are immutable–callers must use the provided APIs (‘+`, `counts_path`, `nested`) to get back new instances with state changes applied.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#from_parentObject (readonly)

Returns the value of attribute from_parent

Returns:

  • (Object)

    the current value of from_parent



19
20
21
# File 'lib/elastic_graph/graphql/filtering/field_path.rb', line 19

def from_parent
  @from_parent
end

#from_rootObject (readonly)

Returns the value of attribute from_root

Returns:

  • (Object)

    the current value of from_root



19
20
21
# File 'lib/elastic_graph/graphql/filtering/field_path.rb', line 19

def from_root
  @from_root
end

Class Method Details

.emptyObject

Builds an empty instance.



32
33
34
# File 'lib/elastic_graph/graphql/filtering/field_path.rb', line 32

def self.empty
  new([], [])
end

.of(parts) ⇒ Object



36
37
38
# File 'lib/elastic_graph/graphql/filtering/field_path.rb', line 36

def self.of(parts)
  new(parts, parts)
end

Instance Method Details

#+(other) ⇒ Object

Creates a new instance with ‘sub_path` appended.



46
47
48
# File 'lib/elastic_graph/graphql/filtering/field_path.rb', line 46

def +(other)
  FieldPath.new(from_root + [other], from_parent + [other])
end

#counts_pathObject

Converts the current paths to what they need to be to be able to query our hidden ‘__counts` field (which is a map containing the counts of elements of every list field on the document). The `__counts` field sits a the root of every document (for both an overall root document and a `nested` document). Here’s an example (which assumes ‘seasons` and `seasons.players` fields which are both `nested` and an `awards` field which is a list of strings). Given a filter like this:

filter: {any_satisfy: {players: {any_satisfy: {results: {awards: {count: {gt: 1}}}}}}}

…after processing the ‘awards` key, our `FieldPath` will be:

FieldPath.new([“seasons”, “players”, “results”, “awards”], [“results”, “awards”])

When we then reach the ‘count` sub field and `counts_path` is called on it, the following will be returned:

FieldPath.new([“seasons”, “players”, LIST_COUNTS_FIELD, “results|awards”], [LIST_COUNTS_FIELD, “results|awards”])

This gives us what we want:

  • The path from the root is ‘seasons.players.__counts.results|awards`.

  • The path from the (nested) parent is ‘__counts.results|awards`.

Note that our ‘__counts` field is a flat map which uses `|` (the `LIST_COUNTS_FIELD_PATH_KEY_SEPARATOR` character) to separate its parts (hence, it’s ‘results|awards` instead of `results.awards`).



72
73
74
75
76
77
# File 'lib/elastic_graph/graphql/filtering/field_path.rb', line 72

def counts_path
  from_root_to_parent_of_counts_field = from_root[0...-from_parent.size] # : ::Array[::String]
  counts_sub_field = [LIST_COUNTS_FIELD, from_parent.join(LIST_COUNTS_FIELD_PATH_KEY_SEPARATOR)]

  FieldPath.new(from_root_to_parent_of_counts_field + counts_sub_field, counts_sub_field)
end

#nestedObject

Used when we encounter a ‘nested` field to restart the `from_parent` path (while preserving the `from_root` path).



41
42
43
# File 'lib/elastic_graph/graphql/filtering/field_path.rb', line 41

def nested
  FieldPath.new(from_root, [])
end