Skip to content

Commit df8ffad

Browse files
committed
Exec-next: Implement GraphQL::Current.field
1 parent af75b52 commit df8ffad

6 files changed

Lines changed: 24 additions & 5 deletions

File tree

lib/graphql/current.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ def self.operation_name
4141
# @see GraphQL::Field#path for a string identifying this field
4242
# @return [GraphQL::Field, nil] The currently-running field, if there is one.
4343
def self.field
44-
Fiber[:__graphql_runtime_info]&.values&.first&.current_field
44+
if (interpreter_info = Fiber[:__graphql_runtime_info])
45+
interpreter_info.values&.first&.current_field
46+
elsif (field = Fiber[:__graphql_current_field])
47+
field
48+
else
49+
nil
50+
end
4551
end
4652

4753
# @return [Class, nil] The currently-running {Dataloader::Source} class, if there is one.

lib/graphql/execution/field_resolve_step.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def append_selection(ast_node)
5050

5151
def value
5252
query = @selections_step.query
53+
set_current_field
5354
query.current_trace.begin_execute_field(@field_definition, @arguments, @field_results, query)
5455
sync(@field_results)
5556
query.current_trace.end_execute_field(@field_definition, @arguments, @field_results, query, @field_results)
@@ -76,6 +77,7 @@ def sync(lazy)
7677
end
7778

7879
def call
80+
set_current_field if @field_definition
7981
if @enqueued_authorization
8082
enqueue_next_steps
8183
elsif @finish_extension_idx
@@ -121,6 +123,7 @@ def build_arguments
121123
query = @selections_step.query
122124
field_name = @ast_node.name
123125
@field_definition = query.types.field(@parent_type, field_name) || raise(GraphQL::Error, "No field definition found for #{@parent_type.to_type_signature}.#{ast_node.name} (at #{@ast_node.position})")
126+
set_current_field
124127
@arguments, errors = @runner.input_values[query].argument_values(@field_definition, @ast_node.arguments, self) # rubocop:disable Development/ContextIsPassedCop
125128
if errors
126129
build_errors_result(errors, nil)
@@ -552,6 +555,10 @@ def add_non_null_error(is_from_array)
552555
@runner.schema.type_error(err, @selections_step.query.context)
553556
end
554557

558+
def set_current_field
559+
Fiber[:__graphql_current_field] = @field_definition
560+
end
561+
555562
private
556563

557564
def build_graphql_result(graphql_result, key, field_result, return_type, is_nn, is_list, is_from_array) # rubocop:disable Metrics/ParameterLists

lib/graphql/execution/load_argument_step.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def initialize(field_resolve_step:, arguments:, load_receiver:, argument_value:,
1414
end
1515

1616
def value
17+
@field_resolve_step.set_current_field
1718
schema = @field_resolve_step.runner.schema
1819
@loaded_value = schema.sync_lazy(@loaded_value)
1920
assign_value
@@ -37,6 +38,7 @@ def value
3738
end
3839

3940
def call
41+
@field_resolve_step.set_current_field
4042
context = @field_resolve_step.selections_step.query.context
4143
@loaded_value = begin
4244
@load_receiver.load_and_authorize_application_object(@argument_definition, @argument_value, context)

lib/graphql/execution/prepare_object_step.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def initialize(object:, runner:, graphql_result:, key:, is_non_null:, field_reso
1919
end
2020

2121
def value
22+
@field_resolve_step.set_current_field
2223
if @authorized_value
2324
query = @field_resolve_step.selections_step.query
2425
query.current_trace.begin_authorized(@resolved_type, @object, query.context)
@@ -39,6 +40,7 @@ def value
3940
end
4041

4142
def call
43+
@field_resolve_step.set_current_field
4244
case @next_step
4345
when :resolve_type
4446
static_type = @field_resolve_step.static_type

spec/graphql/current_spec.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ def fetch(names)
2424
end
2525
end
2626
class Thing < GraphQL::Schema::Object
27-
field :name, String
27+
field :name, String, resolve_static: :get_name
2828

29-
def name
29+
def self.get_name(context)
3030
context[:current_field] << GraphQL::Current.field.path
3131
context.dataload(ThingSource, context, "thing")
3232
end
33+
34+
def name
35+
self.class.get_name(context)
36+
end
3337
end
3438
class Query < GraphQL::Schema::Object
3539
field :thing, Thing, resolve_static: true
@@ -49,7 +53,6 @@ def thing
4953
end
5054

5155
it "returns execution information" do
52-
exec_next_TODO("not implemented yet")
5356
ctx = {
5457
current_field: [],
5558
current_source: [],

spec/integration/rails/query_logs_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ def exec_query(str)
103103
assert_equal "Fork", res["data"]["someThing"]["otherThing"]["name"]
104104
# These can appear in different orders in the SQL comment:
105105
assert_includes log, "current_graphql_operation:OtherThingName"
106-
exec_next_TODO "Exec-next doesn't broadcast this info yet but it should"
107106
assert_includes log, "current_graphql_field:Query.someThing"
108107
assert_includes log, "current_graphql_field:Thing.otherThing"
109108
end

0 commit comments

Comments
 (0)