Skip to content

Commit a04428f

Browse files
authored
Merge pull request #4423 from rmosolgo/print-one-of
print @OneOf definition in SDL when it's used
2 parents ea45d0e + c0a9e0c commit a04428f

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

lib/graphql/language/document_from_schema_definition.rb

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def initialize(
2222
@include_introspection_types = include_introspection_types
2323
@include_built_in_scalars = include_built_in_scalars
2424
@include_built_in_directives = include_built_in_directives
25+
@include_one_of = false
2526

2627
filter = GraphQL::Filter.new(only: only, except: except)
2728
if @schema.respond_to?(:visible?)
@@ -245,20 +246,30 @@ def build_argument_nodes(arguments)
245246
end
246247

247248
def build_directive_nodes(directives)
248-
if !include_built_in_directives
249-
directives = directives.reject { |directive| directive.default_directive? }
250-
end
251-
252249
directives
253250
.map { |directive| build_directive_node(directive) }
254251
.sort_by(&:name)
255252
end
256253

257254
def build_definition_nodes
258-
definitions = []
259-
definitions << build_schema_node if include_schema_node?
260-
definitions += build_directive_nodes(warden.directives)
261-
definitions += build_type_definition_nodes(warden.reachable_types)
255+
dirs_to_build = warden.directives
256+
if !include_built_in_directives
257+
dirs_to_build = dirs_to_build.reject { |directive| directive.default_directive? }
258+
end
259+
dir_nodes = build_directive_nodes(dirs_to_build)
260+
261+
type_nodes = build_type_definition_nodes(warden.reachable_types)
262+
263+
if @include_one_of
264+
# This may have been set to true when iterating over all types
265+
dir_nodes.concat(build_directive_nodes([GraphQL::Schema::Directive::OneOf]))
266+
end
267+
268+
definitions = [*dir_nodes, *type_nodes]
269+
if include_schema_node?
270+
definitions.unshift(build_schema_node)
271+
end
272+
262273
definitions
263274
end
264275

@@ -318,6 +329,11 @@ def definition_directives(member, directives_method)
318329
)
319330
end
320331
end
332+
333+
# If this schema uses this built-in directive definition,
334+
# include it in the print-out since it's not part of the spec yet.
335+
@include_one_of ||= dir.class == GraphQL::Schema::Directive::OneOf
336+
321337
GraphQL::Language::Nodes::Directive.new(
322338
name: dir.class.graphql_name,
323339
arguments: args

spec/graphql/schema/input_object_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,7 @@ def f(a:)
11681168
it "prints in the SDL" do
11691169
sdl = OneOfSchema.to_definition
11701170
assert_includes sdl, "input OneOfInput @oneOf {\n"
1171+
assert_includes sdl, "directive @oneOf on INPUT_OBJECT"
11711172
end
11721173

11731174
it "shows in the introspection query" do

0 commit comments

Comments
 (0)