graphql 2.0.13 → 2.3.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (228) hide show
  1. checksums.yaml +4 -4
  2. data/lib/generators/graphql/install/mutation_root_generator.rb +2 -2
  3. data/lib/generators/graphql/install/templates/base_mutation.erb +2 -0
  4. data/lib/generators/graphql/install/templates/mutation_type.erb +2 -0
  5. data/lib/generators/graphql/install_generator.rb +3 -0
  6. data/lib/generators/graphql/mutation_delete_generator.rb +1 -1
  7. data/lib/generators/graphql/mutation_update_generator.rb +1 -1
  8. data/lib/generators/graphql/relay.rb +18 -1
  9. data/lib/generators/graphql/templates/base_argument.erb +2 -0
  10. data/lib/generators/graphql/templates/base_connection.erb +2 -0
  11. data/lib/generators/graphql/templates/base_edge.erb +2 -0
  12. data/lib/generators/graphql/templates/base_enum.erb +2 -0
  13. data/lib/generators/graphql/templates/base_field.erb +2 -0
  14. data/lib/generators/graphql/templates/base_input_object.erb +2 -0
  15. data/lib/generators/graphql/templates/base_interface.erb +2 -0
  16. data/lib/generators/graphql/templates/base_object.erb +2 -0
  17. data/lib/generators/graphql/templates/base_resolver.erb +6 -0
  18. data/lib/generators/graphql/templates/base_scalar.erb +2 -0
  19. data/lib/generators/graphql/templates/base_union.erb +2 -0
  20. data/lib/generators/graphql/templates/graphql_controller.erb +2 -0
  21. data/lib/generators/graphql/templates/loader.erb +2 -0
  22. data/lib/generators/graphql/templates/mutation.erb +2 -0
  23. data/lib/generators/graphql/templates/node_type.erb +2 -0
  24. data/lib/generators/graphql/templates/query_type.erb +2 -0
  25. data/lib/generators/graphql/templates/schema.erb +8 -0
  26. data/lib/graphql/analysis/analyzer.rb +89 -0
  27. data/lib/graphql/analysis/field_usage.rb +82 -0
  28. data/lib/graphql/analysis/max_query_complexity.rb +20 -0
  29. data/lib/graphql/analysis/max_query_depth.rb +20 -0
  30. data/lib/graphql/analysis/query_complexity.rb +183 -0
  31. data/lib/graphql/analysis/query_depth.rb +58 -0
  32. data/lib/graphql/analysis/visitor.rb +283 -0
  33. data/lib/graphql/analysis.rb +92 -1
  34. data/lib/graphql/backtrace/inspect_result.rb +0 -12
  35. data/lib/graphql/backtrace/table.rb +2 -2
  36. data/lib/graphql/backtrace/trace.rb +93 -0
  37. data/lib/graphql/backtrace/tracer.rb +1 -1
  38. data/lib/graphql/backtrace.rb +2 -1
  39. data/lib/graphql/coercion_error.rb +1 -9
  40. data/lib/graphql/dataloader/async_dataloader.rb +88 -0
  41. data/lib/graphql/dataloader/null_dataloader.rb +1 -1
  42. data/lib/graphql/dataloader/request.rb +5 -0
  43. data/lib/graphql/dataloader/source.rb +89 -45
  44. data/lib/graphql/dataloader.rb +115 -142
  45. data/lib/graphql/duration_encoding_error.rb +16 -0
  46. data/lib/graphql/execution/interpreter/argument_value.rb +5 -1
  47. data/lib/graphql/execution/interpreter/arguments.rb +1 -1
  48. data/lib/graphql/execution/interpreter/arguments_cache.rb +33 -33
  49. data/lib/graphql/execution/interpreter/resolve.rb +19 -0
  50. data/lib/graphql/execution/interpreter/runtime/graphql_result.rb +175 -0
  51. data/lib/graphql/execution/interpreter/runtime.rb +331 -455
  52. data/lib/graphql/execution/interpreter.rb +125 -61
  53. data/lib/graphql/execution/lazy.rb +6 -12
  54. data/lib/graphql/execution/lookahead.rb +124 -46
  55. data/lib/graphql/execution/multiplex.rb +3 -117
  56. data/lib/graphql/execution.rb +0 -1
  57. data/lib/graphql/introspection/directive_type.rb +3 -3
  58. data/lib/graphql/introspection/dynamic_fields.rb +1 -1
  59. data/lib/graphql/introspection/entry_points.rb +11 -5
  60. data/lib/graphql/introspection/field_type.rb +2 -2
  61. data/lib/graphql/introspection/schema_type.rb +10 -13
  62. data/lib/graphql/introspection/type_type.rb +17 -10
  63. data/lib/graphql/introspection.rb +3 -2
  64. data/lib/graphql/language/block_string.rb +34 -18
  65. data/lib/graphql/language/definition_slice.rb +1 -1
  66. data/lib/graphql/language/document_from_schema_definition.rb +75 -59
  67. data/lib/graphql/language/lexer.rb +358 -1506
  68. data/lib/graphql/language/nodes.rb +166 -93
  69. data/lib/graphql/language/parser.rb +795 -1953
  70. data/lib/graphql/language/printer.rb +340 -160
  71. data/lib/graphql/language/sanitized_printer.rb +21 -23
  72. data/lib/graphql/language/static_visitor.rb +167 -0
  73. data/lib/graphql/language/visitor.rb +188 -141
  74. data/lib/graphql/language.rb +61 -1
  75. data/lib/graphql/load_application_object_failed_error.rb +5 -1
  76. data/lib/graphql/pagination/active_record_relation_connection.rb +0 -8
  77. data/lib/graphql/pagination/array_connection.rb +6 -6
  78. data/lib/graphql/pagination/connection.rb +33 -6
  79. data/lib/graphql/pagination/mongoid_relation_connection.rb +1 -2
  80. data/lib/graphql/query/context/scoped_context.rb +101 -0
  81. data/lib/graphql/query/context.rb +117 -112
  82. data/lib/graphql/query/null_context.rb +12 -25
  83. data/lib/graphql/query/validation_pipeline.rb +6 -5
  84. data/lib/graphql/query/variables.rb +3 -3
  85. data/lib/graphql/query.rb +86 -30
  86. data/lib/graphql/railtie.rb +9 -6
  87. data/lib/graphql/rake_task.rb +29 -11
  88. data/lib/graphql/rubocop/graphql/base_cop.rb +1 -1
  89. data/lib/graphql/schema/addition.rb +59 -23
  90. data/lib/graphql/schema/always_visible.rb +11 -0
  91. data/lib/graphql/schema/argument.rb +55 -26
  92. data/lib/graphql/schema/base_64_encoder.rb +3 -5
  93. data/lib/graphql/schema/build_from_definition.rb +56 -32
  94. data/lib/graphql/schema/directive/one_of.rb +24 -0
  95. data/lib/graphql/schema/directive/specified_by.rb +14 -0
  96. data/lib/graphql/schema/directive/transform.rb +1 -1
  97. data/lib/graphql/schema/directive.rb +15 -3
  98. data/lib/graphql/schema/enum.rb +35 -24
  99. data/lib/graphql/schema/enum_value.rb +2 -3
  100. data/lib/graphql/schema/field/connection_extension.rb +2 -16
  101. data/lib/graphql/schema/field/scope_extension.rb +8 -1
  102. data/lib/graphql/schema/field.rb +147 -107
  103. data/lib/graphql/schema/field_extension.rb +1 -4
  104. data/lib/graphql/schema/find_inherited_value.rb +2 -7
  105. data/lib/graphql/schema/has_single_input_argument.rb +158 -0
  106. data/lib/graphql/schema/input_object.rb +47 -11
  107. data/lib/graphql/schema/interface.rb +15 -21
  108. data/lib/graphql/schema/introspection_system.rb +7 -17
  109. data/lib/graphql/schema/late_bound_type.rb +10 -0
  110. data/lib/graphql/schema/list.rb +2 -2
  111. data/lib/graphql/schema/loader.rb +2 -3
  112. data/lib/graphql/schema/member/base_dsl_methods.rb +18 -14
  113. data/lib/graphql/schema/member/build_type.rb +11 -3
  114. data/lib/graphql/schema/member/has_arguments.rb +170 -130
  115. data/lib/graphql/schema/member/has_ast_node.rb +12 -0
  116. data/lib/graphql/schema/member/has_deprecation_reason.rb +3 -4
  117. data/lib/graphql/schema/member/has_directives.rb +81 -61
  118. data/lib/graphql/schema/member/has_fields.rb +100 -38
  119. data/lib/graphql/schema/member/has_interfaces.rb +65 -10
  120. data/lib/graphql/schema/member/has_unresolved_type_error.rb +5 -1
  121. data/lib/graphql/schema/member/has_validators.rb +32 -6
  122. data/lib/graphql/schema/member/relay_shortcuts.rb +19 -0
  123. data/lib/graphql/schema/member/scoped.rb +19 -0
  124. data/lib/graphql/schema/member/type_system_helpers.rb +16 -0
  125. data/lib/graphql/schema/member/validates_input.rb +3 -3
  126. data/lib/graphql/schema/mutation.rb +7 -0
  127. data/lib/graphql/schema/object.rb +16 -5
  128. data/lib/graphql/schema/printer.rb +11 -8
  129. data/lib/graphql/schema/relay_classic_mutation.rb +7 -129
  130. data/lib/graphql/schema/resolver/has_payload_type.rb +9 -9
  131. data/lib/graphql/schema/resolver.rb +47 -32
  132. data/lib/graphql/schema/scalar.rb +3 -3
  133. data/lib/graphql/schema/subscription.rb +11 -4
  134. data/lib/graphql/schema/subset.rb +397 -0
  135. data/lib/graphql/schema/timeout.rb +25 -29
  136. data/lib/graphql/schema/type_expression.rb +2 -2
  137. data/lib/graphql/schema/type_membership.rb +3 -0
  138. data/lib/graphql/schema/union.rb +11 -2
  139. data/lib/graphql/schema/unique_within_type.rb +1 -1
  140. data/lib/graphql/schema/validator/all_validator.rb +60 -0
  141. data/lib/graphql/schema/validator.rb +4 -2
  142. data/lib/graphql/schema/warden.rb +238 -93
  143. data/lib/graphql/schema.rb +498 -103
  144. data/lib/graphql/static_validation/all_rules.rb +2 -1
  145. data/lib/graphql/static_validation/base_visitor.rb +7 -6
  146. data/lib/graphql/static_validation/definition_dependencies.rb +7 -1
  147. data/lib/graphql/static_validation/literal_validator.rb +24 -7
  148. data/lib/graphql/static_validation/rules/argument_literals_are_compatible.rb +1 -1
  149. data/lib/graphql/static_validation/rules/arguments_are_defined.rb +1 -1
  150. data/lib/graphql/static_validation/rules/directives_are_defined.rb +1 -2
  151. data/lib/graphql/static_validation/rules/fields_are_defined_on_type.rb +1 -1
  152. data/lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb +12 -4
  153. data/lib/graphql/static_validation/rules/fields_will_merge.rb +10 -10
  154. data/lib/graphql/static_validation/rules/fragment_spreads_are_possible.rb +3 -3
  155. data/lib/graphql/static_validation/rules/fragment_types_exist.rb +1 -1
  156. data/lib/graphql/static_validation/rules/fragments_are_on_composite_types.rb +1 -1
  157. data/lib/graphql/static_validation/rules/mutation_root_exists.rb +1 -1
  158. data/lib/graphql/static_validation/rules/one_of_input_objects_are_valid.rb +66 -0
  159. data/lib/graphql/static_validation/rules/one_of_input_objects_are_valid_error.rb +29 -0
  160. data/lib/graphql/static_validation/rules/query_root_exists.rb +1 -1
  161. data/lib/graphql/static_validation/rules/required_arguments_are_present.rb +4 -4
  162. data/lib/graphql/static_validation/rules/required_input_object_attributes_are_present.rb +5 -5
  163. data/lib/graphql/static_validation/rules/subscription_root_exists.rb +1 -1
  164. data/lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb +18 -27
  165. data/lib/graphql/static_validation/rules/variable_usages_are_allowed.rb +1 -1
  166. data/lib/graphql/static_validation/rules/variables_are_input_types.rb +1 -1
  167. data/lib/graphql/static_validation/validation_context.rb +5 -5
  168. data/lib/graphql/static_validation/validator.rb +4 -1
  169. data/lib/graphql/static_validation.rb +0 -1
  170. data/lib/graphql/subscriptions/action_cable_subscriptions.rb +11 -4
  171. data/lib/graphql/subscriptions/broadcast_analyzer.rb +11 -5
  172. data/lib/graphql/subscriptions/event.rb +11 -10
  173. data/lib/graphql/subscriptions/serialize.rb +2 -0
  174. data/lib/graphql/subscriptions.rb +20 -13
  175. data/lib/graphql/testing/helpers.rb +151 -0
  176. data/lib/graphql/testing.rb +2 -0
  177. data/lib/graphql/tracing/active_support_notifications_trace.rb +16 -0
  178. data/lib/graphql/tracing/appoptics_trace.rb +251 -0
  179. data/lib/graphql/tracing/appoptics_tracing.rb +2 -2
  180. data/lib/graphql/tracing/appsignal_trace.rb +77 -0
  181. data/lib/graphql/tracing/data_dog_trace.rb +183 -0
  182. data/lib/graphql/tracing/data_dog_tracing.rb +9 -21
  183. data/lib/graphql/{execution/instrumentation.rb → tracing/legacy_hooks_trace.rb} +10 -28
  184. data/lib/graphql/tracing/legacy_trace.rb +69 -0
  185. data/lib/graphql/tracing/new_relic_trace.rb +75 -0
  186. data/lib/graphql/tracing/notifications_trace.rb +45 -0
  187. data/lib/graphql/tracing/platform_trace.rb +118 -0
  188. data/lib/graphql/tracing/platform_tracing.rb +17 -3
  189. data/lib/graphql/tracing/{prometheus_tracing → prometheus_trace}/graphql_collector.rb +4 -2
  190. data/lib/graphql/tracing/prometheus_trace.rb +89 -0
  191. data/lib/graphql/tracing/prometheus_tracing.rb +3 -3
  192. data/lib/graphql/tracing/scout_trace.rb +72 -0
  193. data/lib/graphql/tracing/sentry_trace.rb +112 -0
  194. data/lib/graphql/tracing/statsd_trace.rb +56 -0
  195. data/lib/graphql/tracing/trace.rb +76 -0
  196. data/lib/graphql/tracing.rb +20 -40
  197. data/lib/graphql/type_kinds.rb +7 -4
  198. data/lib/graphql/types/iso_8601_duration.rb +77 -0
  199. data/lib/graphql/types/relay/base_connection.rb +1 -1
  200. data/lib/graphql/types/relay/connection_behaviors.rb +68 -6
  201. data/lib/graphql/types/relay/edge_behaviors.rb +33 -5
  202. data/lib/graphql/types/relay/node_behaviors.rb +8 -2
  203. data/lib/graphql/types/relay/page_info_behaviors.rb +11 -2
  204. data/lib/graphql/types/relay.rb +0 -1
  205. data/lib/graphql/types/string.rb +1 -1
  206. data/lib/graphql/types.rb +1 -0
  207. data/lib/graphql/version.rb +1 -1
  208. data/lib/graphql.rb +27 -20
  209. data/readme.md +13 -3
  210. metadata +96 -47
  211. data/lib/graphql/analysis/ast/analyzer.rb +0 -84
  212. data/lib/graphql/analysis/ast/field_usage.rb +0 -57
  213. data/lib/graphql/analysis/ast/max_query_complexity.rb +0 -22
  214. data/lib/graphql/analysis/ast/max_query_depth.rb +0 -22
  215. data/lib/graphql/analysis/ast/query_complexity.rb +0 -230
  216. data/lib/graphql/analysis/ast/query_depth.rb +0 -55
  217. data/lib/graphql/analysis/ast/visitor.rb +0 -269
  218. data/lib/graphql/analysis/ast.rb +0 -81
  219. data/lib/graphql/deprecation.rb +0 -9
  220. data/lib/graphql/filter.rb +0 -53
  221. data/lib/graphql/language/lexer.rl +0 -280
  222. data/lib/graphql/language/parser.y +0 -554
  223. data/lib/graphql/language/token.rb +0 -34
  224. data/lib/graphql/schema/base_64_bp.rb +0 -26
  225. data/lib/graphql/schema/invalid_type_error.rb +0 -7
  226. data/lib/graphql/static_validation/type_stack.rb +0 -216
  227. data/lib/graphql/subscriptions/instrumentation.rb +0 -28
  228. data/lib/graphql/types/relay/default_relay.rb +0 -21
