11# frozen_string_literal: true
22
33require_relative "composer/base_validator"
4- require_relative "composer/supergraph_directives"
54require_relative "composer/validate_interfaces"
65require_relative "composer/validate_type_resolvers"
76require_relative "composer/type_resolver_config"
@@ -55,6 +54,7 @@ def initialize(
5554 query_name : "Query" ,
5655 mutation_name : "Mutation" ,
5756 subscription_name : "Subscription" ,
57+ visibility_profiles : [ ] ,
5858 description_merger : nil ,
5959 deprecation_merger : nil ,
6060 default_value_merger : nil ,
@@ -74,6 +74,7 @@ def initialize(
7474 @resolver_map = { }
7575 @resolver_configs = { }
7676 @mapped_type_names = { }
77+ @visibility_profiles = Set . new ( visibility_profiles )
7778 @subgraph_directives_by_name_and_location = nil
7879 @subgraph_types_by_name_and_location = nil
7980 @schema_directives = nil
@@ -88,9 +89,9 @@ def perform(locations_input)
8889
8990 directives_to_omit = [
9091 GraphQL ::Stitching . stitch_directive ,
91- KeyDirective . graphql_name ,
92- ResolverDirective . graphql_name ,
93- SourceDirective . graphql_name ,
92+ Directives :: SupergraphKey . graphql_name ,
93+ Directives :: SupergraphResolver . graphql_name ,
94+ Directives :: SupergraphSource . graphql_name ,
9495 ]
9596
9697 # "directive_name" => "location" => subgraph_directive
@@ -184,6 +185,10 @@ def perform(locations_input)
184185 expand_abstract_resolvers ( schema , schemas )
185186 apply_supergraph_directives ( schema , @resolver_map , @field_map )
186187
188+ if ( visibility_def = schema . directives [ GraphQL ::Stitching . visibility_directive ] )
189+ visibility_def . get_argument ( "profiles" ) . default_value ( @visibility_profiles . to_a . sort )
190+ end
191+
187192 supergraph = Supergraph . from_definition ( schema , executables : executables )
188193
189194 COMPOSITION_VALIDATORS . each do |validator_class |
@@ -461,14 +466,6 @@ def build_merged_directives(type_name, members_by_location, owner, field_name: n
461466 # handled by deprecation_reason merger...
462467 next if directive_class . graphql_name == "deprecated"
463468
464- if directive_class . graphql_name == GraphQL ::Stitching . visibility_directive
465- unless GraphQL ::Stitching . supports_visibility?
466- raise CompositionError , "Using `@#{ GraphQL ::Stitching . visibility_directive } ` directive " \
467- "for schema visibility controls requires GraphQL Ruby v2.4.15 or later."
468- end
469- kwarg_merger = VISIBILITY_PROFILES_MERGER
470- end
471-
472469 kwarg_values_by_name_location = directives_by_location . each_with_object ( { } ) do |( location , directive ) , memo |
473470 directive . arguments . keyword_arguments . each do |key , value |
474471 key = key . to_s
@@ -479,6 +476,18 @@ def build_merged_directives(type_name, members_by_location, owner, field_name: n
479476 end
480477 end
481478
479+ if directive_class . graphql_name == GraphQL ::Stitching . visibility_directive
480+ unless GraphQL ::Stitching . supports_visibility?
481+ raise CompositionError , "Using `@#{ GraphQL ::Stitching . visibility_directive } ` directive " \
482+ "for schema visibility controls requires GraphQL Ruby v2.4.15 or later."
483+ end
484+
485+ if ( profiles = kwarg_values_by_name_location [ "profiles" ] )
486+ @visibility_profiles . merge ( profiles . each_value . reduce ( &:| ) )
487+ kwarg_merger = VISIBILITY_PROFILES_MERGER
488+ end
489+ end
490+
482491 kwargs = kwarg_values_by_name_location . each_with_object ( { } ) do |( kwarg_name , kwarg_values_by_location ) , memo |
483492 memo [ kwarg_name . to_sym ] = kwarg_merger . call ( kwarg_values_by_location , {
484493 type_name : type_name ,
@@ -705,8 +714,8 @@ def apply_supergraph_directives(schema, resolvers_by_type_name, locations_by_typ
705714
706715 keys_for_type . each do |key , locations |
707716 locations . each do |location |
708- schema_directives [ KeyDirective . graphql_name ] ||= KeyDirective
709- type . directive ( KeyDirective , key : key , location : location )
717+ schema_directives [ Directives :: SupergraphKey . graphql_name ] ||= Directives :: SupergraphKey
718+ type . directive ( Directives :: SupergraphKey , key : key , location : location )
710719 end
711720 end
712721
@@ -722,8 +731,8 @@ def apply_supergraph_directives(schema, resolvers_by_type_name, locations_by_typ
722731 type_name : ( resolver . type_name if resolver . type_name != type_name ) ,
723732 }
724733
725- schema_directives [ ResolverDirective . graphql_name ] ||= ResolverDirective
726- type . directive ( ResolverDirective , **params . tap ( &:compact! ) )
734+ schema_directives [ Directives :: SupergraphResolver . graphql_name ] ||= Directives :: SupergraphResolver
735+ type . directive ( Directives :: SupergraphResolver , **params . tap ( &:compact! ) )
727736 end
728737 end
729738
@@ -749,8 +758,8 @@ def apply_supergraph_directives(schema, resolvers_by_type_name, locations_by_typ
749758
750759 # Apply source directives to annotate the possible locations of each field
751760 locations_for_field . each do |location |
752- schema_directives [ SourceDirective . graphql_name ] ||= SourceDirective
753- field . directive ( SourceDirective , location : location )
761+ schema_directives [ Directives :: SupergraphSource . graphql_name ] ||= Directives :: SupergraphSource
762+ field . directive ( Directives :: SupergraphSource , location : location )
754763 end
755764 end
756765 end
0 commit comments