Skip to content

Commit dda906b

Browse files
authored
Merge pull request #5085 from rmosolgo/fix-union-possible-type-printing
fix printing union possible types when the union is an extra type
2 parents 55024ef + bd34888 commit dda906b

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

lib/graphql/language/document_from_schema_definition.rb

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def initialize(
1818
include_built_in_directives: false, include_built_in_scalars: false, always_include_schema: false
1919
)
2020
@schema = schema
21+
@context = context
2122
@always_include_schema = always_include_schema
2223
@include_introspection_types = include_introspection_types
2324
@include_built_in_scalars = include_built_in_scalars
@@ -268,7 +269,33 @@ def build_definition_nodes
268269
end
269270
definitions = build_directive_nodes(dirs_to_build)
270271
all_types = @types.all_types
271-
type_nodes = build_type_definition_nodes(all_types + schema.extra_types)
272+
type_nodes = build_type_definition_nodes(all_types)
273+
274+
if (ex_t = schema.extra_types).any?
275+
dummy_query = Class.new(GraphQL::Schema::Object) do
276+
graphql_name "DummyQuery"
277+
(all_types + ex_t).each_with_index do |type, idx|
278+
if !type.kind.input_object? && !type.introspection?
279+
field "f#{idx}", type
280+
end
281+
end
282+
end
283+
284+
extra_types_schema = Class.new(GraphQL::Schema) do
285+
query(dummy_query)
286+
end
287+
288+
extra_types_types = GraphQL::Query.new(extra_types_schema, "{ __typename }", context: @context).types # rubocop:disable Development/ContextIsPassedCop
289+
# Temporarily replace `@types` with something from this example schema.
290+
# It'd be much nicer to pass this in, but that would be a big refactor :S
291+
prev_types = @types
292+
@types = extra_types_types
293+
type_nodes += build_type_definition_nodes(ex_t)
294+
@types = prev_types
295+
end
296+
297+
type_nodes.sort_by!(&:name)
298+
272299
if @include_one_of
273300
# This may have been set to true when iterating over all types
274301
definitions.concat(build_directive_nodes([GraphQL::Schema::Directive::OneOf]))
@@ -291,9 +318,7 @@ def build_type_definition_nodes(types)
291318
types = types.reject { |type| type.kind.scalar? && type.default_scalar? }
292319
end
293320

294-
types
295-
.map { |type| build_type_definition_node(type) }
296-
.sort_by(&:name)
321+
types.map { |type| build_type_definition_node(type) }
297322
end
298323

299324
def build_field_nodes(fields)

spec/graphql/schema/printer_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,4 +967,30 @@ class OddlyNamedQuery < GraphQL::Schema::Object
967967
schema = GraphQL::Schema.from_definition(input)
968968
assert_equal input, GraphQL::Schema::Printer.print_schema(schema)
969969
end
970+
971+
describe "when Union is used in extra_types" do
972+
it "can be included" do
973+
obj_1 = Class.new(GraphQL::Schema::Object) { graphql_name("Obj1"); field(:f1, String)}
974+
obj_2 = Class.new(GraphQL::Schema::Object) { graphql_name("Obj2"); field(:f2, obj_1) }
975+
union_type = Class.new(GraphQL::Schema::Union) do
976+
graphql_name "Union1"
977+
possible_types(obj_1, obj_2)
978+
end
979+
980+
assert_equal "union Union1 = Obj1 | Obj2\n", Class.new(GraphQL::Schema) { extra_types(union_type) }.to_definition
981+
982+
expected_defn = <<~GRAPHQL
983+
type Obj1 {
984+
f1: String
985+
}
986+
987+
type Obj2 {
988+
f2: Obj1
989+
}
990+
991+
union Union1 = Obj1 | Obj2
992+
GRAPHQL
993+
assert_equal expected_defn, Class.new(GraphQL::Schema) { extra_types(union_type, obj_1, obj_2) }.to_definition
994+
end
995+
end
970996
end

0 commit comments

Comments
 (0)