Skip to content

Commit

Permalink
Merge pull request #5096 from rmosolgo/testing-helpers-context-curren…
Browse files Browse the repository at this point in the history
…t-field

Set context[:current_field] in testing helpers
  • Loading branch information
rmosolgo authored Sep 10, 2024
2 parents 2c10917 + bb73923 commit a85669b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/graphql/testing/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def run_graphql_field(schema, field_path, object, arguments: {}, context: {}, as
visible_field = dummy_query.types.field(object_type, field_name) # rubocop:disable Development/ContextIsPassedCop
if visible_field
dummy_query.context.dataloader.run_isolated {
query_context[:current_field] = visible_field
field_args = visible_field.coerce_arguments(graphql_result, arguments, query_context)
field_args = schema.sync_lazy(field_args)
if visible_field.extras.any?
Expand Down
7 changes: 7 additions & 0 deletions spec/graphql/testing/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ def authorized?(obj, args, ctx)
ctx[:current_user]&.admin?
end
end

field :current_field, String

def current_field
context[:current_field].path
end
end

class Query < GraphQL::Schema::Object
Expand Down Expand Up @@ -143,6 +149,7 @@ def self.resolve_type(abs_t, obj, ctx)
with_resolution_context(AssertionsSchema, object: { name: "Foo" }, type: "Student", context: { admin_for: ["Foo"] }) do |rc|
rc.run_graphql_field("name")
rc.run_graphql_field("isAdminFor")
assert_equal "Student.currentField", rc.run_graphql_field("currentField")
end
end

Expand Down

0 comments on commit a85669b

Please sign in to comment.