graphql 2.0.16 → 2.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of graphql might be problematic. Click here for more details.

Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/lib/graphql/analysis/ast/visitor.rb +42 -35
  3. data/lib/graphql/analysis/ast.rb +2 -2
  4. data/lib/graphql/backtrace/tracer.rb +1 -1
  5. data/lib/graphql/execution/interpreter/resolve.rb +19 -0
  6. data/lib/graphql/execution/interpreter/runtime.rb +106 -88
  7. data/lib/graphql/execution/interpreter.rb +14 -9
  8. data/lib/graphql/execution/lazy.rb +6 -12
  9. data/lib/graphql/execution/multiplex.rb +2 -1
  10. data/lib/graphql/graphql_ext.bundle +0 -0
  11. data/lib/graphql/introspection/directive_type.rb +2 -2
  12. data/lib/graphql/introspection/field_type.rb +1 -1
  13. data/lib/graphql/introspection/schema_type.rb +2 -2
  14. data/lib/graphql/introspection/type_type.rb +5 -5
  15. data/lib/graphql/language/lexer.rb +216 -1505
  16. data/lib/graphql/language/lexer.ri +744 -0
  17. data/lib/graphql/language/nodes.rb +39 -31
  18. data/lib/graphql/language/parser.rb +9 -9
  19. data/lib/graphql/language/parser.y +9 -9
  20. data/lib/graphql/language/visitor.rb +191 -83
  21. data/lib/graphql/pagination/active_record_relation_connection.rb +0 -8
  22. data/lib/graphql/query/context.rb +45 -11
  23. data/lib/graphql/query.rb +15 -2
  24. data/lib/graphql/schema/argument.rb +0 -4
  25. data/lib/graphql/schema/directive.rb +12 -2
  26. data/lib/graphql/schema/enum.rb +24 -17
  27. data/lib/graphql/schema/enum_value.rb +5 -3
  28. data/lib/graphql/schema/field.rb +53 -45
  29. data/lib/graphql/schema/interface.rb +0 -10
  30. data/lib/graphql/schema/late_bound_type.rb +2 -0
  31. data/lib/graphql/schema/member/base_dsl_methods.rb +15 -14
  32. data/lib/graphql/schema/member/has_arguments.rb +104 -57
  33. data/lib/graphql/schema/member/has_deprecation_reason.rb +3 -4
  34. data/lib/graphql/schema/member/has_fields.rb +14 -2
  35. data/lib/graphql/schema/member/has_interfaces.rb +49 -8
  36. data/lib/graphql/schema/member/has_validators.rb +31 -5
  37. data/lib/graphql/schema/member/type_system_helpers.rb +17 -0
  38. data/lib/graphql/schema/object.rb +2 -4
  39. data/lib/graphql/schema/resolver/has_payload_type.rb +9 -9
  40. data/lib/graphql/schema/timeout.rb +23 -27
  41. data/lib/graphql/schema/warden.rb +26 -4
  42. data/lib/graphql/schema.rb +37 -19
  43. data/lib/graphql/static_validation/literal_validator.rb +15 -1
  44. data/lib/graphql/static_validation/validator.rb +1 -1
  45. data/lib/graphql/subscriptions/event.rb +2 -7
  46. data/lib/graphql/tracing/active_support_notifications_trace.rb +16 -0
  47. data/lib/graphql/tracing/appoptics_trace.rb +231 -0
  48. data/lib/graphql/tracing/appsignal_trace.rb +66 -0
  49. data/lib/graphql/tracing/data_dog_trace.rb +148 -0
  50. data/lib/graphql/tracing/new_relic_trace.rb +75 -0
  51. data/lib/graphql/tracing/notifications_trace.rb +41 -0
  52. data/lib/graphql/tracing/platform_trace.rb +107 -0
  53. data/lib/graphql/tracing/platform_tracing.rb +15 -3
  54. data/lib/graphql/tracing/prometheus_trace.rb +89 -0
  55. data/lib/graphql/tracing/prometheus_tracing.rb +3 -3
  56. data/lib/graphql/tracing/scout_trace.rb +72 -0
  57. data/lib/graphql/tracing/statsd_trace.rb +56 -0
  58. data/lib/graphql/tracing.rb +136 -39
  59. data/lib/graphql/type_kinds.rb +6 -3
  60. data/lib/graphql/types/relay/connection_behaviors.rb +0 -4
  61. data/lib/graphql/types/relay/edge_behaviors.rb +0 -4
  62. data/lib/graphql/types/string.rb +1 -1
  63. data/lib/graphql/version.rb +1 -1
  64. data/lib/graphql.rb +7 -8
  65. metadata +15 -4
  66. data/lib/graphql/language/lexer.rl +0 -280
