Skip to content

Commit 812eff7

Browse files
committed
Make Schema#as_json return an instance of Hash
Certain JSON encoders (namely, Oj) were previously serializing `GraphQL::Query::Result` incorrectly. Instead of returning a schema definition, it would return serialize the output of `GraphQL::Query::Result#inspect` as a string. In order to have better compatibility, this patch opts to represent the schema as a Hash when calling `#as_json`.
1 parent 6e24f61 commit 812eff7

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

lib/graphql/schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ def to_document
554554
# @param except [<#call(member, ctx)>]
555555
# @return [Hash] GraphQL result
556556
def as_json(only: nil, except: nil, context: {})
557-
execute(Introspection::INTROSPECTION_QUERY, only: only, except: except, context: context)
557+
execute(Introspection::INTROSPECTION_QUERY, only: only, except: except, context: context).to_h
558558
end
559559

560560
# Returns the JSON response of {Introspection::INTROSPECTION_QUERY}.

spec/graphql/schema_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,13 @@ class LazyObjChild < LazyObj; end
425425
end
426426
end
427427

428+
describe "#as_json" do
429+
it "returns a hash" do
430+
result = schema.execute(GraphQL::Introspection::INTROSPECTION_QUERY)
431+
assert_equal result.as_json.class, Hash
432+
end
433+
end
434+
428435
describe "#get_field" do
429436
it "returns fields by type or type name" do
430437
field = schema.get_field("Cheese", "id")

0 commit comments

Comments
 (0)