@@ -0,0 +1,251 @@
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 execute_field(query:, field:, ast_node:, arguments:, object:)
59
+ return_type = field.type.unwrap
60
+ trace_field = if return_type.kind.scalar? || return_type.kind.enum?
61
+ (field.trace.nil? && @trace_scalars) || field.trace
62
+ else
63
+ true
64
+ end
65
+ platform_key = if trace_field
66
+ @platform_key_cache[AppOpticsTrace].platform_field_key_cache[field]
67
+ else
68
+ nil
69
+ end
70
+ if platform_key && trace_field
71
+ return super if !defined?(AppOpticsAPM) || gql_config[:enabled] == false
72
+ layer = platform_key
73
+ kvs = metadata({query: query, field: field, ast_node: ast_node, arguments: arguments, object: object}, layer)
74
+
75
+ ::AppOpticsAPM::SDK.trace(layer, kvs) do
76
+ kvs.clear # we don't have to send them twice
77
+ super
78
+ end
79
+ else
80
+ super
81
+ end
82
+ end
83
+
84
+ def execute_field_lazy(query:, field:, ast_node:, arguments:, object:)
85
+ execute_field(query: query, field: field, ast_node: ast_node, arguments: arguments, object: object)
86
+ end
87
+
88
+ def authorized(**data)
89
+ return super if !defined?(AppOpticsAPM) || gql_config[:enabled] == false
90
+ layer = @platform_key_cache[AppOpticsTrace].platform_authorized_key_cache[data[:type]]
91
+ kvs = metadata(data, layer)
92
+
93
+ ::AppOpticsAPM::SDK.trace(layer, kvs) do
94
+ kvs.clear # we don't have to send them twice
95
+ super
96
+ end
97
+ end
98
+
99
+ def authorized_lazy(**data)
100
+ return super if !defined?(AppOpticsAPM) || gql_config[:enabled] == false
101
+ layer = @platform_key_cache[AppOpticsTrace].platform_authorized_key_cache[data[:type]]
102
+ kvs = metadata(data, layer)
103
+
104
+ ::AppOpticsAPM::SDK.trace(layer, kvs) do
105
+ kvs.clear # we don't have to send them twice
106
+ super
107
+ end
108
+ end
109
+
110
+ def resolve_type(**data)
111
+ return super if !defined?(AppOpticsAPM) || gql_config[:enabled] == false
112
+ layer = @platform_key_cache[AppOpticsTrace].platform_resolve_type_key_cache[data[:type]]
113
+
114
+ kvs = metadata(data, layer)
115
+
116
+ ::AppOpticsAPM::SDK.trace(layer, kvs) do
117
+ kvs.clear # we don't have to send them twice
118
+ super
119
+ end
120
+ end
121
+
122
+ def resolve_type_lazy(**data)
123
+ return super if !defined?(AppOpticsAPM) || gql_config[:enabled] == false
124
+ layer = @platform_key_cache[AppOpticsTrace].platform_resolve_type_key_cache[data[:type]]
125
+ kvs = metadata(data, layer)
126
+
127
+ ::AppOpticsAPM::SDK.trace(layer, kvs) do
128
+ kvs.clear # we don't have to send them twice
129
+ super
130
+ end
131
+ end
132
+
133
+ include PlatformTrace
134
+
135
+ def platform_field_key(field)
136
+ "graphql.#{field.owner.graphql_name}.#{field.graphql_name}"
137
+ end
138
+
139
+ def platform_authorized_key(type)
140
+ "graphql.authorized.#{type.graphql_name}"
141
+ end
142
+
143
+ def platform_resolve_type_key(type)
144
+ "graphql.resolve_type.#{type.graphql_name}"
145
+ end
146
+
147
+ private
148
+
149
+ def gql_config
150
+ ::AppOpticsAPM::Config[:graphql] ||= {}
151
+ end
152
+
153
+ def transaction_name(query)
154
+ return if gql_config[:transaction_name] == false ||
155
+ ::AppOpticsAPM::SDK.get_transaction_name
156
+
157
+ split_query = query.strip.split(/\W+/, 3)
158
+ split_query[0] = 'query' if split_query[0].empty?
159
+ name = "graphql.#{split_query[0..1].join('.')}"
160
+
161
+ ::AppOpticsAPM::SDK.set_transaction_name(name)
162
+ end
163
+
164
+ def multiplex_transaction_name(names)
165
+ return if gql_config[:transaction_name] == false ||
166
+ ::AppOpticsAPM::SDK.get_transaction_name
167
+
168
+ name = "graphql.multiplex.#{names.join('.')}"
169
+ name = "#{name[0..251]}..." if name.length > 254
170
+
171
+ ::AppOpticsAPM::SDK.set_transaction_name(name)
172
+ end
173
+
174
+ def span_name(key)
175
+ return 'graphql.prep' if PREP_KEYS.include?(key)
176
+ return 'graphql.execute' if EXEC_KEYS.include?(key)
177
+
178
+ key[/^graphql\./] ? key : "graphql.#{key}"
179
+ end
180
+
181
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
182
+ def metadata(data, layer)
183
+ data.keys.map do |key|
184
+ case key
185
+ when :context
186
+ graphql_context(data[key], layer)
187
+ when :query
188
+ graphql_query(data[key])
189
+ when :query_string
190
+ graphql_query_string(data[key])
191
+ when :multiplex
192
+ graphql_multiplex(data[key])
193
+ when :path
194
+ [key, data[key].join(".")]
195
+ else
196
+ [key, data[key]]
197
+ end
198
+ end.tap { _1.flatten!(2) }.each_slice(2).to_h.merge(Spec: 'graphql')
199
+ end
200
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
201
+
202
+ def graphql_context(context, layer)
203
+ context.errors && context.errors.each do |err|
204
+ AppOpticsAPM::API.log_exception(layer, err)
205
+ end
206
+
207
+ [[:Path, context.path.join('.')]]
208
+ end
209
+
210
+ def graphql_query(query)
211
+ return [] unless query
212
+
213
+ query_string = query.query_string
214
+ query_string = remove_comments(query_string) if gql_config[:remove_comments] != false
215
+ query_string = sanitize(query_string) if gql_config[:sanitize_query] != false
216
+
217
+ [[:InboundQuery, query_string],
218
+ [:Operation, query.selected_operation_name]]
219
+ end
220
+
221
+ def graphql_query_string(query_string)
222
+ query_string = remove_comments(query_string) if gql_config[:remove_comments] != false
223
+ query_string = sanitize(query_string) if gql_config[:sanitize_query] != false
224
+
225
+ [:InboundQuery, query_string]
226
+ end
227
+
228
+ def graphql_multiplex(data)
229
+ names = data.queries.map(&:operations).map!(&:keys).tap(&:flatten!).tap(&:compact!)
230
+ multiplex_transaction_name(names) if names.size > 1
231
+
232
+ [:Operations, names.join(', ')]
233
+ end
234
+
235
+ def sanitize(query)
236
+ return unless query
237
+
238
+ # remove arguments
239
+ query.gsub(/"[^"]*"/, '"?"') # strings
240
+ .gsub(/-?[0-9]*\.?[0-9]+e?[0-9]*/, '?') # ints + floats
241
+ .gsub(/\[[^\]]*\]/, '[?]') # arrays
242
+ end
243
+
244
+ def remove_comments(query)
245
+ return unless query
246
+
247
+ query.gsub(/#[^\n\r]*/, '')
248
+ end
249
+ end
250
+ end
251
+ end
@@ -117,7 +117,7 @@ module GraphQL
117
117
  else
118
118
  [key, data[key]]
119
119
  end
120
- end.flatten(2).each_slice(2).to_h.merge(Spec: 'graphql')
120
+ end.tap { _1.flatten!(2) }.each_slice(2).to_h.merge(Spec: 'graphql')
121
121
  end
122
122
  # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
123
123
 
@@ -148,7 +148,7 @@ module GraphQL
148
148
  end
149
149
 
150
150
  def graphql_multiplex(data)
151
- names = data.queries.map(&:operations).map(&:keys).flatten.compact
151
+ names = data.queries.map(&:operations).map!(&:keys).tap(&:flatten!).tap(&:compact!)
152
152
  multiplex_transaction_name(names) if names.size > 1
153
153
 
154
154
  [:Operations, names.join(', ')]
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Tracing
5
+ module AppsignalTrace
6
+ include PlatformTrace
7
+
8
+ # @param set_action_name [Boolean] If true, the GraphQL operation name will be used as the transaction name.
9
+ # This is not advised if you run more than one query per HTTP request, for example, with `graphql-client` or multiplexing.
10
+ # It can also be specified per-query with `context[:set_appsignal_action_name]`.
11
+ def initialize(set_action_name: false, **rest)
12
+ @set_action_name = set_action_name
13
+ super
14
+ end
15
+
16
+ {
17
+ "lex" => "lex.graphql",
18
+ "parse" => "parse.graphql",
19
+ "validate" => "validate.graphql",
20
+ "analyze_query" => "analyze.graphql",
21
+ "analyze_multiplex" => "analyze.graphql",
22
+ "execute_multiplex" => "execute.graphql",
23
+ "execute_query" => "execute.graphql",
24
+ "execute_query_lazy" => "execute.graphql",
25
+ }.each do |trace_method, platform_key|
26
+ module_eval <<-RUBY, __FILE__, __LINE__
27
+ def #{trace_method}(**data)
28
+ #{
29
+ if trace_method == "execute_query"
30
+ <<-RUBY
31
+ set_this_txn_name = data[:query].context[:set_appsignal_action_name]
32
+ if set_this_txn_name == true || (set_this_txn_name.nil? && @set_action_name)
33
+ Appsignal::Transaction.current.set_action(transaction_name(data[:query]))
34
+ end
35
+ RUBY
36
+ end
37
+ }
38
+
39
+ Appsignal.instrument("#{platform_key}") do
40
+ super
41
+ end
42
+ end
43
+ RUBY
44
+ end
45
+
46
+ def platform_execute_field(platform_key)
47
+ Appsignal.instrument(platform_key) do
48
+ yield
49
+ end
50
+ end
51
+
52
+ def platform_authorized(platform_key)
53
+ Appsignal.instrument(platform_key) do
54
+ yield
55
+ end
56
+ end
57
+
58
+ def platform_resolve_type(platform_key)
59
+ Appsignal.instrument(platform_key) do
60
+ yield
61
+ end
62
+ end
63
+
64
+ def platform_field_key(field)
65
+ "#{field.owner.graphql_name}.#{field.graphql_name}.graphql"
66
+ end
67
+
68
+ def platform_authorized_key(type)
69
+ "#{type.graphql_name}.authorized.graphql"
70
+ end
71
+
72
+ def platform_resolve_type_key(type)
73
+ "#{type.graphql_name}.resolve_type.graphql"
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,183 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Tracing
5
+ module DataDogTrace
6
+ # @param tracer [#trace] Deprecated
7
+ # @param analytics_enabled [Boolean] Deprecated
8
+ # @param analytics_sample_rate [Float] Deprecated
9
+ def initialize(tracer: nil, analytics_enabled: false, analytics_sample_rate: 1.0, service: nil, **rest)
10
+ if tracer.nil?
11
+ tracer = defined?(Datadog::Tracing) ? Datadog::Tracing : Datadog.tracer
12
+ end
13
+ @tracer = tracer
14
+
15
+ @analytics_enabled = analytics_enabled
16
+ @analytics_sample_rate = analytics_sample_rate
17
+
18
+ @service_name = service
19
+ @has_prepare_span = respond_to?(:prepare_span)
20
+ super
21
+ end
22
+
23
+ {
24
+ 'lex' => 'lex.graphql',
25
+ 'parse' => 'parse.graphql',
26
+ 'validate' => 'validate.graphql',
27
+ 'analyze_query' => 'analyze.graphql',
28
+ 'analyze_multiplex' => 'analyze.graphql',
29
+ 'execute_multiplex' => 'execute.graphql',
30
+ 'execute_query' => 'execute.graphql',
31
+ 'execute_query_lazy' => 'execute.graphql',
32
+ }.each do |trace_method, trace_key|
33
+ module_eval <<-RUBY, __FILE__, __LINE__
34
+ def #{trace_method}(**data)
35
+ @tracer.trace("#{trace_key}", service: @service_name, type: 'custom') do |span|
36
+ span.set_tag('component', 'graphql')
37
+ span.set_tag('operation', '#{trace_method}')
38
+
39
+ #{
40
+ if trace_method == 'execute_multiplex'
41
+ <<-RUBY
42
+ operations = data[:multiplex].queries.map(&:selected_operation_name).join(', ')
43
+
44
+ resource = if operations.empty?
45
+ first_query = data[:multiplex].queries.first
46
+ fallback_transaction_name(first_query && first_query.context)
47
+ else
48
+ operations
49
+ end
50
+ span.resource = resource if resource
51
+
52
+ # [Deprecated] will be removed in the future
53
+ span.set_metric('_dd1.sr.eausr', @analytics_sample_rate) if @analytics_enabled
54
+ RUBY
55
+ elsif trace_method == 'execute_query'
56
+ <<-RUBY
57
+ span.set_tag(:selected_operation_name, data[:query].selected_operation_name)
58
+ span.set_tag(:selected_operation_type, data[:query].selected_operation.operation_type)
59
+ span.set_tag(:query_string, data[:query].query_string)
60
+ RUBY
61
+ end
62
+ }
63
+ if @has_prepare_span
64
+ prepare_span("#{trace_method.sub("platform_", "")}", data, span)
65
+ end
66
+ super
67
+ end
68
+ end
69
+ RUBY
70
+ end
71
+
72
+ def execute_field_span(span_key, query, field, ast_node, arguments, object)
73
+ return_type = field.type.unwrap
74
+ trace_field = if return_type.kind.scalar? || return_type.kind.enum?
75
+ (field.trace.nil? && @trace_scalars) || field.trace
76
+ else
77
+ true
78
+ end
79
+ platform_key = if trace_field
80
+ @platform_key_cache[DataDogTrace].platform_field_key_cache[field]
81
+ else
82
+ nil
83
+ end
84
+ if platform_key && trace_field
85
+ @tracer.trace(platform_key, service: @service_name, type: 'custom') do |span|
86
+ span.set_tag('component', 'graphql')
87
+ span.set_tag('operation', span_key)
88
+
89
+ if @has_prepare_span
90
+ prepare_span_data = { query: query, field: field, ast_node: ast_node, arguments: arguments, object: object }
91
+ prepare_span(span_key, prepare_span_data, span)
92
+ end
93
+ yield
94
+ end
95
+ else
96
+ yield
97
+ end
98
+ end
99
+ def execute_field(query:, field:, ast_node:, arguments:, object:)
100
+ execute_field_span("execute_field", query, field, ast_node, arguments, object) do
101
+ super(query: query, field: field, ast_node: ast_node, arguments: arguments, object: object)
102
+ end
103
+ end
104
+
105
+ def execute_field_lazy(query:, field:, ast_node:, arguments:, object:)
106
+ execute_field_span("execute_field_lazy", query, field, ast_node, arguments, object) do
107
+ super(query: query, field: field, ast_node: ast_node, arguments: arguments, object: object)
108
+ end
109
+ end
110
+
111
+ def authorized(query:, type:, object:)
112
+ authorized_span("authorized", object, type, query) do
113
+ super(query: query, type: type, object: object)
114
+ end
115
+ end
116
+
117
+ def authorized_span(span_key, object, type, query)
118
+ platform_key = @platform_key_cache[DataDogTrace].platform_authorized_key_cache[type]
119
+ @tracer.trace(platform_key, service: @service_name, type: 'custom') do |span|
120
+ span.set_tag('component', 'graphql')
121
+ span.set_tag('operation', span_key)
122
+
123
+ if @has_prepare_span
124
+ prepare_span(span_key, {object: object, type: type, query: query}, span)
125
+ end
126
+ yield
127
+ end
128
+ end
129
+
130
+ def authorized_lazy(object:, type:, query:)
131
+ authorized_span("authorized_lazy", object, type, query) do
132
+ super(query: query, type: type, object: object)
133
+ end
134
+ end
135
+
136
+ def resolve_type(object:, type:, query:)
137
+ resolve_type_span("resolve_type", object, type, query) do
138
+ super(object: object, query: query, type: type)
139
+ end
140
+ end
141
+
142
+ def resolve_type_lazy(object:, type:, query:)
143
+ resolve_type_span("resolve_type_lazy", object, type, query) do
144
+ super(object: object, query: query, type: type)
145
+ end
146
+ end
147
+
148
+ def resolve_type_span(span_key, object, type, query)
149
+ platform_key = @platform_key_cache[DataDogTrace].platform_resolve_type_key_cache[type]
150
+ @tracer.trace(platform_key, service: @service_name, type: 'custom') do |span|
151
+ span.set_tag('component', 'graphql')
152
+ span.set_tag('operation', span_key)
153
+
154
+ if @has_prepare_span
155
+ prepare_span(span_key, {object: object, type: type, query: query}, span)
156
+ end
157
+ yield
158
+ end
159
+ end
160
+
161
+ include PlatformTrace
162
+
163
+ # Implement this method in a subclass to apply custom tags to datadog spans
164
+ # @param key [String] The event being traced
165
+ # @param data [Hash] The runtime data for this event (@see GraphQL::Tracing for keys for each event)
166
+ # @param span [Datadog::Tracing::SpanOperation] The datadog span for this event
167
+ # def prepare_span(key, data, span)
168
+ # end
169
+
170
+ def platform_field_key(field)
171
+ field.path
172
+ end
173
+
174
+ def platform_authorized_key(type)
175
+ "#{type.graphql_name}.authorized"
176
+ end
177
+
178
+ def platform_resolve_type_key(type)
179
+ "#{type.graphql_name}.resolve_type"
180
+ end
181
+ end
182
+ end
183
+ end
@@ -15,12 +15,9 @@ module GraphQL
15
15
  }
