Skip to content

Commit 53dd5f5

Browse files
committed
Deprecate one-arg coercion
1 parent fd9ed3c commit 53dd5f5

File tree

5 files changed

+53
-15
lines changed

5 files changed

+53
-15
lines changed

lib/graphql/base_type.rb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ def to_s
9797

9898
alias :inspect :to_s
9999

100-
# Use this in places where there is no `ctx`
101100
def valid_isolated_input?(value)
102101
valid_input?(value, GraphQL::Query::NullContext)
103102
end
@@ -114,22 +113,36 @@ def coerce_isolated_result(value)
114113
coerce_result(value, GraphQL::Query::NullContext)
115114
end
116115

117-
def valid_input?(value, ctx)
116+
def valid_input?(value, ctx = nil)
117+
if ctx.nil?
118+
warn_deprecated_coerce("valid_isolated_input?")
119+
ctx = GraphQL::Query::NullContext
120+
end
121+
118122
validate_input(value, ctx).valid?
119123
end
120124

121-
def validate_input(value, ctx)
125+
def validate_input(value, ctx = nil)
126+
if ctx.nil?
127+
warn_deprecated_coerce("validate_isolated_input")
128+
ctx = GraphQL::Query::NullContext
129+
end
130+
122131
if value.nil?
123132
GraphQL::Query::InputValidationResult.new
124133
else
125134
validate_non_null_input(value, ctx)
126135
end
127136
end
128137

129-
def coerce_input(value, ctx)
138+
def coerce_input(value, ctx = nil)
130139
if value.nil?
131140
nil
132141
else
142+
if ctx.nil?
143+
warn_deprecated_coerce("coerce_isolated_input")
144+
ctx = GraphQL::Query::NullContext
145+
end
133146
coerce_non_null_input(value, ctx)
134147
end
135148
end
@@ -192,5 +205,11 @@ def to_definition(schema, printer: nil, **args)
192205
printer ||= GraphQL::Schema::Printer.new(schema, **args)
193206
printer.print_type(self)
194207
end
208+
209+
private
210+
211+
def warn_deprecated_coerce(alt_method_name)
212+
warn("Coercing without a context is deprecated; use `#{alt_method_name}` if you don't want context-awareness")
213+
end
195214
end
196215
end

lib/graphql/enum_type.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,12 @@ def coerce_non_null_input(value_name, ctx)
133133
end
134134
end
135135

136-
def coerce_result(value, ctx)
136+
def coerce_result(value, ctx = nil)
137+
if ctx.nil?
138+
warn_deprecated_coerce("coerce_isolated_result")
139+
ctx = GraphQL::Query::NullContext
140+
end
141+
137142
warden = ctx.warden
138143
all_values = warden ? warden.enum_values(self) : @values_by_name.each_value
139144
enum_value = all_values.find { |val| val.value == value }

lib/graphql/input_object_type.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,12 @@ def coerce_non_null_input(value, ctx)
104104
GraphQL::Query::Arguments.new(input_values, argument_definitions: arguments)
105105
end
106106

107-
def coerce_result(value, ctx = GraphQL::Query::NullContext)
107+
def coerce_result(value, ctx = nil)
108+
if ctx.nil?
109+
warn_deprecated_coerce("coerce_isolated_result")
110+
ctx = GraphQL::Query::NullContext
111+
end
112+
108113
# Allow the application to provide values as :symbols, and convert them to the strings
109114
value = value.reduce({}) { |memo, (k, v)| memo[k.to_s] = v; memo }
110115

lib/graphql/list_type.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ def coerce_non_null_input(value, ctx)
5757
ensure_array(value).map { |item| of_type.coerce_input(item, ctx) }
5858
end
5959

60-
def coerce_result(value, ctx)
60+
def coerce_result(value, ctx = nil)
61+
if ctx.nil?
62+
warn_deprecated_coerce("coerce_isolated_result")
63+
ctx = GraphQL::Query::NullContext
64+
end
6165
ensure_array(value).map { |item| item.nil? ? nil : of_type.coerce_result(item, ctx) }
6266
end
6367

lib/graphql/scalar_type.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,23 @@ def coerce_non_null_input(value, ctx = GraphQL::Query::NullContext)
6666
@coerce_input_proc.call(value, ctx)
6767
end
6868

69-
def coerce_input=(proc)
70-
if !proc.nil?
71-
@coerce_input_proc = ensure_two_arg(proc)
69+
def coerce_input=(coerce_input_fn)
70+
if !coerce_input_fn.nil?
71+
@coerce_input_proc = ensure_two_arg(coerce_input_fn, :coerce_input)
7272
end
7373
end
7474

75-
def coerce_result(value, ctx = GraphQL::Query::NullContext)
75+
def coerce_result(value, ctx = nil)
76+
if ctx.nil?
77+
warn_deprecated_coerce("coerce_isolated_result")
78+
ctx = GraphQL::Query::NullContext
79+
end
7680
@coerce_result_proc.call(value, ctx)
7781
end
7882

79-
def coerce_result=(proc)
80-
if !proc.nil?
81-
@coerce_result_proc = ensure_two_arg(proc)
83+
def coerce_result=(coerce_result_fn)
84+
if !coerce_result_fn.nil?
85+
@coerce_result_proc = ensure_two_arg(coerce_result_fn, :coerce_result)
8286
end
8387
end
8488

@@ -97,8 +101,9 @@ def get_arity(callable)
97101
end
98102
end
99103

100-
def ensure_two_arg(callable)
104+
def ensure_two_arg(callable, method_name)
101105
if get_arity(callable) == 1
106+
warn("Scalar coerce functions receive two values (`val` and `ctx`), one-argument functions are deprecated (see #{name}.#{method_name}).")
102107
->(val, ctx) { callable.call(val) }
103108
else
104109
callable

0 commit comments

Comments
 (0)