Skip to content

Commit b0927e7

Browse files
authored
Merge pull request rmosolgo#2343 from daemonsy/to_json_circular_reference
Implement #to_hash so input objects can be serialized/splatted
2 parents 0d046a6 + 0ef6fee commit b0927e7

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

lib/graphql/schema/input_object.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ def to_h
5353
end
5454
end
5555

56+
def to_hash
57+
to_h
58+
end
59+
5660
def unwrap_value(value)
5761
case value
5862
when Array

spec/graphql/schema/input_object_spec.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def self.resolve_type(type, obj, ctx)
277277
end
278278
end
279279

280-
describe "#to_h" do
280+
describe 'hash conversion behavior' do
281281
module InputObjectToHTest
282282
class TestInput1 < GraphQL::Schema::InputObject
283283
graphql_name "TestInput1"
@@ -297,16 +297,26 @@ class TestInput2 < GraphQL::Schema::InputObject
297297
TestInput2.to_graphql
298298
end
299299

300-
it "returns a symbolized, aliased, ruby keyword style hash" do
300+
before do
301301
arg_values = {a: 1, b: 2, c: { d: 3, e: 4, instrumentId: "Instrument/Drum Kit"}}
302302

303-
input_object = InputObjectToHTest::TestInput2.new(
303+
@input_object = InputObjectToHTest::TestInput2.new(
304304
arg_values,
305305
context: OpenStruct.new(schema: Jazz::Schema),
306306
defaults_used: Set.new
307307
)
308+
end
308309

309-
assert_equal({ a: 1, b: 2, input_object: { d: 3, e: 4, instrument: Jazz::Models::Instrument.new("Drum Kit", "PERCUSSION") } }, input_object.to_h)
310+
describe "#to_h" do
311+
it "returns a symbolized, aliased, ruby keyword style hash" do
312+
assert_equal({ a: 1, b: 2, input_object: { d: 3, e: 4, instrument: Jazz::Models::Instrument.new("Drum Kit", "PERCUSSION") } }, @input_object.to_h)
313+
end
314+
end
315+
316+
describe "#to_hash" do
317+
it "returns the same results as #to_h (aliased)" do
318+
assert_equal(@input_object.to_h, @input_object.to_hash)
319+
end
310320
end
311321
end
312322

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
require "spec_helper"
3+
4+
describe GraphQL::Schema::InputObject do
5+
let(:input_object) do
6+
Dummy::DairyProductInput.new(
7+
{ source: 'COW', fatContent: 0.8 },
8+
defaults_used: Set.new,
9+
context: GraphQL::Query::NullContext)
10+
end
11+
12+
describe '#to_json' do
13+
it 'returns JSON serialized representation of the variables hash' do
14+
# Regression note: Previously, calling `to_json` on input objects caused stack too deep errors
15+
assert_equal input_object.to_json, { source: "COW", fat_content: 0.8 }.to_json
16+
end
17+
end
18+
end
19+

0 commit comments

Comments
 (0)