16
16
 
17
17
  def platform_trace(platform_key, key, data)
18
- tracer.trace(platform_key, service: service_name) do |span|
19
- span.span_type = 'custom'
20
- if defined?(Datadog::Tracing::Metadata::Ext) # Introduced in ddtrace 1.0
21
- span.set_tag(Datadog::Tracing::Metadata::Ext::TAG_COMPONENT, 'graphql')
22
- span.set_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION, key)
23
- end
18
+ tracer.trace(platform_key, service: options[:service], type: 'custom') do |span|
19
+ span.set_tag('component', 'graphql')
20
+ span.set_tag('operation', key)
24
21
 
25
22
  if key == 'execute_multiplex'
26
23
  operations = data[:multiplex].queries.map(&:selected_operation_name).join(', ')
@@ -33,10 +30,8 @@ module GraphQL
33
30
  end
34
31
  span.resource = resource if resource
35
32
 
36
- # For top span of query, set the analytics sample rate tag, if available.
37
- if analytics_enabled?
38
- Datadog::Contrib::Analytics.set_sample_rate(span, analytics_sample_rate)
39
- end
33
+ # [Deprecated] will be removed in the future
34
+ span.set_metric('_dd1.sr.eausr', analytics_sample_rate) if analytics_enabled?
40
35
  end
