Skip to content

Commit 7d9cfd9

Browse files
committed
Make RelayClassicMutation work without field_options
1 parent 87e95b0 commit 7d9cfd9

File tree

4 files changed

+67
-18
lines changed

4 files changed

+67
-18
lines changed

lib/graphql/schema/member/has_arguments.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,27 @@ def add_argument(arg_defn)
9292
arg_defn
9393
end
9494

95+
def remove_argument(arg_defn)
96+
prev_defn = @own_arguments[arg_defn.name]
97+
case prev_defn
98+
when nil
99+
# done
100+
when Array
101+
prev_defn.delete(arg_defn)
102+
when GraphQL::Schema::Argument
103+
@own_arguments.delete(arg_defn.name)
104+
else
105+
raise "Invariant: unexpected `@own_arguments[#{arg_defn.name.inspect}]`: #{prev_defn.inspect}"
106+
end
107+
nil
108+
end
109+
95110
# @return [Hash<String => GraphQL::Schema::Argument] Arguments defined on this thing, keyed by name. Includes inherited definitions
96111
def arguments(context = GraphQL::Query::NullContext)
97112
inherited_arguments = if self.is_a?(Class) && superclass.respond_to?(:arguments)
98113
superclass.arguments(context)
99114
elsif defined?(@resolver_class) && @resolver_class
100-
@resolver_class.arguments(context)
115+
@resolver_class.field_arguments(context)
101116
else
102117
nil
103118
end
@@ -131,6 +146,10 @@ def all_argument_definitions
131146
all_defns.merge!(ancestor.own_arguments)
132147
end
133148
end
149+
elsif defined?(@resolver_class) && @resolver_class
150+
all_defns = {}
151+
all_defns.merge!(@resolver_class.own_field_arguments)
152+
all_defns.merge!(own_arguments)
134153
else
135154
all_defns = own_arguments
136155
end
@@ -147,7 +166,7 @@ def get_argument(argument_name, context = GraphQL::Query::NullContext)
147166
if a && Warden.visible_entry?(:visible_argument?, a, context, warden)
148167
a
149168
elsif defined?(@resolver_class) && @resolver_class
150-
@resolver_class.get_argument(argument_name, context)
169+
@resolver_class.get_field_argument(argument_name, context)
151170
else
152171
nil
153172
end

lib/graphql/schema/relay_classic_mutation.rb

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,32 @@ def resolve_with_support(**inputs)
7070
end
7171

7272
class << self
73+
def dummy
74+
@dummy ||= begin
75+
d = Class.new(GraphQL::Schema::Resolver)
76+
d.argument_class(self.argument_class)
77+
# TODO make this lazier?
78+
d.argument(:input, input_type, description: "Parameters for #{self.graphql_name}", required: true)
79+
d
80+
end
81+
end
82+
83+
def field_arguments(context = GraphQL::Query::NullContext)
84+
dummy.arguments(context)
85+
end
86+
87+
def get_field_argument(name, context = GraphQL::Query::NullContext)
88+
dummy.get_argument(name, context)
89+
end
90+
91+
def own_field_arguments
92+
dummy.own_arguments
93+
end
7394

7495
# Also apply this argument to the input type:
75-
def argument(*args, **kwargs, &block)
96+
def argument(*args, own_argument: false, **kwargs, &block)
7697
it = input_type # make sure any inherited arguments are already added to it
77-
arg = super
98+
arg = super(*args, **kwargs, &block)
7899

79100
# This definition might be overriding something inherited;
80101
# if it is, remove the inherited definition so it's not confused at runtime as having multiple definitions
@@ -114,27 +135,24 @@ def input_type(new_input_type = nil)
114135
@input_type ||= generate_input_type
115136
end
116137

117-
# Extend {Schema::Mutation.field_options} to add the `input` argument
118-
def field_options
119-
sig = super
120-
# Arguments were added at the root, but they should be nested
121-
sig[:arguments].clear
122-
sig[:arguments][:input] = { type: input_type, required: true, description: "Parameters for #{graphql_name}" }
123-
sig
124-
end
125-
126138
private
127139

128140
# Generate the input type for the `input:` argument
129141
# To customize how input objects are generated, override this method
130142
# @return [Class] a subclass of {.input_object_class}
131143
def generate_input_type
132144
mutation_args = all_argument_definitions
133-
mutation_name = graphql_name
134145
mutation_class = self
135146
Class.new(input_object_class) do
136-
graphql_name("#{mutation_name}Input")
137-
description("Autogenerated input type of #{mutation_name}")
147+
class << self
148+
def default_graphql_name
149+
"#{self.mutation.graphql_name}Input"
150+
end
151+
152+
def description(new_desc = nil)
153+
super || "Autogenerated input type of #{self.mutation.graphql_name}"
154+
end
155+
end
138156
mutation(mutation_class)
139157
# these might be inherited:
140158
mutation_args.each do |arg|

lib/graphql/schema/resolver.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,18 @@ def get_argument(name, context = GraphQL::Query::NullContext)
208208
end
209209

210210
class << self
211+
def field_arguments(context = GraphQL::Query::NullContext)
212+
arguments(arguments)
213+
end
214+
215+
def get_field_argument(name, context = GraphQL::Query::NullContext)
216+
get_argument(name, context)
217+
end
218+
219+
def own_field_arguments
220+
own_arguments
221+
end
222+
211223
# Default `:resolve` set below.
212224
# @return [Symbol] The method to call on instances of this object to resolve the field
213225
def resolve_method(new_method = nil)

spec/graphql/schema/relay_classic_mutation_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
graphql_name "SomeMutation"
5252
end
5353

54-
assert_equal "Parameters for SomeMutation", mutation.field_options[:arguments][:input][:description]
54+
field = GraphQL::Schema::Field.new(name: "blah", resolver_class: mutation)
55+
assert_equal "Parameters for SomeMutation", field.get_argument("input").description
5556
end
5657
end
5758

@@ -70,7 +71,6 @@
7071
}
7172
}
7273
GRAPHQL
73-
7474
assert_equal "Sitar", res["data"]["addSitar"]["instrument"]["name"]
7575
end
7676

0 commit comments

Comments
 (0)