@@ -62,7 +62,7 @@ module GraphQL
62
62
  # Schemas can specify how queries should be executed against them.
63
63
  # `query_execution_strategy`, `mutation_execution_strategy` and `subscription_execution_strategy`
64
64
  # each apply to corresponding root types.
65
- # #
65
+ #
66
66
  # @example defining a schema
67
67
  # class MySchema < GraphQL::Schema
68
68
  # query QueryType
@@ -143,6 +143,15 @@ module GraphQL
143
143
  @subscriptions = new_implementation
144
144
  end
145
145
 
146
+ def trace_class(new_class = nil)
147
+ if new_class
148
+ @trace_class = new_class
149
+ elsif !defined?(@trace_class)
150
+ @trace_class = Class.new(GraphQL::Tracing::Trace)
151
+ end
152
+ @trace_class
153
+ end
154
+
146
155
  # Returns the JSON response of {Introspection::INTROSPECTION_QUERY}.
147
156
  # @see {#as_json}
148
157
  # @return [String]
@@ -755,7 +764,7 @@ module GraphQL
755
764
  if handler
756
765
  obj = context[:current_object]
757
766
  args = context[:current_arguments]
758
- args = args && args.keyword_arguments
767
+ args = args && args.respond_to?(:keyword_arguments) ? args.keyword_arguments : nil
759
768
  field = context[:current_field]
760
769
  if obj.is_a?(GraphQL::Schema::Object)
761
770
  obj = obj.object
@@ -826,10 +835,6 @@ module GraphQL
826
835
  member.visible?(ctx)
827
836
  end
828
837
 
829
- def accessible?(member, ctx)
830
- member.accessible?(ctx)
831
- end
832
-
833
838
  def schema_directive(dir_class, **options)
834
839
  @own_schema_directives ||= []
835
840
  Member::HasDirectives.add_directive(self, @own_schema_directives, dir_class, options)
@@ -839,18 +844,6 @@ module GraphQL
839
844
  Member::HasDirectives.get_directives(self, @own_schema_directives, :schema_directives)
840
845
  end
841
846
 
842
- # This hook is called when a client tries to access one or more
843
- # fields that fail the `accessible?` check.
844
- #
845
- # By default, an error is added to the response. Override this hook to
846
- # track metrics or return a different error to the client.
847
- #
848
- # @param error [InaccessibleFieldsError] The analysis error for this check
849
- # @return [AnalysisError, nil] Return an error to skip the query
850
- def inaccessible_fields(error)
851
- error
852
- end
853
-
854
847
  # This hook is called when an object fails an `authorized?` check.
855
848
  # You might report to your bug tracker here, so you can correct
856
849
  # the field resolvers not to return unauthorized objects.
@@ -900,7 +893,7 @@ module GraphQL
900
893
  # A function to call when {#execute} receives an invalid query string
901
894
  #
902
895
  # The default is to add the error to `context.errors`
903
- # @param err [GraphQL::ParseError] The error encountered during parsing
896
+ # @param parse_err [GraphQL::ParseError] The error encountered during parsing
904
897
  # @param ctx [GraphQL::Query::Context] The context for the query where the error occurred
905
898
  # @return void
906
899
  def parse_error(parse_err, ctx)
@@ -942,6 +935,12 @@ module GraphQL
942
935
  end
943
936
 
944
937
  def tracer(new_tracer)
938
+ if defined?(@trace_class) && !(@trace_class < GraphQL::Tracing::LegacyTrace)
939
+ raise ArgumentError, "Can't add tracer after configuring a `trace_class`, use GraphQL::Tracing::LegacyTrace to merge legacy tracers into a trace class instead."
940
+ elsif !defined?(@trace_class)
941
+ @trace_class = Class.new(GraphQL::Tracing::LegacyTrace)
942
+ end
943
+
945
944
  own_tracers << new_tracer
946
945
  end
947
946
 
@@ -949,6 +948,25 @@ module GraphQL
949
948
  find_inherited_value(:tracers, EMPTY_ARRAY) + own_tracers
