3
3
require "graphql/schema/catchall_middleware"
4
4
require "graphql/schema/default_parse_error"
5
5
require "graphql/schema/default_type_error"
6
+ require "graphql/schema/find_inherited_value"
6
7
require "graphql/schema/finder"
7
8
require "graphql/schema/invalid_type_error"
8
9
require "graphql/schema/introspection_system"
@@ -81,6 +82,8 @@ class Schema
81
82
extend Forwardable
82
83
extend GraphQL ::Schema ::Member ::AcceptsDefinition
83
84
include GraphQL ::Define ::InstanceDefinable
85
+ extend GraphQL ::Schema ::FindInheritedValue
86
+
84
87
accepts_definitions \
85
88
:query , :mutation , :subscription ,
86
89
:query_execution_strategy , :mutation_execution_strategy , :subscription_execution_strategy ,
@@ -159,7 +162,9 @@ def default_filter
159
162
# @see {Query#tracers} for query-specific tracers
160
163
attr_reader :tracers
161
164
162
- DYNAMIC_FIELDS = [ "__type" , "__typename" , "__schema" ]
165
+ DYNAMIC_FIELDS = [ "__type" , "__typename" , "__schema" ] . freeze
166
+ EMPTY_ARRAY = [ ] . freeze
167
+ EMPTY_HASH = { } . freeze
163
168
164
169
attr_reader :static_validator , :object_from_id_proc , :id_from_object_proc , :resolve_type_proc
165
170
@@ -728,11 +733,11 @@ def graphql_definition
728
733
end
729
734
730
735
def use ( plugin , options = { } )
731
- plugins << [ plugin , options ]
736
+ own_plugins << [ plugin , options ]
732
737
end
733
738
734
739
def plugins
735
- @ plugins ||= [ ]
740
+ find_inherited_value ( : plugins, EMPTY_ARRAY ) + own_plugins
736
741
end
737
742
738
743
def to_graphql
@@ -746,7 +751,7 @@ def to_graphql
746
751
schema_defn . max_depth = max_depth
747
752
schema_defn . default_max_page_size = default_max_page_size
748
753
schema_defn . orphan_types = orphan_types
749
- schema_defn . disable_introspection_entry_points = @ disable_introspection_entry_points
754
+ schema_defn . disable_introspection_entry_points = disable_introspection_entry_points?
750
755
751
756
prepped_dirs = { }
752
757
directives . each { |k , v | prepped_dirs [ k ] = v . graphql_definition }
@@ -758,26 +763,24 @@ def to_graphql
758
763
schema_defn . type_error = method ( :type_error )
759
764
schema_defn . context_class = context_class
760
765
schema_defn . cursor_encoder = cursor_encoder
761
- schema_defn . tracers . concat ( defined_tracers )
762
- schema_defn . query_analyzers . concat ( defined_query_analyzers )
766
+ schema_defn . tracers . concat ( tracers )
767
+ schema_defn . query_analyzers . concat ( query_analyzers )
763
768
764
- schema_defn . middleware . concat ( defined_middleware )
765
- schema_defn . multiplex_analyzers . concat ( defined_multiplex_analyzers )
769
+ schema_defn . middleware . concat ( all_middleware )
770
+ schema_defn . multiplex_analyzers . concat ( multiplex_analyzers )
766
771
schema_defn . query_execution_strategy = query_execution_strategy
767
772
schema_defn . mutation_execution_strategy = mutation_execution_strategy
768
773
schema_defn . subscription_execution_strategy = subscription_execution_strategy
769
- defined_instrumenters . each do |step , insts |
774
+ all_instrumenters . each do |step , insts |
770
775
insts . each do |inst |
771
776
schema_defn . instrumenters [ step ] << inst
772
777
end
773
778
end
774
779
lazy_classes . each do |lazy_class , value_method |
775
780
schema_defn . lazy_methods . set ( lazy_class , value_method )
776
781
end
777
- if @rescues
778
- @rescues . each do |err_class , handler |
779
- schema_defn . rescue_from ( err_class , &handler )
780
- end
782
+ rescues . each do |err_class , handler |
783
+ schema_defn . rescue_from ( err_class , &handler )
781
784
end
782
785
783
786
if plugins . any?
@@ -806,107 +809,118 @@ def query(new_query_object = nil)
806
809
if new_query_object
807
810
@query_object = new_query_object
808
811
else
809
- @query_object . respond_to? ( :graphql_definition ) ? @query_object . graphql_definition : @query_object
812
+ query_object = @query_object || find_inherited_value ( :query )
813
+ query_object . respond_to? ( :graphql_definition ) ? query_object . graphql_definition : query_object
810
814
end
811
815
end
812
816
813
817
def mutation ( new_mutation_object = nil )
814
818
if new_mutation_object
815
819
@mutation_object = new_mutation_object
816
820
else
817
- @mutation_object . respond_to? ( :graphql_definition ) ? @mutation_object . graphql_definition : @mutation_object
821
+ mutation_object = @mutation_object || find_inherited_value ( :mutation )
822
+ mutation_object . respond_to? ( :graphql_definition ) ? mutation_object . graphql_definition : mutation_object
818
823
end
819
824
end
820
825
821
826
def subscription ( new_subscription_object = nil )
822
827
if new_subscription_object
823
828
@subscription_object = new_subscription_object
824
829
else
825
- @subscription_object . respond_to? ( :graphql_definition ) ? @subscription_object . graphql_definition : @subscription_object
830
+ subscription_object = @subscription_object || find_inherited_value ( :subscription )
831
+ subscription_object . respond_to? ( :graphql_definition ) ? subscription_object . graphql_definition : subscription_object
826
832
end
827
833
end
828
834
829
835
def introspection ( new_introspection_namespace = nil )
830
836
if new_introspection_namespace
831
837
@introspection = new_introspection_namespace
832
838
else
833
- @introspection
839
+ @introspection || find_inherited_value ( :introspection )
834
840
end
835
841
end
836
842
837
843
def cursor_encoder ( new_encoder = nil )
838
844
if new_encoder
839
845
@cursor_encoder = new_encoder
840
846
end
841
- @cursor_encoder || Base64Encoder
847
+ @cursor_encoder || find_inherited_value ( :cursor_encoder , Base64Encoder )
842
848
end
843
849
844
850
def default_max_page_size ( new_default_max_page_size = nil )
845
851
if new_default_max_page_size
846
852
@default_max_page_size = new_default_max_page_size
847
853
else
848
- @default_max_page_size
854
+ @default_max_page_size || find_inherited_value ( :default_max_page_size )
849
855
end
850
856
end
851
857
852
858
def query_execution_strategy ( new_query_execution_strategy = nil )
853
859
if new_query_execution_strategy
854
860
@query_execution_strategy = new_query_execution_strategy
855
861
else
856
- @query_execution_strategy || self . default_execution_strategy
862
+ @query_execution_strategy || find_inherited_value ( :query_execution_strategy , self . default_execution_strategy )
857
863
end
858
864
end
859
865
860
866
def mutation_execution_strategy ( new_mutation_execution_strategy = nil )
861
867
if new_mutation_execution_strategy
862
868
@mutation_execution_strategy = new_mutation_execution_strategy
863
869
else
864
- @mutation_execution_strategy || self . default_execution_strategy
870
+ @mutation_execution_strategy || find_inherited_value ( :mutation_execution_strategy , self . default_execution_strategy )
865
871
end
866
872
end
867
873
868
874
def subscription_execution_strategy ( new_subscription_execution_strategy = nil )
869
875
if new_subscription_execution_strategy
870
876
@subscription_execution_strategy = new_subscription_execution_strategy
871
877
else
872
- @subscription_execution_strategy || self . default_execution_strategy
878
+ @subscription_execution_strategy || find_inherited_value ( :subscription_execution_strategy , self . default_execution_strategy )
873
879
end
874
880
end
875
881
876
882
def max_complexity ( max_complexity = nil )
877
883
if max_complexity
878
884
@max_complexity = max_complexity
879
885
else
880
- @max_complexity
886
+ @max_complexity || find_inherited_value ( :max_complexity )
881
887
end
882
888
end
883
889
884
890
def error_bubbling ( new_error_bubbling = nil )
885
891
if !new_error_bubbling . nil?
886
892
@error_bubbling = new_error_bubbling
887
893
else
888
- @error_bubbling
894
+ @error_bubbling . nil? ? find_inherited_value ( :error_bubbling ) : @error_bubbling
889
895
end
890
896
end
891
897
892
898
def max_depth ( new_max_depth = nil )
893
899
if new_max_depth
894
900
@max_depth = new_max_depth
895
901
else
896
- @max_depth
902
+ @max_depth || find_inherited_value ( :max_depth )
897
903
end
898
904
end
899
905
900
906
def disable_introspection_entry_points
901
907
@disable_introspection_entry_points = true
902
908
end
903
909
910
+ def disable_introspection_entry_points?
911
+ if instance_variable_defined? ( :@disable_introspection_entry_points )
912
+ @disable_introspection_entry_points
913
+ else
914
+ find_inherited_value ( :disable_introspection_entry_points? , false )
915
+ end
916
+ end
917
+
904
918
def orphan_types ( *new_orphan_types )
905
919
if new_orphan_types . any?
906
- @orphan_types = new_orphan_types . flatten
907
- else
908
- @orphan_types || [ ]
920
+ own_orphan_types . concat ( new_orphan_types . flatten )
909
921
end
922
+
923
+ find_inherited_value ( :orphan_types , EMPTY_ARRAY ) + own_orphan_types
910
924
end
911
925
912
926
def default_execution_strategy
@@ -921,17 +935,20 @@ def context_class(new_context_class = nil)
921
935
if new_context_class
922
936
@context_class = new_context_class
923
937
else
924
- @context_class || GraphQL ::Query ::Context
938
+ @context_class || find_inherited_value ( :context_class , GraphQL ::Query ::Context )
925
939
end
926
940
end
927
941
928
942
def rescue_from ( *err_classes , &handler_block )
929
- @rescues ||= { }
930
943
err_classes . each do |err_class |
931
- @rescues [ err_class ] = handler_block
944
+ own_rescues [ err_class ] = handler_block
932
945
end
933
946
end
934
947
948
+ def rescues
949
+ find_inherited_value ( :rescues , EMPTY_HASH ) . merge ( own_rescues )
950
+ end
951
+
935
952
def resolve_type ( type , obj , ctx )
936
953
if type . kind . object?
937
954
type
@@ -1017,19 +1034,20 @@ def instrument(instrument_step, instrumenter, options = {})
1017
1034
else
1018
1035
instrument_step
1019
1036
end
1020
- defined_instrumenters [ step ] << instrumenter
1037
+
1038
+ own_instrumenters [ step ] << instrumenter
1021
1039
end
1022
1040
1023
1041
def directives ( new_directives = nil )
1024
1042
if new_directives
1025
- @directives = new_directives . reduce ( { } ) { | m , d | m [ d . name ] = d ; m }
1043
+ new_directives . each { | d | directive ( d ) }
1026
1044
end
1027
1045
1028
- @ directives ||= default_directives
1046
+ find_inherited_value ( : directives, default_directives ) . merge ( own_directives )
1029
1047
end
1030
1048
1031
1049
def directive ( new_directive )
1032
- directives [ new_directive . graphql_name ] = new_directive
1050
+ own_directives [ new_directive . graphql_name ] = new_directive
1033
1051
end
1034
1052
1035
1053
def default_directives
@@ -1041,26 +1059,38 @@ def default_directives
1041
1059
end
1042
1060
1043
1061
def tracer ( new_tracer )
1044
- defined_tracers << new_tracer
1062
+ own_tracers << new_tracer
1063
+ end
1064
+
1065
+ def tracers
1066
+ find_inherited_value ( :tracers , EMPTY_ARRAY ) + own_tracers
1045
1067
end
1046
1068
1047
1069
def query_analyzer ( new_analyzer )
1048
1070
if new_analyzer == GraphQL ::Authorization ::Analyzer
1049
1071
warn ( "The Authorization query analyzer is deprecated. Authorizing at query runtime is generally a better idea." )
1050
1072
end
1051
- defined_query_analyzers << new_analyzer
1073
+ own_query_analyzers << new_analyzer
1074
+ end
1075
+
1076
+ def query_analyzers
1077
+ find_inherited_value ( :query_analyzers , EMPTY_ARRAY ) + own_query_analyzers
1052
1078
end
1053
1079
1054
1080
def middleware ( new_middleware = nil )
1055
1081
if new_middleware
1056
- defined_middleware << new_middleware
1082
+ own_middleware << new_middleware
1057
1083
else
1058
1084
graphql_definition . middleware
1059
1085
end
1060
1086
end
1061
1087
1062
1088
def multiplex_analyzer ( new_analyzer )
1063
- defined_multiplex_analyzers << new_analyzer
1089
+ own_multiplex_analyzers << new_analyzer
1090
+ end
1091
+
1092
+ def multiplex_analyzers
1093
+ find_inherited_value ( :multiplex_analyzers , EMPTY_ARRAY ) + own_multiplex_analyzers
1064
1094
end
1065
1095
1066
1096
private
@@ -1069,24 +1099,51 @@ def lazy_classes
1069
1099
@lazy_classes ||= { }
1070
1100
end
1071
1101
1072
- def defined_instrumenters
1073
- @defined_instrumenters ||= Hash . new { |h , k | h [ k ] = [ ] }
1102
+ def own_plugins
1103
+ @own_plugins ||= [ ]
1104
+ end
1105
+
1106
+ def own_rescues
1107
+ @own_rescues ||= { }
1074
1108
end
1075
1109
1076
- def defined_tracers
1077
- @defined_tracers ||= [ ]
1110
+ def own_orphan_types
1111
+ @own_orphan_types ||= [ ]
1078
1112
end
1079
1113
1080
- def defined_query_analyzers
1114
+ def own_directives
1115
+ @own_directives ||= { }
1116
+ end
1117
+
1118
+ def all_instrumenters
1119
+ inherited_instrumenters = find_inherited_value ( :all_instrumenters ) || Hash . new { |h , k | h [ k ] = [ ] }
1120
+ inherited_instrumenters . merge ( own_instrumenters ) do |_step , inherited , own |
1121
+ inherited + own
1122
+ end
1123
+ end
1124
+
1125
+ def own_instrumenters
1126
+ @own_instrumenters ||= Hash . new { |h , k | h [ k ] = [ ] }
1127
+ end
1128
+
1129
+ def own_tracers
1130
+ @own_tracers ||= [ ]
1131
+ end
1132
+
1133
+ def own_query_analyzers
1081
1134
@defined_query_analyzers ||= [ ]
1082
1135
end
1083
1136
1084
- def defined_middleware
1085
- @defined_middleware ||= [ ]
1137
+ def all_middleware
1138
+ find_inherited_value ( :all_middleware , EMPTY_ARRAY ) + own_middleware
1139
+ end
1140
+
1141
+ def own_middleware
1142
+ @own_middleware ||= [ ]
1086
1143
end
1087
1144
1088
- def defined_multiplex_analyzers
1089
- @defined_multiplex_analyzers ||= [ ]
1145
+ def own_multiplex_analyzers
1146
+ @own_multiplex_analyzers ||= [ ]
1090
1147
end
1091
1148
1092
1149
# Given this schema member, find the class-based definition object
0 commit comments