41
36
 
42
37
  if key == 'execute_query'
@@ -51,10 +46,6 @@ module GraphQL
51
46
  end
52
47
  end
53
48
 
54
- def service_name
55
- options.fetch(:service, 'ruby-graphql')
56
- end
57
-
58
49
  # Implement this method in a subclass to apply custom tags to datadog spans
59
50
  # @param key [String] The event being traced
60
51
  # @param data [Hash] The runtime data for this event (@see GraphQL::Tracing for keys for each event)
@@ -65,20 +56,17 @@ module GraphQL
65
56
  def tracer
66
57
  default_tracer = defined?(Datadog::Tracing) ? Datadog::Tracing : Datadog.tracer
67
58
 
59
+ # [Deprecated] options[:tracer] will be removed in the future
68
60
  options.fetch(:tracer, default_tracer)
69
61
  end
70
62
 
71
- def analytics_available?
72
- defined?(Datadog::Contrib::Analytics) \
73
- && Datadog::Contrib::Analytics.respond_to?(:enabled?) \
74
- && Datadog::Contrib::Analytics.respond_to?(:set_sample_rate)
75
- end
76
-
77
63
  def analytics_enabled?
78
- analytics_available? && Datadog::Contrib::Analytics.enabled?(options.fetch(:analytics_enabled, false))
64
+ # [Deprecated] options[:analytics_enabled] will be removed in the future
65
+ options.fetch(:analytics_enabled, false)
79
66
  end
