Skip to content

Commit

Permalink
Merge pull request #4423 from rmosolgo/print-one-of
Browse files Browse the repository at this point in the history
print @OneOf definition in SDL when it's used
  • Loading branch information
rmosolgo authored Apr 5, 2023
2 parents ea45d0e + c0a9e0c commit a04428f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
32 changes: 24 additions & 8 deletions lib/graphql/language/document_from_schema_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def initialize(
@include_introspection_types = include_introspection_types
@include_built_in_scalars = include_built_in_scalars
@include_built_in_directives = include_built_in_directives
@include_one_of = false

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

def build_directive_nodes(directives)
if !include_built_in_directives
directives = directives.reject { |directive| directive.default_directive? }
end

directives
.map { |directive| build_directive_node(directive) }
.sort_by(&:name)
end

def build_definition_nodes
definitions = []
definitions << build_schema_node if include_schema_node?
definitions += build_directive_nodes(warden.directives)
definitions += build_type_definition_nodes(warden.reachable_types)
dirs_to_build = warden.directives
if !include_built_in_directives
dirs_to_build = dirs_to_build.reject { |directive| directive.default_directive? }
end
dir_nodes = build_directive_nodes(dirs_to_build)

type_nodes = build_type_definition_nodes(warden.reachable_types)

if @include_one_of
# This may have been set to true when iterating over all types
dir_nodes.concat(build_directive_nodes([GraphQL::Schema::Directive::OneOf]))
end

definitions = [*dir_nodes, *type_nodes]
if include_schema_node?
definitions.unshift(build_schema_node)
end

definitions
end

Expand Down Expand Up @@ -318,6 +329,11 @@ def definition_directives(member, directives_method)
)
end
end

# If this schema uses this built-in directive definition,
# include it in the print-out since it's not part of the spec yet.
@include_one_of ||= dir.class == GraphQL::Schema::Directive::OneOf

GraphQL::Language::Nodes::Directive.new(
name: dir.class.graphql_name,
arguments: args
Expand Down
1 change: 1 addition & 0 deletions spec/graphql/schema/input_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,7 @@ def f(a:)
it "prints in the SDL" do
sdl = OneOfSchema.to_definition
assert_includes sdl, "input OneOfInput @oneOf {\n"
assert_includes sdl, "directive @oneOf on INPUT_OBJECT"
end

it "shows in the introspection query" do
Expand Down

0 comments on commit a04428f

Please sign in to comment.