Class: GraphQL::Execution::SelectionResult

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/execution/selection_result.rb

Overview

A set of key-value pairs suitable for a GraphQL response.

Instance Method Summary collapse

Constructor Details

#initializeSelectionResult

Returns a new instance of SelectionResult.



6
7
8
9
10
# File 'lib/graphql/execution/selection_result.rb', line 6

def initialize
  @storage = {}
  @owner = nil
  @invalid_null = false
end

Instance Method Details

#eachObject

Visit each key-result pair in this result



25
26
27
28
29
# File 'lib/graphql/execution/selection_result.rb', line 25

def each
  @storage.each do |key, field_res|
    yield(key, field_res)
  end
end

#fetch(key) ⇒ FieldResult

Returns The result for this field.

Parameters:

  • key (String)

    The name of an already-defined result

Returns:



20
21
22
# File 'lib/graphql/execution/selection_result.rb', line 20

def fetch(key)
  @storage.fetch(key)
end

#invalid_null?Boolean

Returns True if this selection has been nullified by a null child.

Returns:

  • (Boolean)

    True if this selection has been nullified by a null child



51
52
53
# File 'lib/graphql/execution/selection_result.rb', line 51

def invalid_null?
  @invalid_null
end

#owner=(field_result) ⇒ Object

Parameters:

  • field_result (FieldResult)

    The field that this selection belongs to (used for propagating nulls)



56
57
58
59
60
61
62
# File 'lib/graphql/execution/selection_result.rb', line 56

def owner=(field_result)
  if @owner
    raise("Can't change owners of SelectionResult")
  else
    @owner = field_result
  end
end

#propagate_nullObject

A field has been unexpectedly nullified. Tell the owner FieldResult if it is present. Record #invalid_null in case an owner is added later.



43
44
45
46
47
48
# File 'lib/graphql/execution/selection_result.rb', line 43

def propagate_null
  if @owner
    @owner.value = GraphQL::Execution::Execute::PROPAGATE_NULL
  end
  @invalid_null = true
end

#set(key, field_result) ⇒ Object

Parameters:

  • key (String)

    The name for this value in the result

  • field_result (FieldResult)

    The result for this field



14
15
16
# File 'lib/graphql/execution/selection_result.rb', line 14

def set(key, field_result)
  @storage[key] = field_result
end

#to_hHash

Returns A plain Hash representation of this result.

Returns:

  • (Hash)

    A plain Hash representation of this result



32
33
34
35
36
37
38
# File 'lib/graphql/execution/selection_result.rb', line 32

def to_h
  if @invalid_null
    nil
  else
    flatten(self)
  end
end