Skip to content

Commit

Permalink
Merge pull request #4930 from rmosolgo/run-graphql-field-extras-support
Browse files Browse the repository at this point in the history
Support extras: [:lookahead, :ast_node] in run_graphql_field
  • Loading branch information
rmosolgo committed Apr 22, 2024
2 parents 2f46e14 + e68025a commit cb3f017
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
24 changes: 23 additions & 1 deletion lib/graphql/testing/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def initialize(type_name:, field_name:)
end
end

def run_graphql_field(schema, field_path, object, arguments: {}, context: {})
def run_graphql_field(schema, field_path, object, arguments: {}, context: {}, ast_node: nil, lookahead: nil)
type_name, *field_names = field_path.split(".")
dummy_query = GraphQL::Query.new(schema, "{ __typename }", context: context)
query_context = dummy_query.context
Expand All @@ -57,6 +57,28 @@ def run_graphql_field(schema, field_path, object, arguments: {}, context: {})
dummy_query.context.dataloader.run_isolated {
field_args = visible_field.coerce_arguments(graphql_result, arguments, query_context)
field_args = schema.sync_lazy(field_args)
if visible_field.extras.any?
extra_args = {}
visible_field.extras.each do |extra|
extra_args[extra] = case extra
when :ast_node
ast_node ||= GraphQL::Language::Nodes::Field.new(name: visible_field.graphql_name)
when :lookahead
lookahead ||= begin
ast_node ||= GraphQL::Language::Nodes::Field.new(name: visible_field.graphql_name)
Execution::Lookahead.new(
query: dummy_query,
ast_nodes: [ast_node],
field: visible_field,
)
end
else
raise ArgumentError, "This extra isn't supported in `run_graphql_field` yet: `#{extra.inspect}`. Open an issue on GitHub to request it: https://github.com/rmosolgo/graphql-ruby/issues/new"
end
end

field_args = field_args.merge_extras(extra_args)
end
graphql_result = visible_field.resolve(graphql_result, field_args.keyword_arguments, query_context)
graphql_result = schema.sync_lazy(graphql_result)
}
Expand Down
24 changes: 20 additions & 4 deletions spec/graphql/testing/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ def self.authorized?(object, context)
context.errors.empty?
end

field :name, String do
field :name, String, extras: [:ast_node] do
argument :full_name, Boolean, required: false
argument :prefix, String, required: false, default_value: "Mc", prepare: ->(val, ctx) { -> { val.capitalize } }
end

def name(full_name: nil, prefix: nil)
def name(full_name: nil, prefix: nil, ast_node:)
name = object[:name]
if full_name
"#{name} #{prefix}#{name}"
"#{name} #{ast_node.alias ? "\"#{ast_node.alias}\" " : ""}#{prefix}#{name}"
else
name
end
Expand All @@ -65,7 +65,7 @@ def after_resolve(value:, **rest)
end
end

field :upcased_name, String, extensions: [Upcase], resolver_method: :name
field :upcased_name, String, extensions: [Upcase], hash_key: :name

field :ssn, String do
def authorized?(obj, args, ctx)
Expand All @@ -76,6 +76,12 @@ def authorized?(obj, args, ctx)

class Query < GraphQL::Schema::Object
field :students, [Student]

field :lookahead_selections, String, extras: [:lookahead]

def lookahead_selections(lookahead:)
lookahead.selections.to_s
end
end

query(Query)
Expand Down Expand Up @@ -133,6 +139,16 @@ def self.unauthorized_field(err)
assert_equal "BILL", run_graphql_field(AssertionsSchema, "Student.upcasedName", { name: "Bill" })
end

it "works with extras: [:ast_node]" do
assert_equal "Billy \"theKid\" McBilly", run_graphql_field(AssertionsSchema, "Student.name", { name: "Billy" }, arguments: { full_name: true }, ast_node: GraphQL::Language::Nodes::Field.new(name: "name", field_alias: "theKid"))
end

it "works with extras: [:lookahead]" do
assert_equal "[]", run_graphql_field(AssertionsSchema, "Query.lookaheadSelections", :something)
dummy_lookahead = OpenStruct.new(selections: ["one", "two"])
assert_equal "[\"one\", \"two\"]", run_graphql_field(AssertionsSchema, "Query.lookaheadSelections", :something, lookahead: dummy_lookahead)
end

it "prepares arguments" do
assert_equal "Blah De Blah", run_graphql_field(AssertionsSchema, "Student.name", { name: "Blah" }, arguments: { full_name: true, prefix: "de " })
end
Expand Down

0 comments on commit cb3f017

Please sign in to comment.