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
@@ -2,7 +2,7 @@
2
2
  module GraphQL
3
3
  module Introspection
4
4
  class DynamicFields < Introspection::BaseObject
5
- field :__typename, String, "The name of this type", null: false
5
+ field :__typename, String, "The name of this type", null: false, dynamic_introspection: true
6
6
 
7
7
  def __typename
8
8
  object.class.graphql_name
@@ -2,20 +2,26 @@
2
2
  module GraphQL
3
3
  module Introspection
4
4
  class EntryPoints < Introspection::BaseObject
5
- field :__schema, GraphQL::Schema::LateBoundType.new("__Schema"), "This GraphQL schema", null: false
6
- field :__type, GraphQL::Schema::LateBoundType.new("__Type"), "A type in the GraphQL system" do
5
+ field :__schema, GraphQL::Schema::LateBoundType.new("__Schema"), "This GraphQL schema", null: false, dynamic_introspection: true
6
+ field :__type, GraphQL::Schema::LateBoundType.new("__Type"), "A type in the GraphQL system", dynamic_introspection: true do
7
7
  argument :name, String
8
8
  end
9
9
 
10
10
  def __schema
11
11
  # Apply wrapping manually since this field isn't wrapped by instrumentation
12
- schema = @context.query.schema
12
+ schema = context.schema
13
13
  schema_type = schema.introspection_system.types["__Schema"]
14
- schema_type.authorized_new(schema, @context)
14
+ schema_type.wrap(schema, context)
15
15
  end
16
16
 
17
17
  def __type(name:)
18
- context.warden.reachable_type?(name) ? context.warden.get_type(name) : nil
18
+ if context.types.reachable_type?(name)
19
+ context.types.type(name)
20
+ elsif (type = context.schema.extra_types.find { |t| t.graphql_name == name })
21
+ type
22
+ else
23
+ nil
24
+ end
19
25
  end
20
26
  end
21
27
  end
@@ -7,7 +7,7 @@ module GraphQL
7
7
  "a name, potentially a list of arguments, and a return type."
8
8
  field :name, String, null: false
9
9
  field :description, String
10
- field :args, [GraphQL::Schema::LateBoundType.new("__InputValue")], null: false do
10
+ field :args, [GraphQL::Schema::LateBoundType.new("__InputValue")], null: false, scope: false do
11
11
  argument :include_deprecated, Boolean, required: false, default_value: false
12
12
  end
13
13
  field :type, GraphQL::Schema::LateBoundType.new("__Type"), null: false
@@ -19,7 +19,7 @@ module GraphQL
19
19
  end
20
20
 
21
21
  def args(include_deprecated:)
22
- args = @context.warden.arguments(@object)
22
+ args = @context.types.arguments(@object)
23
23
  args = args.reject(&:deprecation_reason) unless include_deprecated
24
24
  args
25
25
  end
@@ -8,11 +8,11 @@ module GraphQL
8
8
  "available types and directives on the server, as well as the entry points for "\
9
9
  "query, mutation, and subscription operations."
10
10
 
11
- field :types, [GraphQL::Schema::LateBoundType.new("__Type")], "A list of all types supported by this server.", null: false
11
+ field :types, [GraphQL::Schema::LateBoundType.new("__Type")], "A list of all types supported by this server.", null: false, scope: false
12
12
  field :query_type, GraphQL::Schema::LateBoundType.new("__Type"), "The type that query operations will be rooted at.", null: false
13
13
  field :mutation_type, GraphQL::Schema::LateBoundType.new("__Type"), "If this server supports mutation, the type that mutation operations will be rooted at."
14
14
  field :subscription_type, GraphQL::Schema::LateBoundType.new("__Type"), "If this server support subscription, the type that subscription operations will be rooted at."
15
- field :directives, [GraphQL::Schema::LateBoundType.new("__Directive")], "A list of all directives supported by this server.", null: false
15
+ field :directives, [GraphQL::Schema::LateBoundType.new("__Directive")], "A list of all directives supported by this server.", null: false, scope: false
16
16
  field :description, String, resolver_method: :schema_description
17
17
 
18
18
  def schema_description
@@ -20,29 +20,26 @@ module GraphQL
20
20
  end
21
21
 
22
22
  def types