950
949
  end
951
950
 
951
+ # Mix `trace_mod` into this schema's `Trace` class so that its methods
952
+ # will be called at runtime.
953
+ #
954
+ # @param trace_mod [Module] A module that implements tracing methods
955
+ # @param options [Hash] Keywords that will be passed to the tracing class during `#initialize`
956
+ # @return [void]
957
+ def trace_with(trace_mod, **options)
958
+ @trace_options ||= {}
959
+ @trace_options.merge!(options)
960
+ trace_class.include(trace_mod)
961
+ end
962
+
963
+ def new_trace(**options)
964
+ if defined?(@trace_options)
965
+ options = @trace_options.merge(options)
966
+ end
967
+ trace_class.new(**options)
968
+ end
969
+
952
970
  def query_analyzer(new_analyzer)
953
971
  own_query_analyzers << new_analyzer
954
972
  end
@@ -18,6 +18,19 @@ module GraphQL
18
18
 
19
19
  private
20
20
 
21
+ def replace_nulls_in(ast_value)
22
+ case ast_value
23
+ when Array
24
+ ast_value.map { |v| replace_nulls_in(v) }
25
+ when GraphQL::Language::Nodes::InputObject
26
+ ast_value.to_h
27
+ when GraphQL::Language::Nodes::NullValue
28
+ nil
29
+ else
30
+ ast_value
31
+ end
32
+ end
33
+
21
34
  def recursively_validate(ast_value, type)
22
35
  if type.nil?
23
36
  # this means we're an undefined argument, see #present_input_field_values_are_valid
@@ -42,7 +55,8 @@ module GraphQL
42
55
  @valid_response
43
56
  elsif type.kind.scalar? && constant_scalar?(ast_value)
44
57
  maybe_raise_if_invalid(ast_value) do
45
- type.validate_input(ast_value, @context)
58
+ ruby_value = replace_nulls_in(ast_value)
59
+ type.validate_input(ruby_value, @context)
46
60
  end
47
61
  elsif type.kind.enum?
48
62
  maybe_raise_if_invalid(ast_value) do
@@ -27,7 +27,7 @@ module GraphQL
27
27
  # @param max_errors [Integer] Maximum number of errors before aborting validation. Any positive number will limit the number of errors. Defaults to nil for no limit.
28
28
  # @return [Array<Hash>]
29
29
  def validate(query, validate: true, timeout: nil, max_errors: nil)
30
- query.trace("validate", { validate: validate, query: query }) do
30
+ query.current_trace.validate(validate: validate, query: query) do
31
31
  errors = if validate == false
32
32
  []
33
33
  else
@@ -100,13 +100,8 @@ module GraphQL
100
100
  arg_name = k.to_s
101
101
  camelized_arg_name = GraphQL::Schema::Member::BuildType.camelize(arg_name)
102
102
  arg_defn = get_arg_definition(arg_owner, camelized_arg_name, context)
103
-
104
- if arg_defn
105
- normalized_arg_name = camelized_arg_name
106
- else
107
- normalized_arg_name = arg_name
108
- arg_defn = get_arg_definition(arg_owner, normalized_arg_name, context)
109
- end
103
+ arg_defn ||= get_arg_definition(arg_owner, arg_name, context)
104
+ normalized_arg_name = arg_defn.graphql_name
110
105
  arg_base_type = arg_defn.type.unwrap
111
106
  # In the case where the value being emitted is seen as a "JSON"
