Skip to content

Commit f050d95

Browse files
committed
doc(Lazy) add yardoc for Lazy code
1 parent 36db37d commit f050d95

File tree

5 files changed

+43
-24
lines changed

5 files changed

+43
-24
lines changed

lib/graphql/execution/execute.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module GraphQL
22
module Execution
3+
# A valid execution strategy
34
class Execute
45
PROPAGATE_NULL = :__graphql_propagate_null__
56

@@ -25,7 +26,7 @@ def resolve_selection(object, current_type, irep_nodes, query_ctx, mutation: fal
2526
query = query_ctx.query
2627
own_selections = query.selections(irep_nodes, current_type)
2728

28-
selection_result = SelectionResult.new(type: current_type)
29+
selection_result = SelectionResult.new
2930

3031
own_selections.each do |name, child_irep_nodes|
3132
field = query.get_field(current_type, child_irep_nodes.first.definition_name)
@@ -89,9 +90,7 @@ def resolve_field(owner, irep_nodes, parent_type, field, object, query_ctx)
8990

9091
FieldResult.new(
9192
owner: owner,
92-
parent_type: parent_type,
9393
field: field,
94-
name: irep_node.name,
9594
value: result,
9695
)
9796
end

lib/graphql/execution/field_result.rb

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
11
module GraphQL
22
module Execution
3+
# This is one key-value pair in a GraphQL response.
34
class FieldResult
4-
attr_reader :value, :parent_type, :field, :name, :owner
5-
def initialize(parent_type:, field:, value:, name:, owner:)
6-
@parent_type = parent_type
5+
# @return [Any, Lazy] the GraphQL-ready response value, or a {Lazy} instance
6+
attr_reader :value
7+
8+
# @return [GraphQL::Field] The field which resolved this value
9+
attr_reader :field
10+
11+
# @return [SelectionResult] The result object that this field belongs to
12+
attr_reader :owner
13+
14+
def initialize(field:, value:, owner:)
715
@field = field
816
@owner = owner
9-
@name = name
1017
self.value = value
1118
end
1219

20+
# Set a new value for this field in the response.
21+
# It may be updated after resolving a {Lazy}.
22+
# If it is {Execute::PROPAGATE_NULL}, tell the owner to propagate null.
23+
# If the value is a {SelectionResult}, make a link with it, and if it's already null,
24+
# propagate the null as needed.
25+
# @param new_value [Any] The GraphQL-ready value
1326
def value=(new_value)
1427
if new_value.is_a?(SelectionResult)
1528
if new_value.invalid_null?
16-
new_value = new_value.invalid_null
29+
new_value = GraphQL::Execution::Execute::PROPAGATE_NULL
1730
else
1831
new_value.owner = self
1932
end
2033
end
2134

2235
if new_value == GraphQL::Execution::Execute::PROPAGATE_NULL
2336
if field.type.kind.non_null?
24-
@owner.propagate_null(@name, new_value)
37+
@owner.propagate_null
2538
else
2639
@value = nil
2740
end
@@ -31,7 +44,7 @@ def value=(new_value)
3144
end
3245

3346
def inspect
34-
"#<FieldResult #{name.inspect} => #{value.inspect} (#{field.type})>"
47+
"#<FieldResult #{value.inspect} (#{field.type})>"
3548
end
3649
end
3750
end

lib/graphql/execution/lazy/lazy_method_map.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
module GraphQL
22
module Execution
33
class Lazy
4+
# {GraphQL::Schema} uses this to match returned values to lazy resolution methods.
5+
# Methods may be registered for classes, they apply to its subclasses also.
6+
# The result of this lookup is cached for future resolutions.
47
class LazyMethodMap
58
def initialize
69
@storage = {}

lib/graphql/execution/lazy/resolve.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ module Execution
33
class Lazy
44
# Helpers for dealing with data structures containing {Lazy} instances
55
module Resolve
6+
# Mutate `value`, replacing {Lazy} instances in place with their resolved values
7+
# @return [void]
68
def self.resolve(value)
79
lazies = resolve_in_place(value)
810
deep_sync(lazies)
911
end
1012

11-
# Mutate `value`, replacing {Lazy} instances in place with their resolved values
12-
# @return [void]
1313
def self.resolve_in_place(value)
1414
lazies = []
1515

lib/graphql/execution/selection_result.rb

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
module GraphQL
22
module Execution
3+
# A set of key-value pairs suitable for a GraphQL response.
34
class SelectionResult
4-
def initialize(type:)
5-
@type = type
5+
def initialize
66
@storage = {}
77
@owner = nil
88
@invalid_null = false
99
end
1010

11+
# @param key [String] The name for this value in the result
12+
# @param field_result [FieldResult] The result for this field
1113
def set(key, field_result)
1214
@storage[key] = field_result
1315
end
1416

17+
# @param key [String] The name of an already-defined result
18+
# @return [FieldResult] The result for this field
1519
def fetch(key)
1620
@storage.fetch(key)
1721
end
1822

23+
# Visit each key-result pair in this result
1924
def each
2025
@storage.each do |key, field_res|
2126
yield(key, field_res)
2227
end
2328
end
2429

30+
# @return [Hash] A plain Hash representation of this result
2531
def to_h
2632
if @invalid_null
2733
nil
@@ -30,29 +36,27 @@ def to_h
3036
end
3137
end
3238

33-
def propagate_null(key, value)
39+
# A field has been unexpectedly nullified.
40+
# Tell the owner {FieldResult} if it is present.
41+
# Record {#invalid_null} in case an owner is added later.
42+
def propagate_null
3443
if @owner
35-
@owner.value = value
44+
@owner.value = GraphQL::Execution::Execute::PROPAGATE_NULL
3645
end
37-
@invalid_null = value
46+
@invalid_null = true
3847
end
3948

49+
# @return [Boolean] True if this selection has been nullified by a null child
4050
def invalid_null?
4151
@invalid_null
4252
end
4353

44-
def invalid_null
45-
@invalid_null
46-
end
47-
54+
# @param field_result [FieldResult] The field that this selection belongs to (used for propagating nulls)
4855
def owner=(field_result)
4956
if @owner
5057
raise("Can't change owners of SelectionResult")
5158
else
5259
@owner = field_result
53-
if @invalid_null
54-
@owner.value = @invalid_null
55-
end
5660
end
5761
end
5862

0 commit comments

Comments
 (0)