80
67
 
81
68
  def analytics_sample_rate
69
+ # [Deprecated] options[:analytics_sample_rate] will be removed in the future
82
70
  options.fetch(:analytics_sample_rate, 1.0)
83
71
  end
84
72
 
@@ -1,38 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
  module GraphQL
3
- module Execution
4
- module Instrumentation
5
- # This function implements the instrumentation policy:
6
- #
7
- # - Instrumenters are a stack; the first `before_query` will have the last `after_query`
8
- # - If a `before_` hook returned without an error, its corresponding `after_` hook will run.
9
- # - If the `before_` hook did _not_ run, the `after_` hook will not be called.
10
- #
11
- # When errors are raised from `after_` hooks:
12
- # - Subsequent `after_` hooks _are_ called
13
- # - The first raised error is captured; later errors are ignored
14
- # - If an error was capture, it's re-raised after all hooks are finished
15
- #
16
- # Partial runs of instrumentation are possible:
17
- # - If a `before_multiplex` hook raises an error, no `before_query` hooks will run
18
- # - If a `before_query` hook raises an error, subsequent `before_query` hooks will not run (on any query)
19
- def self.apply_instrumenters(multiplex)
20
- schema = multiplex.schema
21
- queries = multiplex.queries
22
- query_instrumenters = schema.instrumenters[:query]
23
- multiplex_instrumenters = schema.instrumenters[:multiplex]
24
-
3
+ module Tracing
4
+ module LegacyHooksTrace
5
+ def execute_multiplex(multiplex:)
6
+ multiplex_instrumenters = multiplex.schema.instrumenters[:multiplex]
7
+ query_instrumenters = multiplex.schema.instrumenters[:query]
25
8
  # First, run multiplex instrumentation, then query instrumentation for each query
26
- call_hooks(multiplex_instrumenters, multiplex, :before_multiplex, :after_multiplex) do
27
- each_query_call_hooks(query_instrumenters, queries) do
28
- # Let them be executed
29
- yield
9
+ RunHooks.call_hooks(multiplex_instrumenters, multiplex, :before_multiplex, :after_multiplex) do
10
+ RunHooks.each_query_call_hooks(query_instrumenters, multiplex.queries) do
11
+ super
30
12
  end
31
13
  end
32
14
  end
33
15
 
34
- class << self
35
- private
16
+ module RunHooks
17
+ module_function
36
18
  # Call the before_ hooks of each query,
37
19
  # Then yield if no errors.
38
20
  # `call_hooks` takes care of appropriate cleanup.