Skip to content

Commit

Permalink
Add support for class level prepare methods on arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug Edey committed Nov 30, 2023
1 parent 7265bd6 commit 3060cf7
Show file tree
Hide file tree
Showing 2 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 owner.respond_to?(@prepare)
owner.public_send(@prepare, value, context || obj.context)
elsif obj.respond_to?(@prepare)
obj.public_send(@prepare, value)
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
10 changes: 8 additions & 2 deletions spec/support/jazz.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Here's the "application"
module Jazz
module Models
Instrument = Struct.new(:name, :family)
Instrument = Struct.new(:name, :family, :alternative_name)
Ensemble = Struct.new(:name)
Musician = Struct.new(:name, :favorite_key)
Key = Struct.new(:root, :sharp, :flat) do
Expand Down Expand Up @@ -544,11 +544,17 @@ class EnsembleInput < GraphQL::Schema::InputObject
end

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

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

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

field :instrument, InstrumentType, null: false
# This is meaningless, but it's to test the conflict with `Hash#entries`
Expand Down

0 comments on commit 3060cf7

Please sign in to comment.