112
107
  # type, treat the value as one atomic unit of serialization
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'graphql/tracing/notifications_trace'
4
+
5
+ module GraphQL
6
+ module Tracing
7
+ # This implementation forwards events to ActiveSupport::Notifications
8
+ # with a `graphql` suffix.
9
+ module ActiveSupportNotificationsTrace
10
+ include NotificationsTrace
11
+ def initialize(engine: ActiveSupport::Notifications, **rest)
12
+ super
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,231 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Tracing
5
+
6
+ # This class uses the AppopticsAPM SDK from the appoptics_apm gem to create
7
+ # traces for GraphQL.
8
+ #
9
+ # There are 4 configurations available. They can be set in the
10
+ # appoptics_apm config file or in code. Please see:
11
+ # {https://docs.appoptics.com/kb/apm_tracing/ruby/configure}
12
+ #
13
+ # AppOpticsAPM::Config[:graphql][:enabled] = true|false
14
+ # AppOpticsAPM::Config[:graphql][:transaction_name] = true|false
15
+ # AppOpticsAPM::Config[:graphql][:sanitize_query] = true|false
16
+ # AppOpticsAPM::Config[:graphql][:remove_comments] = true|false
17
+ module AppOpticsTrace
18
+ # These GraphQL events will show up as 'graphql.prep' spans
19
+ PREP_KEYS = ['lex', 'parse', 'validate', 'analyze_query', 'analyze_multiplex'].freeze
20
+ # These GraphQL events will show up as 'graphql.execute' spans
21
+ EXEC_KEYS = ['execute_multiplex', 'execute_query', 'execute_query_lazy'].freeze
22
+
23
+ # During auto-instrumentation this version of AppOpticsTracing is compared
24
+ # with the version provided in the appoptics_apm gem, so that the newer
25
+ # version of the class can be used
26
+
27
+ def self.version
28
+ Gem::Version.new('1.0.0')
29
+ end
30
+
31
+ [
32
+ 'lex',
33
+ 'parse',
34
+ 'validate',
35
+ 'analyze_query',
36
+ 'analyze_multiplex',
37
+ 'execute_multiplex',
38
+ 'execute_query',
39
+ 'execute_query_lazy',
40
+ ].each do |trace_method|
41
+ module_eval <<-RUBY, __FILE__, __LINE__
42
+ def #{trace_method}(**data)
43
+ return super if !defined?(AppOpticsAPM) || gql_config[:enabled] == false
44
+ layer = span_name("#{trace_method}")
45
+ kvs = metadata(data, layer)
46
+ kvs[:Key] = "#{trace_method}" if (PREP_KEYS + EXEC_KEYS).include?("#{trace_method}")
47
+
48
+ transaction_name(kvs[:InboundQuery]) if kvs[:InboundQuery] && layer == 'graphql.execute'
49
+
50
+ ::AppOpticsAPM::SDK.trace(layer, kvs) do
51
+ kvs.clear # we don't have to send them twice
52
+ super
53
+ end
54
+ end
55
+ RUBY
56
+ end
57
+
58
+ def platform_execute_field(platform_key, data)
59
+ return super if !defined?(AppOpticsAPM) || gql_config[:enabled] == false
60
+ layer = platform_key
61
+ kvs = metadata(data, layer)
62
+
63
+ ::AppOpticsAPM::SDK.trace(layer, kvs) do
64
+ kvs.clear # we don't have to send them twice
65
+ yield
66
+ end
67
+ end
68
+
69
+ def authorized(**data)
70
+ return super if !defined?(AppOpticsAPM) || gql_config[:enabled] == false
71
+ layer = @platform_authorized_key_cache[data[:type]]
72
+ kvs = metadata(data, layer)
73
+
74
+ ::AppOpticsAPM::SDK.trace(layer, kvs) do
75
+ kvs.clear # we don't have to send them twice
76
+ super
77
+ end
78
+ end
79
+
80
+ def authorized_lazy(**data)
81
+ return super if !defined?(AppOpticsAPM) || gql_config[:enabled] == false
82
+ layer = @platform_authorized_key_cache[data[:type]]
83
+ kvs = metadata(data, layer)
84
+
85
+ ::AppOpticsAPM::SDK.trace(layer, kvs) do
86
+ kvs.clear # we don't have to send them twice
87
+ super
88
+ end
89
+ end
90
+
91
+ def resolve_type(**data)
92
+ return super if !defined?(AppOpticsAPM) || gql_config[:enabled] == false
93
+ layer = @platform_resolve_type_key_cache[data[:type]]
94
+ kvs = metadata(data, layer)
95
+
96
+ ::AppOpticsAPM::SDK.trace(layer, kvs) do
97
+ kvs.clear # we don't have to send them twice
98
+ super
99
+ end
100
+ end
101
+
102
+ def resolve_type_lazy(**data)
103
+ return super if !defined?(AppOpticsAPM) || gql_config[:enabled] == false
104
+ layer = @platform_resolve_type_key_cache[data[:type]]
105
+ kvs = metadata(data, layer)
106
+
107
+ ::AppOpticsAPM::SDK.trace(layer, kvs) do
108
+ kvs.clear # we don't have to send them twice
109
+ super
110
+ end
111
+ end
112
+
113
+ include PlatformTrace
114
+
115
+ def platform_field_key(field)
116
+ "graphql.#{field.owner.graphql_name}.#{field.graphql_name}"
117
+ end
118
+
119
+ def platform_authorized_key(type)
120
+ "graphql.authorized.#{type.graphql_name}"
121
+ end
122
+
123
+ def platform_resolve_type_key(type)
124
+ "graphql.resolve_type.#{type.graphql_name}"
125
+ end
126
+
127
+ private
128
+
129
+ def gql_config
130
+ ::AppOpticsAPM::Config[:graphql] ||= {}
131
+ end
132
+
133
+ def transaction_name(query)
134
+ return if gql_config[:transaction_name] == false ||
135
+ ::AppOpticsAPM::SDK.get_transaction_name
136
+
137
+ split_query = query.strip.split(/\W+/, 3)
138
+ split_query[0] = 'query' if split_query[0].empty?
139
+ name = "graphql.#{split_query[0..1].join('.')}"
140
+
141
+ ::AppOpticsAPM::SDK.set_transaction_name(name)
142
+ end
143
+
144
+ def multiplex_transaction_name(names)
145
+ return if gql_config[:transaction_name] == false ||
146
+ ::AppOpticsAPM::SDK.get_transaction_name
147
+
148
+ name = "graphql.multiplex.#{names.join('.')}"
149
+ name = "#{name[0..251]}..." if name.length > 254
150
+
151
+ ::AppOpticsAPM::SDK.set_transaction_name(name)
152
+ end
153
+
154
+ def span_name(key)
155
+ return 'graphql.prep' if PREP_KEYS.include?(key)
156
+ return 'graphql.execute' if EXEC_KEYS.include?(key)
157
+
158
+ key[/^graphql\./] ? key : "graphql.#{key}"
159
+ end
160
+
161
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
162
+ def metadata(data, layer)
163
+ data.keys.map do |key|
164
+ case key
165
+ when :context
166
+ graphql_context(data[key], layer)
167
+ when :query
168
+ graphql_query(data[key])
169
+ when :query_string
170
+ graphql_query_string(data[key])
171
+ when :multiplex
172
+ graphql_multiplex(data[key])
173
+ when :path
174
+ [key, data[key].join(".")]
175
+ else
176
+ [key, data[key]]
177
+ end
178
+ end.flatten(2).each_slice(2).to_h.merge(Spec: 'graphql')
179
+ end
180
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
181
+
182
+ def graphql_context(context, layer)
183
+ context.errors && context.errors.each do |err|
184
+ AppOpticsAPM::API.log_exception(layer, err)
185
+ end
186
+
187
+ [[:Path, context.path.join('.')]]
188
+ end
189
+
190
+ def graphql_query(query)
191
+ return [] unless query
192
+
193
+ query_string = query.query_string
194
+ query_string = remove_comments(query_string) if gql_config[:remove_comments] != false
195
+ query_string = sanitize(query_string) if gql_config[:sanitize_query] != false
196
+
197
+ [[:InboundQuery, query_string],
198
+ [:Operation, query.selected_operation_name]]
199
+ end
200
+
201
+ def graphql_query_string(query_string)
202
+ query_string = remove_comments(query_string) if gql_config[:remove_comments] != false
203
+ query_string = sanitize(query_string) if gql_config[:sanitize_query] != false
204
+
205
+ [:InboundQuery, query_string]
206
+ end
207
+
208
+ def graphql_multiplex(data)
209
+ names = data.queries.map(&:operations).map(&:keys).flatten.compact
210
+ multiplex_transaction_name(names) if names.size > 1
211
+
212
+ [:Operations, names.join(', ')]
213
+ end
214
+
215
+ def sanitize(query)
216
+ return unless query
217
+
218
+ # remove arguments
219
+ query.gsub(/"[^"]*"/, '"?"') # strings
220
+ .gsub(/-?[0-9]*\.?[0-9]+e?[0-9]*/, '?') # ints + floats
221
+ .gsub(/\[[^\]]*\]/, '[?]') # arrays
222
+ end
223
+
224
+ def remove_comments(query)
225
+ return unless query
226
+
227
+ query.gsub(/#[^\n\r]*/, '')
228
+ end
229
+ end
230
+ end
231
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Tracing
5
+ module AppsignalTrace
6
+ include PlatformTrace
7
+
8
+
9
+ # @param set_action_name [Boolean] If true, the GraphQL operation name will be used as the transaction name.
10
+ # This is not advised if you run more than one query per HTTP request, for example, with `graphql-client` or multiplexing.
11
+ # It can also be specified per-query with `context[:set_appsignal_action_name]`.
12
+ def initialize(set_action_name: false, **rest)
13
+ @set_action_name = set_action_name
14
+ super
15
+ end
16
+
17
+ {
18
+ "lex" => "lex.graphql",
19
+ "parse" => "parse.graphql",
20
+ "validate" => "validate.graphql",
21
+ "analyze_query" => "analyze.graphql",
22
+ "analyze_multiplex" => "analyze.graphql",
23
+ "execute_multiplex" => "execute.graphql",
24
+ "execute_query" => "execute.graphql",
25
+ "execute_query_lazy" => "execute.graphql",
26
+ }.each do |trace_method, platform_key|
27
+ module_eval <<-RUBY, __FILE__, __LINE__
28
+ def #{trace_method}(**data)
29
+ #{
30
+ if trace_method == "execute_query"
31
+ <<-RUBY
32
+ set_this_txn_name = data[:query].context[:set_appsignal_action_name]
33
+ if set_this_txn_name == true || (set_this_txn_name.nil? && @set_action_name)
34
+ Appsignal::Transaction.current.set_action(transaction_name(data[:query]))
35
+ end
36
+ RUBY
37
+ end
38
+ }
39
+
40
+ Appsignal.instrument("#{platform_key}") do
41
+ super
42
+ end
43
+ end
44
+ RUBY
45
+ end
46
+
47
+ def platform_execute_field(platform_key)
48
+ Appsignal.instrument(platform_key) do
49
+ super
50
+ end
51
+ end
52
+
53
+ def platform_field_key(field)
54
+ "#{field.owner.graphql_name}.#{field.graphql_name}.graphql"
55
+ end
56
+
57
+ def platform_authorized_key(type)
58
+ "#{type.graphql_name}.authorized.graphql"
59
+ end
60
+
61
+ def platform_resolve_type_key(type)
62
+ "#{type.graphql_name}.resolve_type.graphql"
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,148 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Tracing
5
+ module DataDogTrace
6
+ # @param analytics_enabled [Boolean] Deprecated
7
+ # @param analytics_sample_rate [Float] Deprecated
8
+ def initialize(tracer: nil, analytics_enabled: false, analytics_sample_rate: 1.0, service: "ruby-graphql", **rest)
9
+ if tracer.nil?
10
+ tracer = defined?(Datadog::Tracing) ? Datadog::Tracing : Datadog.tracer
11
+ end
12
+ @tracer = tracer
13
+
14
+ analytics_available = defined?(Datadog::Contrib::Analytics) \
15
+ && Datadog::Contrib::Analytics.respond_to?(:enabled?) \
16
+ && Datadog::Contrib::Analytics.respond_to?(:set_sample_rate)
17
+
18
+ @analytics_enabled = analytics_available && Datadog::Contrib::Analytics.enabled?(analytics_enabled)
19
+ @analytics_sample_rate = analytics_sample_rate
20
+ @service_name = service
21
+ super
22
+ end
23
+
24
+ {
25
+ 'lex' => 'lex.graphql',
26
+ 'parse' => 'parse.graphql',
27
+ 'validate' => 'validate.graphql',
28
+ 'analyze_query' => 'analyze.graphql',
29
+ 'analyze_multiplex' => 'analyze.graphql',
30
+ 'execute_multiplex' => 'execute.graphql',
31
+ 'execute_query' => 'execute.graphql',
32
+ 'execute_query_lazy' => 'execute.graphql',
33
+ }.each do |trace_method, trace_key|
34
+ module_eval <<-RUBY, __FILE__, __LINE__
35
+ def #{trace_method}(**data)
36
+ @tracer.trace("#{trace_key}", service: @service_name) do |span|
37
+ span.span_type = 'custom'
38
+ if defined?(Datadog::Tracing::Metadata::Ext) # Introduced in ddtrace 1.0
39
+ span.set_tag(Datadog::Tracing::Metadata::Ext::TAG_COMPONENT, 'graphql')
40
+ span.set_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION, '#{trace_method}')
41
+ end
42
+
43
+ #{
44
+ if trace_method == 'execute_multiplex'
45
+ <<-RUBY
46
+ operations = data[:multiplex].queries.map(&:selected_operation_name).join(', ')
47
+
48
+ resource = if operations.empty?
49
+ first_query = data[:multiplex].queries.first
50
+ fallback_transaction_name(first_query && first_query.context)
51
+ else
52
+ operations
53
+ end
54
+ span.resource = resource if resource
55
+
56
+ # For top span of query, set the analytics sample rate tag, if available.
57
+ if @analytics_enabled
58
+ Datadog::Contrib::Analytics.set_sample_rate(span, @analytics_sample_rate)
59
+ end
60
+ RUBY
61
+ elsif trace_method == 'execute_query'
62
+ <<-RUBY
63
+ span.set_tag(:selected_operation_name, data[:query].selected_operation_name)
64
+ span.set_tag(:selected_operation_type, data[:query].selected_operation.operation_type)
65
+ span.set_tag(:query_string, data[:query].query_string)
66
+ RUBY
67
+ end
68
+ }
69
+ prepare_span("#{trace_method.sub("platform_", "")}", data, span)
70
+ super
71
+ end
72
+ end
73
+ RUBY
74
+ end
75
+
76
+ def platform_execute_field(platform_key, data, span_key = "execute_field")
77
+ @tracer.trace(platform_key, service: @service_name) do |span|
78
+ span.span_type = 'custom'
79
+ if defined?(Datadog::Tracing::Metadata::Ext) # Introduced in ddtrace 1.0
80
+ span.set_tag(Datadog::Tracing::Metadata::Ext::TAG_COMPONENT, 'graphql')
81
+ span.set_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION, span_key)
82
+ end
83
+ prepare_span(span_key, data, span)
84
+ yield
85
+ end
86
+ end
87
+
88
+ def platform_execute_field_lazy(platform_key, data, &block)
89
+ platform_execute_field(platform_key, data, "execute_field_lazy", &block)
90
+ end
91
+
92
+ def authorized(object:, type:, query:, span_key: "authorized")
93
+ platform_key = @platform_authorized_key_cache[type]
94
+ @tracer.trace(platform_key, service: @service_name) do |span|
95
+ span.span_type = 'custom'
96
+ if defined?(Datadog::Tracing::Metadata::Ext) # Introduced in ddtrace 1.0
97
+ span.set_tag(Datadog::Tracing::Metadata::Ext::TAG_COMPONENT, 'graphql')
98
+ span.set_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION, span_key)
99
+ end
100
+ prepare_span(span_key, {object: object, type: type, query: query}, span)
101
+ super(query: query, type: type, object: object)
102
+ end
103
+ end
104
+
105
+ def authorized_lazy(**kwargs, &block)
106
+ authorized(span_key: "authorized_lazy", **kwargs, &block)
107
+ end
108
+
109
+ def resolve_type(object:, type:, query:, span_key: "resolve_type")
110
+ platform_key = @platform_resolve_type_key_cache[type]
111
+ @tracer.trace(platform_key, service: @service_name) do |span|
112
+ span.span_type = 'custom'
113
+ if defined?(Datadog::Tracing::Metadata::Ext) # Introduced in ddtrace 1.0
114
+ span.set_tag(Datadog::Tracing::Metadata::Ext::TAG_COMPONENT, 'graphql')
115
+ span.set_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION, span_key)
116
+ end
117
+ prepare_span(span_key, {object: object, type: type, query: query}, span)
118
+ super(query: query, type: type, object: object)
119
+ end
120
+ end
121
+
122
+ def resolve_type_lazy(**kwargs, &block)
123
+ resolve_type(span_key: "resolve_type_lazy", **kwargs, &block)
124
+ end
125
+
126
+ include PlatformTrace
127
+
128
+ # Implement this method in a subclass to apply custom tags to datadog spans
129
+ # @param key [String] The event being traced
130
+ # @param data [Hash] The runtime data for this event (@see GraphQL::Tracing for keys for each event)
131
+ # @param span [Datadog::Tracing::SpanOperation] The datadog span for this event
132
+ def prepare_span(key, data, span)
133
+ end
134
+
135
+ def platform_field_key(field)
136
+ field.path
137
+ end
138
+
139
+ def platform_authorized_key(type)
140
+ "#{type.graphql_name}.authorized"
141
+ end
142
+
143
+ def platform_resolve_type_key(type)
144
+ "#{type.graphql_name}.resolve_type"
145
+ end
146
+ end
147
+ end
148
+ end