23
- @context.warden.reachable_types.sort_by(&:graphql_name)
23
+ query_types = context.types.all_types
24
+ types = query_types + context.schema.extra_types
25
+ types.sort_by!(&:graphql_name)
26
+ types
24
27
  end
25
28
 
26
29
  def query_type
27
- permitted_root_type("query")
30
+ @context.types.query_root
28
31
  end
29
32
 
30
33
  def mutation_type
31
- permitted_root_type("mutation")
34
+ @context.types.mutation_root
32
35
  end
33
36
 
34
37
  def subscription_type
35
- permitted_root_type("subscription")
38
+ @context.types.subscription_root
36
39
  end
37
40
 
38
41
  def directives
39
- @context.warden.directives
40
- end
41
-
42
- private
43
-
44
- def permitted_root_type(op_type)
45
- @context.warden.root_type_for_operation(op_type)
42
+ @context.types.directives
46
43
  end
47
44
  end
48
45
  end
@@ -14,21 +14,28 @@ module GraphQL
14
14
  field :kind, GraphQL::Schema::LateBoundType.new("__TypeKind"), null: false
15
15
  field :name, String, method: :graphql_name
16
16
  field :description, String
17
- field :fields, [GraphQL::Schema::LateBoundType.new("__Field")] do
17
+ field :fields, [GraphQL::Schema::LateBoundType.new("__Field")], scope: false do
18
18
  argument :include_deprecated, Boolean, required: false, default_value: false
19
19
  end
20
- field :interfaces, [GraphQL::Schema::LateBoundType.new("__Type")]
21
- field :possible_types, [GraphQL::Schema::LateBoundType.new("__Type")]
22
- field :enum_values, [GraphQL::Schema::LateBoundType.new("__EnumValue")] do
20
+ field :interfaces, [GraphQL::Schema::LateBoundType.new("__Type")], scope: false
21
+ field :possible_types, [GraphQL::Schema::LateBoundType.new("__Type")], scope: false
22
+ field :enum_values, [GraphQL::Schema::LateBoundType.new("__EnumValue")], scope: false do
23
23
  argument :include_deprecated, Boolean, required: false, default_value: false
24
24
  end
25
- field :input_fields, [GraphQL::Schema::LateBoundType.new("__InputValue")] do
25
+ field :input_fields, [GraphQL::Schema::LateBoundType.new("__InputValue")], scope: false do
26
26
  argument :include_deprecated, Boolean, required: false, default_value: false
27
27
  end
28
28
  field :of_type, GraphQL::Schema::LateBoundType.new("__Type")
29
29
 
30
30
  field :specifiedByURL, String, resolver_method: :specified_by_url
31
31
 
32
+ field :is_one_of, Boolean, null: false
33
+
34
+ def is_one_of
35
+ object.kind.input_object? &&
36
+ object.directives.any? { |d| d.graphql_name == "oneOf" }
37
+ end
38
+
32
39
  def specified_by_url
33
40
  if object.kind.scalar?
34
41
  object.specified_by_url
@@ -45,7 +52,7 @@ module GraphQL
45
52
  if !@object.kind.enum?
46
53
  nil
47
54
  else
48
- enum_values = @context.warden.enum_values(@object)
55
+ enum_values = @context.types.enum_values(@object)
49
56
 
50
57
  if !include_deprecated
51
58
  enum_values = enum_values.select {|f| !f.deprecation_reason }
@@ -57,7 +64,7 @@ module GraphQL
57
64
 
58
65
  def interfaces
59
66
  if @object.kind.object? || @object.kind.interface?
60
- @context.warden.interfaces(@object).sort_by(&:graphql_name)
67
+ @context.types.interfaces(@object).sort_by(&:graphql_name)
61
68
  else
62
69
  nil
63
70
  end
@@ -65,7 +72,7 @@ module GraphQL
65
72
 
66
73
  def input_fields(include_deprecated:)
67
74
  if @object.kind.input_object?
68
- args = @context.warden.arguments(@object)
75
+ args = @context.types.arguments(@object)
69
76
  args = args.reject(&:deprecation_reason) unless include_deprecated
70
77
  args
71
78
  else
@@ -75,7 +82,7 @@ module GraphQL
75
82
 
76
83
  def possible_types
77
84
  if @object.kind.abstract?
78
- @context.warden.possible_types(@object).sort_by(&:graphql_name)
85
+ @context.types.possible_types(@object).sort_by(&:graphql_name)
79
86
  else
