Skip to content

Commit

Permalink
Merge pull request #4717 from DougEdey/de/resolver_prepare_method
Browse files Browse the repository at this point in the history
Add support for class level prepare methods on arguments
  • Loading branch information
rmosolgo authored Dec 7, 2023
2 parents 0bb6b34 + 7e4f5b4 commit 05d120d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/graphql/schema/argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,13 @@ def prepare_value(obj, value, context: nil)
#
# This will have to be called later, when the runtime object _is_ available.
value
else
elsif obj.respond_to?(@prepare)
obj.public_send(@prepare, value)
elsif owner.respond_to?(@prepare)
owner.public_send(@prepare, value, context || obj.context)
else
raise "Invalid prepare for #{@owner.name}.name: #{@prepare.inspect}. "\
"Could not find prepare method #{@prepare} on #{obj.class} or #{owner}."
end
elsif @prepare.respond_to?(:call)
@prepare.call(value, context || obj.context)
Expand Down
2 changes: 1 addition & 1 deletion spec/graphql/schema/mutation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
it "runs mutations" do
query_str = <<-GRAPHQL
mutation {
addInstrument(name: "Trombone", family: BRASS) {
addInstrument(name: "trombone", family: BRASS) {
instrument {
name
family
Expand Down
8 changes: 7 additions & 1 deletion spec/support/jazz.rb
Original file line number Diff line number Diff line change
Expand Up @@ -544,10 +544,16 @@ class EnsembleInput < GraphQL::Schema::InputObject
end

class AddInstrument < GraphQL::Schema::Mutation
class << self
def prepare_name(value, context)
value.capitalize
end
end

null true
description "Register a new musical instrument in the database"

argument :name, String
argument :name, String, prepare: :prepare_name
argument :family, Family

field :instrument, InstrumentType, null: false
Expand Down

0 comments on commit 05d120d

Please sign in to comment.