Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for class level prepare methods on arguments #4717

Merged
merged 3 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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