80
87
  nil
81
88
  end
@@ -85,7 +92,7 @@ module GraphQL
85
92
  if !@object.kind.fields?
86
93
  nil
87
94
  else
88
- fields = @context.warden.fields(@object)
95
+ fields = @context.types.fields(@object)
89
96
  if !include_deprecated
90
97
  fields = fields.select {|f| !f.deprecation_reason }
91
98
  end
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
  module GraphQL
3
3
  module Introspection
4
- def self.query(include_deprecated_args: false, include_schema_description: false, include_is_repeatable: false, include_specified_by_url: false)
4
+ def self.query(include_deprecated_args: false, include_schema_description: false, include_is_repeatable: false, include_specified_by_url: false, include_is_one_of: false)
5
5
  # The introspection query to end all introspection queries, copied from
6
6
  # https://github.com/graphql/graphql-js/blob/master/src/utilities/introspectionQuery.js
7
- <<-QUERY
7
+ <<-QUERY.gsub(/\n{2,}/, "\n")
8
8
  query IntrospectionQuery {
9
9
  __schema {
10
10
  #{include_schema_description ? "description" : ""}
@@ -30,6 +30,7 @@ fragment FullType on __Type {
30
30
  name
31
31
  description
32
32
  #{include_specified_by_url ? "specifiedByURL" : ""}
33
+ #{include_is_one_of ? "isOneOf" : ""}
33
34
  fields(includeDeprecated: true) {
34
35
  name
35
36
  description
@@ -47,10 +47,10 @@ module GraphQL
47
47
  end
48
48
 
49
49
  # Remove leading & trailing blank lines
50
- while lines.size > 0 && lines[0].empty?
50
+ while lines.size > 0 && contains_only_whitespace?(lines.first)
51
51
  lines.shift
52
52
  end
53
- while lines.size > 0 && lines[-1].empty?
53
+ while lines.size > 0 && contains_only_whitespace?(lines.last)
54
54
  lines.pop
55
55
  end
56
56
 
@@ -59,40 +59,56 @@ module GraphQL
59
59
  end
60
60
 
61
61
  def self.print(str, indent: '')
62
- lines = str.split("\n")
62
+ line_length = 120 - indent.length
63
+ block_str = "".dup
64
+ triple_quotes = "\"\"\"\n"
65
+ block_str << indent
66
+ block_str << triple_quotes
63
67
 
64
- block_str = "#{indent}\"\"\"\n".dup
65
-
66
- lines.each do |line|
67
- if line == ''
68
- block_str << "\n"
69
- else
70
- sublines = break_line(line, 120 - indent.length)
71
- sublines.each do |subline|
72
- block_str << "#{indent}#{subline}\n"
68
+ if str.include?("\n")
69
+ str.split("\n") do |line|
70
+ if line == ''
71
+ block_str << "\n"
72
+ else
73
+ break_line(line, line_length) do |subline|
74
+ block_str << indent
75
+ block_str << subline
76
+ block_str << "\n"
77
+ end
73
78
  end
74
79
  end
80
+ else
81
+ break_line(str, line_length) do |subline|
82
+ block_str << indent
83
+ block_str << subline
84
+ block_str << "\n"
85
+ end
75
86
  end
76
87
 
77
- block_str << "#{indent}\"\"\"\n".dup
88
+ block_str << indent
89
+ block_str << triple_quotes
78
90
  end
79
91
 
80
92
  private
81
93
 
82
94
  def self.break_line(line, length)
83
- return [line] if line.length < length + 5
95
+ return yield(line) if line.length < length + 5
84
96
 
85
97
  parts = line.split(Regexp.new("((?: |^).{15,#{length - 40}}(?= |$))"))
86
- return [line] if parts.length < 4
98
+ return yield(line) if parts.length < 4
87
99
 
88
- sublines = [parts.slice!(0, 3).join]
100
+ yield(parts.slice!(0, 3).join)
89
101
 
90
102
  parts.each_with_index do |part, i|
91
103
  next if i % 2 == 1
92
- sublines << "#{part[1..-1]}#{parts[i + 1]}"
104
+ yield "#{part[1..-1]}#{parts[i + 1]}"
93
105
  end
94
106
 
95
- sublines
107
+ nil
108
+ end
109
+
110
+ def self.contains_only_whitespace?(line)
111
+ line.match?(/^\s*$/)
96
112
  end
97
113
  end
98
114
  end
@@ -15,7 +15,7 @@ module GraphQL
15
15
 
16
16
  private
17
17
 
18
- class DependencyVisitor < GraphQL::Language::Visitor
18
+ class DependencyVisitor < GraphQL::Language::StaticVisitor
19
19
  def initialize(doc, definitions, names)
20
20
  @names = names
21
21
  @definitions = definitions
@@ -14,7 +14,7 @@ module GraphQL
14
14
  # @param include_built_in_directives [Boolean] Whether or not to include built in directives in the AST
15
15
  class DocumentFromSchemaDefinition
16
16
  def initialize(
17
- schema, context: nil, only: nil, except: nil, include_introspection_types: false,
17
+ schema, context: nil, include_introspection_types: false,
18
18
  include_built_in_directives: false, include_built_in_scalars: false, always_include_schema: false
19
19
  )
20
20
  @schema = schema
@@ -22,19 +22,10 @@ module GraphQL
22
22
  @include_introspection_types = include_introspection_types
23
23
  @include_built_in_scalars = include_built_in_scalars
24
24
  @include_built_in_directives = include_built_in_directives
25
+ @include_one_of = false
25
26
 
26
- filter = GraphQL::Filter.new(only: only, except: except)
27
- if @schema.respond_to?(:visible?)
28
- filter = filter.merge(only: @schema.method(:visible?))
29
- end
30
-
31
- schema_context = schema.context_class.new(query: nil, object: nil, schema: schema, values: context)
32
- @warden = GraphQL::Schema::Warden.new(
33
- filter,
34
- schema: @schema,
35
- context: schema_context,
36
- )
37
- schema_context.warden = @warden
27
+ dummy_query = @schema.query_class.new(@schema, "{ __typename }", validate: false, context: context)
28
+ @types = dummy_query.types # rubocop:disable Development/ContextIsPassedCop
38
29
  end
39
30
 
40
31
  def document
@@ -44,23 +35,31 @@ module GraphQL
44
35
  end
45
36
 
46
37
  def build_schema_node
47
- GraphQL::Language::Nodes::SchemaDefinition.new(
48
- query: (q = warden.root_type_for_operation("query")) && q.graphql_name,
49
- mutation: (m = warden.root_type_for_operation("mutation")) && m.graphql_name,
50
- subscription: (s = warden.root_type_for_operation("subscription")) && s.graphql_name,
51
- # This only supports directives from parsing,
52
- # use a custom printer to add to this list.
53
- #
54
- # `@schema.directives` is covered by `build_definition_nodes`
55
- directives: ast_directives(@schema),
56
- )
38
+ if !schema_respects_root_name_conventions?(@schema)
39
+ GraphQL::Language::Nodes::SchemaDefinition.new(
40
+ query: @types.query_root&.graphql_name,
41
+ mutation: @types.mutation_root&.graphql_name,
42
+ subscription: @types.subscription_root&.graphql_name,
43
+ directives: definition_directives(@schema, :schema_directives)
44
+ )
45
+ else
46
+ # A plain `schema ...` _must_ include root type definitions.
47
+ # If the only difference is directives, then you have to use `extend schema`
48
+ GraphQL::Language::Nodes::SchemaExtension.new(directives: definition_directives(@schema, :schema_directives))
49
+ end
57
50
  end
58
51
 
59
52
  def build_object_type_node(object_type)
53
+ ints = @types.interfaces(object_type)
54
+ if ints.any?
55
+ ints.sort_by!(&:graphql_name)
56
+ ints.map! { |iface| build_type_name_node(iface) }
57
+ end
58
+
60
59
  GraphQL::Language::Nodes::ObjectTypeDefinition.new(
61
60
  name: object_type.graphql_name,
62
- interfaces: warden.interfaces(object_type).sort_by(&:graphql_name).map { |iface| build_type_name_node(iface) },
63
- fields: build_field_nodes(warden.fields(object_type)),
61
+ interfaces: ints,
62
+ fields: build_field_nodes(@types.fields(object_type)),
64
63
  description: object_type.description,
65
64
  directives: directives(object_type),
66
65
  )
@@ -69,7 +68,7 @@ module GraphQL
69
68
  def build_field_node(field)
70
69
  GraphQL::Language::Nodes::FieldDefinition.new(
71
70
  name: field.graphql_name,
72
- arguments: build_argument_nodes(warden.arguments(field)),
71
+ arguments: build_argument_nodes(@types.arguments(field)),
73
72
  type: build_type_name_node(field.type),
74
73
  description: field.description,
75
74
  directives: directives(field),
@@ -80,7 +79,7 @@ module GraphQL
80
79
  GraphQL::Language::Nodes::UnionTypeDefinition.new(
81
80
  name: union_type.graphql_name,
82
81
  description: union_type.description,
83
- types: warden.possible_types(union_type).sort_by(&:graphql_name).map { |type| build_type_name_node(type) },
82
+ types: @types.possible_types(union_type).sort_by(&:graphql_name).map { |type| build_type_name_node(type) },
84
83
  directives: directives(union_type),
85
84
  )
86
85
  end
@@ -88,9 +87,9 @@ module GraphQL
88
87
  def build_interface_type_node(interface_type)
89
88
  GraphQL::Language::Nodes::InterfaceTypeDefinition.new(
90
89
  name: interface_type.graphql_name,
91
- interfaces: warden.interfaces(interface_type).sort_by(&:graphql_name).map { |type| build_type_name_node(type) },
90
+ interfaces: @types.interfaces(interface_type).sort_by(&:graphql_name).map { |type| build_type_name_node(type) },
92
91
  description: interface_type.description,
93
- fields: build_field_nodes(warden.fields(interface_type)),
92
+ fields: build_field_nodes(@types.fields(interface_type)),
94
93
  directives: directives(interface_type),
95
94
  )
96
95
  end
@@ -98,7 +97,7 @@ module GraphQL
98
97
  def build_enum_type_node(enum_type)
99
98
  GraphQL::Language::Nodes::EnumTypeDefinition.new(
100
99
  name: enum_type.graphql_name,
101
- values: warden.enum_values(enum_type).sort_by(&:graphql_name).map do |enum_value|
100
+ values: @types.enum_values(enum_type).sort_by(&:graphql_name).map do |enum_value|
102
101
  build_enum_value_node(enum_value)
103
102
  end,
104
103
  description: enum_type.description,
@@ -143,7 +142,7 @@ module GraphQL
143
142
  def build_input_object_node(input_object)
144
143
  GraphQL::Language::Nodes::InputObjectTypeDefinition.new(
145
144
  name: input_object.graphql_name,
146
- fields: build_argument_nodes(warden.arguments(input_object)),
145
+ fields: build_argument_nodes(@types.arguments(input_object)),
147
146
  description: input_object.description,
148
147
  directives: directives(input_object),
149
148
  )
@@ -153,7 +152,7 @@ module GraphQL
153
152
  GraphQL::Language::Nodes::DirectiveDefinition.new(
154
153
  name: directive.graphql_name,
155
154
  repeatable: directive.repeatable?,
156
- arguments: build_argument_nodes(warden.arguments(directive)),
155
+ arguments: build_argument_nodes(@types.arguments(directive)),
157
156
  locations: build_directive_location_nodes(directive.locations),
158
157
  description: directive.description,
159
158
  )
@@ -180,7 +179,8 @@ module GraphQL
180
179
  of_type: build_type_name_node(type.of_type)
181
180
  )
182
181
  else
183
- GraphQL::Language::Nodes::TypeName.new(name: type.graphql_name)
182
+ @cached_type_name_nodes ||= {}
183
+ @cached_type_name_nodes[type.graphql_name] ||= GraphQL::Language::Nodes::TypeName.new(name: type.graphql_name)
184
184
  end
185
185
  end
186
186
 
@@ -197,7 +197,7 @@ module GraphQL
197
197
  when "INPUT_OBJECT"
198
198
  GraphQL::Language::Nodes::InputObject.new(
199
199
  arguments: default_value.to_h.map do |arg_name, arg_value|
200
- args = @warden.arguments(type)
200
+ args = @types.arguments(type)
201
201
  arg = args.find { |a| a.keyword.to_s == arg_name.to_s }
202
202
  if arg.nil?
203
203
  raise ArgumentError, "No argument definition on #{type.graphql_name} for argument: #{arg_name.inspect} (expected one of: #{args.map(&:keyword)})"
@@ -237,26 +237,39 @@ module GraphQL
237
237
  end
238
238
 
239
239
  def build_argument_nodes(arguments)
240
- arguments
241
- .map { |arg| build_argument_node(arg) }
242
- .sort_by(&:name)
240
+ if arguments.any?
241
+ nodes = arguments.map { |arg| build_argument_node(arg) }
242
+ nodes.sort_by!(&:name)
243
+ nodes
244
+ else
245
+ arguments
246
+ end
243
247
  end
244
248
 
245
249
  def build_directive_nodes(directives)
246
- if !include_built_in_directives
247
- directives = directives.reject { |directive| directive.default_directive? }
248
- end
249
-
250
250
  directives
251
251
  .map { |directive| build_directive_node(directive) }
252
252
  .sort_by(&:name)
253
253
  end
254
254
 
255
255
  def build_definition_nodes
256
- definitions = []
257
- definitions << build_schema_node if include_schema_node?
258
- definitions += build_directive_nodes(warden.directives)
259
- definitions += build_type_definition_nodes(warden.reachable_types)
256
+ dirs_to_build = @types.directives
257
+ if !include_built_in_directives
258
+ dirs_to_build = dirs_to_build.reject { |directive| directive.default_directive? }
259
+ end
260
+ definitions = build_directive_nodes(dirs_to_build)
261
+ all_types = @types.all_types
262
+ type_nodes = build_type_definition_nodes(all_types + schema.extra_types)
263
+ if @include_one_of
264
+ # This may have been set to true when iterating over all types
265
+ definitions.concat(build_directive_nodes([GraphQL::Schema::Directive::OneOf]))
266
+ end
267
+
268
+ definitions.concat(type_nodes)
269
+ if include_schema_node?
270
+ definitions.unshift(build_schema_node)
271
+ end
272
+
260
273
  definitions
261
274
  end
262
275
 
@@ -275,15 +288,17 @@ module GraphQL
275
288
  end
276
289
 
277
290
  def build_field_nodes(fields)
278
- fields
279
- .map { |field| build_field_node(field) }
280
- .sort_by(&:name)
291
+ f_nodes = fields.map { |field| build_field_node(field) }
292
+ f_nodes.sort_by!(&:name)
293
+ f_nodes
281
294
  end
282
295
 
283
296
  private
284
297
 
285
298
  def include_schema_node?
286
- always_include_schema || !schema_respects_root_name_conventions?(schema)
299
+ always_include_schema ||
300
+ !schema_respects_root_name_conventions?(schema) ||
301
+ !schema.schema_directives.empty?
287
302
  end
288
303
 
289
304
  def schema_respects_root_name_conventions?(schema)
@@ -293,14 +308,14 @@ module GraphQL
293
308
  end
294
309
 
295
310
  def directives(member)
296
- definition_directives(member)
311
+ definition_directives(member, :directives)
297
312
  end
298
313
 
299
- def definition_directives(member)
300
- dirs = if !member.respond_to?(:directives) || member.directives.empty?
301
- []
314
+ def definition_directives(member, directives_method)
315
+ dirs = if !member.respond_to?(directives_method) || member.directives.empty?
316
+ EmptyObjects::EMPTY_ARRAY
302
317
  else
303
- member.directives.map do |dir|
318
+ member.public_send(directives_method).map do |dir|
304
319
  args = []
305
320
  dir.arguments.argument_values.each_value do |arg_value| # rubocop:disable Development/ContextIsPassedCop -- directive instance method
306
321
  arg_defn = arg_value.definition
@@ -314,6 +329,11 @@ module GraphQL
314
329
  )
315
330
  end
316
331
  end
332
+
333
+ # If this schema uses this built-in directive definition,
334
+ # include it in the print-out since it's not part of the spec yet.
335
+ @include_one_of ||= dir.class == GraphQL::Schema::Directive::OneOf
336
+
317
337
  GraphQL::Language::Nodes::Directive.new(
318
338
  name: dir.class.graphql_name,
319
339
  arguments: args
@@ -324,11 +344,7 @@ module GraphQL
324
344
  dirs
325
345
  end
326
346
 
327
- def ast_directives(member)
328
- member.ast_node ? member.ast_node.directives : []
329
- end
330
-
331
- attr_reader :schema, :warden, :always_include_schema,
347
+ attr_reader :schema, :always_include_schema,
332
348
  :include_introspection_types, :include_built_in_directives, :include_built_in_scalars
333
349
  end
334
350
  end