@@ -56,6 +56,7 @@ class Schema
56
56
:orphan_types , :resolve_type ,
57
57
:object_from_id , :id_from_object ,
58
58
directives : -> ( schema , directives ) { schema . directives = directives . reduce ( { } ) { |m , d | m [ d . name ] = d ; m } } ,
59
+ instrument : -> ( schema , type , instrumenter ) { schema . instrumenters [ type ] << instrumenter } ,
59
60
query_analyzer : -> ( schema , analyzer ) { schema . query_analyzers << analyzer } ,
60
61
middleware : -> ( schema , middleware ) { schema . middleware << middleware } ,
61
62
rescue_from : -> ( schema , err_class , &block ) { schema . rescue_from ( err_class , &block ) }
@@ -65,7 +66,7 @@ class Schema
65
66
:query_execution_strategy , :mutation_execution_strategy , :subscription_execution_strategy ,
66
67
:max_depth , :max_complexity ,
67
68
:orphan_types , :directives ,
68
- :query_analyzers , :middleware
69
+ :query_analyzers , :middleware , :instrumenters
69
70
70
71
BUILT_IN_TYPES = Hash [ [ INT_TYPE , STRING_TYPE , FLOAT_TYPE , BOOLEAN_TYPE , ID_TYPE ] . map { |type | [ type . name , type ] } ]
71
72
DIRECTIVES = [ GraphQL ::Directive ::IncludeDirective , GraphQL ::Directive ::SkipDirective , GraphQL ::Directive ::DeprecatedDirective ]
@@ -90,6 +91,7 @@ def initialize
90
91
@resolve_type_proc = nil
91
92
@object_from_id_proc = nil
92
93
@id_from_object_proc = nil
94
+ @instrumenters = Hash . new { |h , k | h [ k ] = [ ] }
93
95
# Default to the built-in execution strategy:
94
96
@query_execution_strategy = GraphQL ::Query ::SerialExecution
95
97
@mutation_execution_strategy = GraphQL ::Query ::SerialExecution
@@ -110,10 +112,16 @@ def define(**kwargs, &block)
110
112
super
111
113
types
112
114
@instrumented_field_map = InstrumentedFieldMap . new ( self )
115
+ field_instrumenters = @instrumenters [ :field ]
113
116
types . each do |type_name , type |
114
117
if type . kind . fields?
115
118
type . all_fields . each do |field_defn |
116
- @instrumented_field_map . set ( type . name , field_defn . name , field_defn )
119
+
120
+ instrumented_field_defn = field_instrumenters . reduce ( field_defn ) do |defn , inst |
121
+ inst . instrument ( type , defn )
122
+ end
123
+
124
+ @instrumented_field_map . set ( type . name , field_defn . name , instrumented_field_defn )
117
125
end
118
126
end
119
127
end
0 commit comments