From d4c13e07c77067e41bca72f9d4bfd24421a3f43f Mon Sep 17 00:00:00 2001 From: Robert Mosolgo Date: Wed, 5 Apr 2023 15:12:07 -0400 Subject: [PATCH 1/2] print @oneOf definition in SDL when it's used --- .../document_from_schema_definition.rb | 32 ++++++++++++++----- spec/graphql/schema/input_object_spec.rb | 1 + 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/graphql/language/document_from_schema_definition.rb b/lib/graphql/language/document_from_schema_definition.rb index 0b0cce9869..516e74b9e0 100644 --- a/lib/graphql/language/document_from_schema_definition.rb +++ b/lib/graphql/language/document_from_schema_definition.rb @@ -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?) @@ -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(warden.directives) + + 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 @@ -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 diff --git a/spec/graphql/schema/input_object_spec.rb b/spec/graphql/schema/input_object_spec.rb index 90b5dbfda0..ad4f63f048 100644 --- a/spec/graphql/schema/input_object_spec.rb +++ b/spec/graphql/schema/input_object_spec.rb @@ -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 From c0a9e0cb5dcef4deb1e81054119ec6b54075a82e Mon Sep 17 00:00:00 2001 From: Robert Mosolgo Date: Wed, 5 Apr 2023 15:38:22 -0400 Subject: [PATCH 2/2] update fix --- lib/graphql/language/document_from_schema_definition.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/graphql/language/document_from_schema_definition.rb b/lib/graphql/language/document_from_schema_definition.rb index 516e74b9e0..842c2e223a 100644 --- a/lib/graphql/language/document_from_schema_definition.rb +++ b/lib/graphql/language/document_from_schema_definition.rb @@ -256,7 +256,7 @@ def build_definition_nodes if !include_built_in_directives dirs_to_build = dirs_to_build.reject { |directive| directive.default_directive? } end - dir_nodes = build_directive_nodes(warden.directives) + dir_nodes = build_directive_nodes(dirs_to_build) type_nodes = build_type_definition_nodes(warden.reachable_types)