rails-graphql 0.2.1 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (315) hide show
  1. checksums.yaml +4 -4
  2. data/ext/console.rb +18 -0
  3. data/ext/extconf.h +3 -0
  4. data/ext/extconf.rb +1 -54
  5. data/ext/gql_parser.c +631 -0
  6. data/ext/gql_parser.h +21 -0
  7. data/ext/shared.c +477 -0
  8. data/ext/shared.h +177 -0
  9. data/lib/generators/graphql/channel_generator.rb +27 -0
  10. data/lib/generators/graphql/controller_generator.rb +9 -4
  11. data/lib/generators/graphql/install_generator.rb +49 -0
  12. data/lib/generators/graphql/schema_generator.rb +9 -4
  13. data/lib/generators/graphql/templates/channel.erb +7 -0
  14. data/lib/generators/graphql/templates/config.rb +97 -0
  15. data/lib/generators/graphql/templates/controller.erb +2 -0
  16. data/lib/generators/graphql/templates/schema.erb +5 -3
  17. data/lib/gql_parser.so +0 -0
  18. data/lib/rails/graphql/adapters/mysql_adapter.rb +59 -0
  19. data/lib/rails/graphql/adapters/pg_adapter.rb +25 -22
  20. data/lib/rails/graphql/adapters/sqlite_adapter.rb +17 -14
  21. data/lib/rails/graphql/alternative/field_set.rb +48 -0
  22. data/lib/rails/graphql/alternative/mutation.rb +17 -0
  23. data/lib/rails/graphql/alternative/query.rb +98 -0
  24. data/lib/rails/graphql/alternative/subscription.rb +18 -0
  25. data/lib/rails/graphql/alternative.rb +20 -0
  26. data/lib/rails/graphql/argument.rb +25 -26
  27. data/lib/rails/graphql/callback.rb +30 -14
  28. data/lib/rails/graphql/collectors/hash_collector.rb +26 -7
  29. data/lib/rails/graphql/collectors/idented_collector.rb +10 -7
  30. data/lib/rails/graphql/collectors/json_collector.rb +43 -17
  31. data/lib/rails/graphql/collectors.rb +4 -4
  32. data/lib/rails/graphql/config.rb +154 -23
  33. data/lib/rails/graphql/directive/cached_directive.rb +33 -0
  34. data/lib/rails/graphql/directive/deprecated_directive.rb +10 -10
  35. data/lib/rails/graphql/directive/include_directive.rb +4 -4
  36. data/lib/rails/graphql/directive/skip_directive.rb +4 -4
  37. data/lib/rails/graphql/directive/specified_by_directive.rb +24 -0
  38. data/lib/rails/graphql/directive.rb +134 -73
  39. data/lib/rails/graphql/errors.rb +33 -4
  40. data/lib/rails/graphql/event.rb +21 -9
  41. data/lib/rails/graphql/field/authorized_field.rb +17 -6
  42. data/lib/rails/graphql/field/input_field.rb +8 -12
  43. data/lib/rails/graphql/field/mutation_field.rb +43 -9
  44. data/lib/rails/graphql/field/output_field.rb +112 -12
  45. data/lib/rails/graphql/field/proxied_field.rb +35 -26
  46. data/lib/rails/graphql/field/resolved_field.rb +27 -25
  47. data/lib/rails/graphql/field/scoped_config.rb +10 -4
  48. data/lib/rails/graphql/field/subscription_field.rb +123 -0
  49. data/lib/rails/graphql/field/typed_field.rb +69 -24
  50. data/lib/rails/graphql/field.rb +89 -74
  51. data/lib/rails/graphql/global_id.rb +89 -0
  52. data/lib/rails/graphql/helpers/attribute_delegator.rb +5 -5
  53. data/lib/rails/graphql/helpers/inherited_collection/array.rb +51 -0
  54. data/lib/rails/graphql/helpers/inherited_collection/base.rb +45 -0
  55. data/lib/rails/graphql/helpers/inherited_collection/hash.rb +88 -0
  56. data/lib/rails/graphql/helpers/inherited_collection.rb +25 -76
  57. data/lib/rails/graphql/helpers/instantiable.rb +15 -0
  58. data/lib/rails/graphql/helpers/leaf_from_ar.rb +7 -7
  59. data/lib/rails/graphql/helpers/registerable.rb +44 -62
  60. data/lib/rails/graphql/helpers/unregisterable.rb +16 -0
  61. data/lib/rails/graphql/helpers/with_arguments.rb +33 -28
  62. data/lib/rails/graphql/helpers/with_assignment.rb +6 -6
  63. data/lib/rails/graphql/helpers/with_callbacks.rb +28 -11
  64. data/lib/rails/graphql/helpers/with_description.rb +73 -0
  65. data/lib/rails/graphql/helpers/with_directives.rb +58 -30
  66. data/lib/rails/graphql/helpers/with_events.rb +22 -23
  67. data/lib/rails/graphql/helpers/with_fields.rb +86 -26
  68. data/lib/rails/graphql/helpers/with_global_id.rb +22 -0
  69. data/lib/rails/graphql/helpers/with_name.rb +44 -0
  70. data/lib/rails/graphql/helpers/with_namespace.rb +7 -4
  71. data/lib/rails/graphql/helpers/with_owner.rb +8 -7
  72. data/lib/rails/graphql/helpers/with_schema_fields.rb +162 -56
  73. data/lib/rails/graphql/helpers/with_validator.rb +9 -9
  74. data/lib/rails/graphql/helpers.rb +10 -3
  75. data/lib/rails/graphql/introspection.rb +43 -36
  76. data/lib/rails/graphql/railtie.rb +89 -33
  77. data/lib/rails/graphql/railties/app/base_channel.rb +10 -0
  78. data/lib/rails/graphql/railties/app/base_controller.rb +12 -0
  79. data/lib/rails/graphql/railties/app/views/_cable.js.erb +56 -0
  80. data/lib/rails/graphql/railties/app/views/_fetch.js.erb +20 -0
  81. data/lib/rails/graphql/railties/app/views/graphiql.html.erb +101 -0
  82. data/lib/rails/graphql/railties/base_generator.rb +5 -17
  83. data/lib/rails/graphql/railties/channel.rb +157 -0
  84. data/lib/rails/graphql/railties/controller.rb +91 -25
  85. data/lib/rails/graphql/railties/controller_runtime.rb +5 -5
  86. data/lib/rails/graphql/railties/log_subscriber.rb +81 -14
  87. data/lib/rails/graphql/request/arguments.rb +26 -50
  88. data/lib/rails/graphql/request/backtrace.rb +212 -0
  89. data/lib/rails/graphql/request/component/field.rb +98 -70
  90. data/lib/rails/graphql/request/component/fragment.rb +80 -26
  91. data/lib/rails/graphql/request/component/operation/subscription.rb +162 -4
  92. data/lib/rails/graphql/request/component/operation.rb +73 -34
  93. data/lib/rails/graphql/request/component/spread.rb +79 -27
  94. data/lib/rails/graphql/request/component/typename.rb +28 -13
  95. data/lib/rails/graphql/request/component.rb +77 -36
  96. data/lib/rails/graphql/request/context.rb +19 -9
  97. data/lib/rails/graphql/request/errors.rb +16 -6
  98. data/lib/rails/graphql/request/event.rb +23 -8
  99. data/lib/rails/graphql/request/helpers/directives.rb +69 -27
  100. data/lib/rails/graphql/request/helpers/selection_set.rb +57 -25
  101. data/lib/rails/graphql/request/helpers/value_writers.rb +24 -19
  102. data/lib/rails/graphql/request/prepared_data.rb +100 -0
  103. data/lib/rails/graphql/request/steps/authorizable.rb +24 -14
  104. data/lib/rails/graphql/request/steps/organizable.rb +111 -49
  105. data/lib/rails/graphql/request/steps/{prepareable.rb → preparable.rb} +21 -8
  106. data/lib/rails/graphql/request/steps/{resolveable.rb → resolvable.rb} +16 -7
  107. data/lib/rails/graphql/request/strategy/cached_strategy.rb +64 -0
  108. data/lib/rails/graphql/request/strategy/dynamic_instance.rb +6 -6
  109. data/lib/rails/graphql/request/strategy/multi_query_strategy.rb +6 -13
  110. data/lib/rails/graphql/request/strategy/sequenced_strategy.rb +9 -9
  111. data/lib/rails/graphql/request/strategy.rb +147 -77
  112. data/lib/rails/graphql/request/subscription.rb +82 -0
  113. data/lib/rails/graphql/request.rb +353 -104
  114. data/lib/rails/graphql/schema.rb +251 -106
  115. data/lib/rails/graphql/shortcuts.rb +33 -8
  116. data/lib/rails/graphql/source/active_record/builders.rb +64 -51
  117. data/lib/rails/graphql/source/active_record_source.rb +158 -82
  118. data/lib/rails/graphql/source/base.rb +83 -0
  119. data/lib/rails/graphql/source/builder.rb +115 -0
  120. data/lib/rails/graphql/source/scoped_arguments.rb +39 -21
  121. data/lib/rails/graphql/source.rb +90 -228
  122. data/lib/rails/graphql/subscription/provider/action_cable.rb +113 -0
  123. data/lib/rails/graphql/subscription/provider/base.rb +192 -0
  124. data/lib/rails/graphql/subscription/provider.rb +18 -0
  125. data/lib/rails/graphql/subscription/store/base.rb +141 -0
  126. data/lib/rails/graphql/subscription/store/memory.rb +136 -0
  127. data/lib/rails/graphql/subscription/store.rb +19 -0
  128. data/lib/rails/graphql/subscription.rb +17 -0
  129. data/lib/rails/graphql/to_gql.rb +29 -32
  130. data/lib/rails/graphql/type/creator.rb +196 -0
  131. data/lib/rails/graphql/type/enum/directive_location_enum.rb +11 -11
  132. data/lib/rails/graphql/type/enum/type_kind_enum.rb +3 -3
  133. data/lib/rails/graphql/type/enum.rb +44 -50
  134. data/lib/rails/graphql/type/input.rb +92 -25
  135. data/lib/rails/graphql/type/interface.rb +29 -28
  136. data/lib/rails/graphql/type/object/directive_object.rb +10 -9
  137. data/lib/rails/graphql/type/object/enum_value_object.rb +3 -3
  138. data/lib/rails/graphql/type/object/field_object.rb +24 -6
  139. data/lib/rails/graphql/type/object/input_value_object.rb +6 -7
  140. data/lib/rails/graphql/type/object/schema_object.rb +5 -8
  141. data/lib/rails/graphql/type/object/type_object.rb +62 -25
  142. data/lib/rails/graphql/type/object.rb +34 -26
  143. data/lib/rails/graphql/type/scalar/any_scalar.rb +30 -0
  144. data/lib/rails/graphql/type/scalar/bigint_scalar.rb +5 -5
  145. data/lib/rails/graphql/type/scalar/binary_scalar.rb +5 -3
  146. data/lib/rails/graphql/type/scalar/boolean_scalar.rb +8 -8
  147. data/lib/rails/graphql/type/scalar/date_scalar.rb +5 -3
  148. data/lib/rails/graphql/type/scalar/date_time_scalar.rb +5 -3
  149. data/lib/rails/graphql/type/scalar/decimal_scalar.rb +5 -3
  150. data/lib/rails/graphql/type/scalar/float_scalar.rb +5 -5
  151. data/lib/rails/graphql/type/scalar/id_scalar.rb +6 -5
  152. data/lib/rails/graphql/type/scalar/int_scalar.rb +6 -5
  153. data/lib/rails/graphql/type/scalar/json_scalar.rb +41 -0
  154. data/lib/rails/graphql/type/scalar/string_scalar.rb +18 -4
  155. data/lib/rails/graphql/type/scalar/time_scalar.rb +8 -6
  156. data/lib/rails/graphql/type/scalar.rb +26 -23
  157. data/lib/rails/graphql/type/union.rb +21 -18
  158. data/lib/rails/graphql/type.rb +43 -26
  159. data/lib/rails/graphql/type_map.rb +268 -165
  160. data/lib/rails/graphql/uri.rb +167 -0
  161. data/lib/rails/graphql/version.rb +19 -3
  162. data/lib/rails/graphql.rake +3 -0
  163. data/lib/rails/graphql.rb +91 -56
  164. data/lib/rails-graphql.rb +1 -1
  165. data/test/assets/en.yml +29 -0
  166. data/test/assets/introspection-mem.txt +1 -1
  167. data/test/assets/introspection.gql +2 -0
  168. data/test/assets/mem.gql +86 -99
  169. data/test/assets/mysql.gql +406 -0
  170. data/test/assets/sqlite.gql +96 -73
  171. data/test/assets/translate.gql +346 -0
  172. data/test/config.rb +19 -8
  173. data/test/graphql/schema_test.rb +14 -50
  174. data/test/graphql/source_test.rb +8 -85
  175. data/test/graphql/type/enum_test.rb +207 -203
  176. data/test/graphql/type/input_test.rb +14 -9
  177. data/test/graphql/type/interface_test.rb +12 -9
  178. data/test/graphql/type/object_test.rb +8 -2
  179. data/test/graphql/type/scalar/any_scalar_test.rb +38 -0
  180. data/test/graphql/type/scalar/boolean_scalar_test.rb +6 -3
  181. data/test/graphql/type/scalar/json_scalar_test.rb +23 -0
  182. data/test/graphql/type_map_test.rb +63 -81
  183. data/test/graphql/type_test.rb +0 -19
  184. data/test/graphql_test.rb +1 -1
  185. data/test/integration/{authorization/authorization_test.rb → authorization_test.rb} +40 -14
  186. data/test/integration/config.rb +36 -3
  187. data/test/integration/customization_test.rb +39 -0
  188. data/test/integration/global_id_test.rb +99 -0
  189. data/test/integration/memory/star_wars_introspection_test.rb +24 -16
  190. data/test/integration/memory/star_wars_query_test.rb +54 -3
  191. data/test/integration/memory/star_wars_validation_test.rb +3 -3
  192. data/test/integration/mysql/star_wars_introspection_test.rb +25 -0
  193. data/test/integration/persisted_query_test.rb +87 -0
  194. data/test/integration/resolver_precedence_test.rb +154 -0
  195. data/test/integration/schemas/memory.rb +24 -10
  196. data/test/integration/schemas/mysql.rb +62 -0
  197. data/test/integration/schemas/sqlite.rb +21 -12
  198. data/test/integration/sqlite/star_wars_global_id_test.rb +89 -0
  199. data/test/integration/sqlite/star_wars_introspection_test.rb +10 -0
  200. data/test/integration/sqlite/star_wars_query_test.rb +14 -1
  201. data/test/integration/translate_test.rb +73 -0
  202. data/test/test_ext.rb +16 -13
  203. metadata +125 -161
  204. data/ext/depend +0 -3
  205. data/ext/graphqlparser/Ast.cpp +0 -346
  206. data/ext/graphqlparser/Ast.h +0 -1214
  207. data/ext/graphqlparser/AstNode.h +0 -36
  208. data/ext/graphqlparser/AstVisitor.h +0 -137
  209. data/ext/graphqlparser/GraphQLParser.cpp +0 -76
  210. data/ext/graphqlparser/GraphQLParser.h +0 -55
  211. data/ext/graphqlparser/JsonVisitor.cpp +0 -161
  212. data/ext/graphqlparser/JsonVisitor.cpp.inc +0 -456
  213. data/ext/graphqlparser/JsonVisitor.h +0 -121
  214. data/ext/graphqlparser/JsonVisitor.h.inc +0 -110
  215. data/ext/graphqlparser/VERSION +0 -1
  216. data/ext/graphqlparser/c/GraphQLAst.cpp +0 -324
  217. data/ext/graphqlparser/c/GraphQLAst.h +0 -180
  218. data/ext/graphqlparser/c/GraphQLAstForEachConcreteType.h +0 -44
  219. data/ext/graphqlparser/c/GraphQLAstNode.cpp +0 -25
  220. data/ext/graphqlparser/c/GraphQLAstNode.h +0 -33
  221. data/ext/graphqlparser/c/GraphQLAstToJSON.cpp +0 -21
  222. data/ext/graphqlparser/c/GraphQLAstToJSON.h +0 -24
  223. data/ext/graphqlparser/c/GraphQLAstVisitor.cpp +0 -55
  224. data/ext/graphqlparser/c/GraphQLAstVisitor.h +0 -53
  225. data/ext/graphqlparser/c/GraphQLParser.cpp +0 -35
  226. data/ext/graphqlparser/c/GraphQLParser.h +0 -54
  227. data/ext/graphqlparser/dump_json_ast.cpp +0 -48
  228. data/ext/graphqlparser/lexer.lpp +0 -324
  229. data/ext/graphqlparser/parser.ypp +0 -693
  230. data/ext/graphqlparser/parsergen/lexer.cpp +0 -2633
  231. data/ext/graphqlparser/parsergen/lexer.h +0 -528
  232. data/ext/graphqlparser/parsergen/location.hh +0 -189
  233. data/ext/graphqlparser/parsergen/parser.tab.cpp +0 -3300
  234. data/ext/graphqlparser/parsergen/parser.tab.hpp +0 -646
  235. data/ext/graphqlparser/parsergen/position.hh +0 -179
  236. data/ext/graphqlparser/parsergen/stack.hh +0 -156
  237. data/ext/graphqlparser/syntaxdefs.h +0 -19
  238. data/ext/libgraphqlparser/AstNode.h +0 -36
  239. data/ext/libgraphqlparser/CMakeLists.txt +0 -148
  240. data/ext/libgraphqlparser/CONTRIBUTING.md +0 -23
  241. data/ext/libgraphqlparser/GraphQLParser.cpp +0 -76
  242. data/ext/libgraphqlparser/GraphQLParser.h +0 -55
  243. data/ext/libgraphqlparser/JsonVisitor.cpp +0 -161
  244. data/ext/libgraphqlparser/JsonVisitor.h +0 -121
  245. data/ext/libgraphqlparser/LICENSE +0 -22
  246. data/ext/libgraphqlparser/README.clang-tidy +0 -7
  247. data/ext/libgraphqlparser/README.md +0 -84
  248. data/ext/libgraphqlparser/ast/ast.ast +0 -203
  249. data/ext/libgraphqlparser/ast/ast.py +0 -61
  250. data/ext/libgraphqlparser/ast/c.py +0 -100
  251. data/ext/libgraphqlparser/ast/c.pyc +0 -0
  252. data/ext/libgraphqlparser/ast/c_impl.py +0 -61
  253. data/ext/libgraphqlparser/ast/c_impl.pyc +0 -0
  254. data/ext/libgraphqlparser/ast/c_visitor_impl.py +0 -39
  255. data/ext/libgraphqlparser/ast/c_visitor_impl.pyc +0 -0
  256. data/ext/libgraphqlparser/ast/casing.py +0 -26
  257. data/ext/libgraphqlparser/ast/casing.pyc +0 -0
  258. data/ext/libgraphqlparser/ast/cxx.py +0 -197
  259. data/ext/libgraphqlparser/ast/cxx.pyc +0 -0
  260. data/ext/libgraphqlparser/ast/cxx_impl.py +0 -61
  261. data/ext/libgraphqlparser/ast/cxx_impl.pyc +0 -0
  262. data/ext/libgraphqlparser/ast/cxx_json_visitor_header.py +0 -42
  263. data/ext/libgraphqlparser/ast/cxx_json_visitor_header.pyc +0 -0
  264. data/ext/libgraphqlparser/ast/cxx_json_visitor_impl.py +0 -80
  265. data/ext/libgraphqlparser/ast/cxx_json_visitor_impl.pyc +0 -0
  266. data/ext/libgraphqlparser/ast/cxx_visitor.py +0 -64
  267. data/ext/libgraphqlparser/ast/cxx_visitor.pyc +0 -0
  268. data/ext/libgraphqlparser/ast/js.py +0 -65
  269. data/ext/libgraphqlparser/ast/license.py +0 -10
  270. data/ext/libgraphqlparser/ast/license.pyc +0 -0
  271. data/ext/libgraphqlparser/c/GraphQLAstNode.cpp +0 -25
  272. data/ext/libgraphqlparser/c/GraphQLAstNode.h +0 -33
  273. data/ext/libgraphqlparser/c/GraphQLAstToJSON.cpp +0 -21
  274. data/ext/libgraphqlparser/c/GraphQLAstToJSON.h +0 -24
  275. data/ext/libgraphqlparser/c/GraphQLAstVisitor.cpp +0 -55
  276. data/ext/libgraphqlparser/c/GraphQLAstVisitor.h +0 -53
  277. data/ext/libgraphqlparser/c/GraphQLParser.cpp +0 -35
  278. data/ext/libgraphqlparser/c/GraphQLParser.h +0 -54
  279. data/ext/libgraphqlparser/clang-tidy-all.sh +0 -3
  280. data/ext/libgraphqlparser/cmake/version.cmake +0 -16
  281. data/ext/libgraphqlparser/dump_json_ast.cpp +0 -48
  282. data/ext/libgraphqlparser/go/README.md +0 -20
  283. data/ext/libgraphqlparser/go/callbacks.go +0 -18
  284. data/ext/libgraphqlparser/go/gotest.go +0 -64
  285. data/ext/libgraphqlparser/lexer.lpp +0 -324
  286. data/ext/libgraphqlparser/libgraphqlparser.pc.in +0 -11
  287. data/ext/libgraphqlparser/parser.ypp +0 -693
  288. data/ext/libgraphqlparser/parsergen/lexer.cpp +0 -2633
  289. data/ext/libgraphqlparser/parsergen/lexer.h +0 -528
  290. data/ext/libgraphqlparser/parsergen/location.hh +0 -189
  291. data/ext/libgraphqlparser/parsergen/parser.tab.cpp +0 -3300
  292. data/ext/libgraphqlparser/parsergen/parser.tab.hpp +0 -646
  293. data/ext/libgraphqlparser/parsergen/position.hh +0 -179
  294. data/ext/libgraphqlparser/parsergen/stack.hh +0 -156
  295. data/ext/libgraphqlparser/python/CMakeLists.txt +0 -14
  296. data/ext/libgraphqlparser/python/README.md +0 -5
  297. data/ext/libgraphqlparser/python/example.py +0 -31
  298. data/ext/libgraphqlparser/syntaxdefs.h +0 -19
  299. data/ext/libgraphqlparser/test/BuildCAPI.c +0 -5
  300. data/ext/libgraphqlparser/test/CMakeLists.txt +0 -25
  301. data/ext/libgraphqlparser/test/JsonVisitorTests.cpp +0 -28
  302. data/ext/libgraphqlparser/test/ParserTests.cpp +0 -352
  303. data/ext/libgraphqlparser/test/kitchen-sink.graphql +0 -59
  304. data/ext/libgraphqlparser/test/kitchen-sink.json +0 -1
  305. data/ext/libgraphqlparser/test/schema-kitchen-sink.graphql +0 -78
  306. data/ext/libgraphqlparser/test/schema-kitchen-sink.json +0 -1
  307. data/ext/libgraphqlparser/test/valgrind.supp +0 -33
  308. data/ext/version.cpp +0 -21
  309. data/lib/graphqlparser.so +0 -0
  310. data/lib/rails/graphql/native/functions.rb +0 -38
  311. data/lib/rails/graphql/native/location.rb +0 -41
  312. data/lib/rails/graphql/native/pointers.rb +0 -23
  313. data/lib/rails/graphql/native/visitor.rb +0 -349
  314. data/lib/rails/graphql/native.rb +0 -56
  315. data/test/integration/schemas/authorization.rb +0 -12
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Rails # :nodoc:
4
- module GraphQL # :nodoc:
5
- class Request # :nodoc:
3
+ module Rails
4
+ module GraphQL
5
+ class Request
6
6
  # = GraphQL Request Component Operation
7
7
  #
8
8
  # This class holds information about a given operation. This will guide
@@ -16,9 +16,9 @@ module Rails # :nodoc:
16
16
  class << self
17
17
  alias type kind
18
18
 
19
- # Helper method to initialize an operation given the data
20
- def build(request, node, data)
21
- request.build(const_get(data[:kind].classify), request, node, data)
19
+ # Helper method to initialize an operation given the node
20
+ def build(request, node)
21
+ request.build(const_get(node.type.to_s.classify), request, node)
22
22
  end
23
23
 
24
24
  # Rewrite the kind to always return +:operation+
@@ -49,6 +49,7 @@ module Rails # :nodoc:
49
49
  autoload :Subscription
50
50
 
51
51
  delegate :type, :query?, :mutation?, :subscription?, to: :class
52
+ delegate :schema, to: :strategy
52
53
 
53
54
  attr_reader :name, :variables, :arguments, :request
54
55
 
@@ -57,11 +58,11 @@ module Rails # :nodoc:
57
58
  alias vars variables
58
59
  alias all_arguments arguments
59
60
 
60
- def initialize(request, node, data)
61
- @name = data[:name]
61
+ def initialize(request, node)
62
+ @name = node[1]
62
63
  @request = request
63
64
 
64
- super(node, data)
65
+ super(node)
65
66
 
66
67
  check_invalid_operation!
67
68
  end
@@ -72,6 +73,13 @@ module Rails # :nodoc:
72
73
  schema.fields_for(type)
73
74
  end
74
75
 
76
+ # Allow accessing the fake type form the schema. It's used for
77
+ # inline spreads without a specified type
78
+ def type_klass
79
+ return @type_klass if defined?(@type_klass)
80
+ @type_klass = schema.public_send("#{type}_type")
81
+ end
82
+
75
83
  # The typename is always based on the fake name used for the set of
76
84
  # schema fields
77
85
  def typename
@@ -88,22 +96,48 @@ module Rails # :nodoc:
88
96
  response.safe_add(name, nil) if stacked_selection?
89
97
  end
90
98
 
91
- # Stores all the used arguments to report not used ones
99
+ # Stores all the used variables to report not used ones
92
100
  def used_variables
93
101
  @used_variables ||= Set.new
94
102
  end
95
103
 
104
+ # Stores all the used fragments
105
+ def used_fragments
106
+ @used_fragments ||= Set.new
107
+ end
108
+
96
109
  # A fast way to access the correct display name for log or errors
97
110
  def log_source
98
- @log_source ||= name.blank? ? type : "#{name} #{type}"
111
+ @log_source ||= name.blank? ? type : +"#{name} #{type}"
112
+ end
113
+
114
+ # The hash of operations must take into consideration the used fragments
115
+ def hash
116
+ return super unless defined?(@used_fragments)
117
+
118
+ super ^ used_fragments.reduce(0) do |value, fragment|
119
+ value ^ request.fragments[fragment].hash
120
+ end
121
+ end
122
+
123
+ # Build the cache object
124
+ def cache_dump
125
+ super.merge(type: self.class)
126
+ end
127
+
128
+ # Organize from cache data
129
+ def cache_load(data)
130
+ @name = data[:node][1]
131
+
132
+ super
99
133
  end
100
134
 
101
135
  protected
102
136
 
103
137
  # Trigger an specific event with the +type+ of the operation
104
138
  def organize
139
+ trigger_event(type)
105
140
  organize_then do
106
- trigger_event(type)
107
141
  yield if block_given?
108
142
  organize_fields
109
143
  report_unused_variables
@@ -113,15 +147,15 @@ module Rails # :nodoc:
113
147
  # Perform the organization step
114
148
  def organize_then(&block)
115
149
  super(block) do
116
- parse_variables
117
- parse_directives(type)
118
- parse_selection
150
+ parse_variables(@node[2])
151
+ parse_directives(@node[3], type)
152
+ parse_selection(@node[4])
119
153
  end
120
154
  end
121
155
 
122
156
  # Resolve all the fields
123
- def resolve
124
- resolve_then { resolve_fields }
157
+ def resolve_then(&block)
158
+ super(block) { resolve_fields }
125
159
  end
126
160
 
127
161
  # Don't stack over response when the operation doesn't have a name
@@ -133,42 +167,47 @@ module Rails # :nodoc:
133
167
 
134
168
  # Name used for debug purposes
135
169
  def display_name
136
- @display_name ||= "#{type.to_s.titlecase} #{name.presence || '__default__'}"
170
+ @display_name ||= +"#{type.to_s.titlecase} #{name.presence || '__default__'}"
137
171
  end
138
172
 
139
- # Add an error for each not used variable and then clean up some data
173
+ # Add an error for each not used variable
140
174
  def report_unused_variables
175
+ return if arguments.nil?
176
+
141
177
  (arguments.keys - used_variables.to_a).each do |key|
142
178
  argument = arguments[key]
143
- request.report_node_error(<<~MSG.squish, argument.node || @node)
144
- Unused variable $#{argument.gql_name} on #{log_source}.
179
+ request.report_node_error((+<<~MSG).squish, argument.node)
180
+ Variable $#{argument.gql_name} was provided to #{log_source} but not used.
145
181
  MSG
146
182
  end
147
183
 
148
- @arguments = nil
184
+ # Report all used variables to the request for greater scope
185
+ request.instance_variable_get(:@used_variables).merge(arguments.keys)
186
+
187
+ # Clear anything that is not necessary anymore
149
188
  @used_variables = nil
150
189
  end
151
190
 
152
191
  # If there is another operation with the same name already defined,
153
192
  # raise an error. If an anonymous was started, then any other
154
- # operatios is invalid.
193
+ # operations is invalid.
155
194
  def check_invalid_operation!
156
- if request.operations.key?(nil)
157
- invalidate!
195
+ other = request.document[0].find do |other|
196
+ other != @node && name == other[1]
197
+ end
198
+
199
+ return if other.nil?
200
+ invalidate!
158
201
 
159
- request.report_node_error(<<~MSG.squish, @node)
202
+ if name.nil?
203
+ request.report_node_error((+<<~MSG).squish, self)
160
204
  Unable to process the operation #{display_name} when the document
161
205
  contain multiple anonymous operations.
162
206
  MSG
163
- elsif request.operations.key?(name)
164
- invalidate!
165
-
166
- other_node = request.operations[name].instance_variable_get(:@node)
167
- location = GraphQL::Native.get_location(other_node)
168
-
169
- request.report_node_error(<<~MSG.squish, @node)
207
+ else
208
+ request.report_node_error((+<<~MSG).squish, self)
170
209
  Duplicated operation named "#{name}" defined on
171
- line #{location.begin_line}:#{location.begin_column}.
210
+ line #{other.begin_line}:#{other.begin_column}.
172
211
  MSG
173
212
  end
174
213
  end
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Rails # :nodoc:
4
- module GraphQL # :nodoc:
5
- class Request # :nodoc:
3
+ module Rails
4
+ module GraphQL
5
+ class Request
6
6
  # = GraphQL Request Component Spread
7
7
  #
8
8
  # This class holds information about a given spread that should be
@@ -11,20 +11,18 @@ module Rails # :nodoc:
11
11
  include SelectionSet
12
12
  include Directives
13
13
 
14
- delegate :operation, :typename, to: :parent
14
+ delegate :operation, :typename, :request, to: :parent
15
15
  delegate :variables, to: :operation
16
16
 
17
- parent_memoize :request
17
+ attr_reader :name, :parent, :fragment, :current_object, :type_klass
18
18
 
19
- attr_reader :name, :parent, :fragment, :type_klass
20
-
21
- def initialize(parent, node, data)
19
+ def initialize(parent, node)
22
20
  @parent = parent
23
21
 
24
- @name = data[:name]
25
- @inline = data[:inline]
22
+ @name = node[0]
23
+ @inline = name.nil?
26
24
 
27
- super(node, data)
25
+ super(node)
28
26
  end
29
27
 
30
28
  # Check if the object is an inline spread
@@ -32,9 +30,14 @@ module Rails # :nodoc:
32
30
  @inline.present?
33
31
  end
34
32
 
33
+ # Check if all the sub fields or the fragment is broadcastable
34
+ def broadcastable?
35
+ inline? ? selection.each_value.all?(&:broadcastable?) : fragment.broadcastable?
36
+ end
37
+
35
38
  # Redirect to the fragment or check the inline type before resolving
36
39
  def resolve_with!(object)
37
- return if invalid?
40
+ return if unresolvable?
38
41
 
39
42
  @current_object = object
40
43
  resolve!
@@ -42,6 +45,25 @@ module Rails # :nodoc:
42
45
  @current_object = nil
43
46
  end
44
47
 
48
+ # Build the cache object
49
+ def cache_dump
50
+ inline? ? super.merge(type_klass: all_to_gid(type_klass)) : super
51
+ end
52
+
53
+ # Organize from cache data
54
+ def cache_load(data)
55
+ @name = data[:node][0]
56
+ @inline = name.nil?
57
+
58
+ if inline?
59
+ @type_klass = all_from_gid(data[:type_klass])
60
+ else
61
+ collect_fragment
62
+ end
63
+
64
+ super
65
+ end
66
+
45
67
  protected
46
68
 
47
69
  # Spread always resolve inline selection unstacked on response,
@@ -51,13 +73,13 @@ module Rails # :nodoc:
51
73
  end
52
74
 
53
75
  # Just provide the correct location for directives
54
- def parse_directives
55
- super(inline? ? :inline_fragment : :fragment_spread)
76
+ def parse_directives(nodes)
77
+ super(nodes, (inline? ? :inline_fragment : :fragment_spread))
56
78
  end
57
79
 
58
80
  # Scope the arguments whenever stacked within a spread
59
81
  def stacked(*)
60
- Request::Arguments.scoped(operation) { super }
82
+ Arguments.scoped(operation) { super }
61
83
  end
62
84
 
63
85
  # Normal mode of the organize step
@@ -66,7 +88,6 @@ module Rails # :nodoc:
66
88
  def organize
67
89
  inline? ? organize_then { organize_fields } : organize_then do
68
90
  run_on_fragment(:organize!)
69
- # Ensure necessary variables
70
91
  end
71
92
  end
72
93
 
@@ -74,33 +95,42 @@ module Rails # :nodoc:
74
95
  def organize_then(&block)
75
96
  super(block) do
76
97
  if inline?
77
- @type_klass = find_type!(data[:type])
78
- parse_selection
98
+ @type_klass = @node[1].nil? ? parent.type_klass : find_type!(@node[1])
99
+ parse_directives(@node[2])
100
+ parse_selection(@node[3])
79
101
  else
80
- @fragment = request.fragments[name]
81
- raise ArgumentError, <<~MSG.squish if @fragment.nil?
102
+ parse_directives(@node[2])
103
+ @fragment = collect_fragment
104
+ raise ArgumentError, (+<<~MSG).squish if @fragment.nil?
82
105
  The "#{name}" fragment is not defined in this request.
83
106
  MSG
84
107
  end
85
-
86
- parse_directives
87
108
  end
88
109
  end
89
110
 
90
111
  # Spread has a special behavior when using a fragment
91
112
  def prepare
92
113
  return super if inline?
93
- raise 'Prepare with fragment not implemented yet'
114
+ catch(:fragment_prepared) do
115
+ return fragment.prepare!
116
+ end
117
+
118
+ invalidate!
119
+ raise ExecutionError, (+<<~MSG).squish
120
+ Fields inside the "#{name}" fragment tried to be prepared more than once.
121
+ This feature is not supported yet.
122
+ MSG
94
123
  end
95
124
 
96
125
  # Resolve the spread operation
97
126
  def resolve
98
- return if invalid?
127
+ return if unresolvable?
99
128
 
100
- object = (defined?(@current_object) && @current_object) || parent.type_klass
129
+ object = (defined?(@current_object) && @current_object)
130
+ object ||= parent.type_klass unless parent.kind == :operation
101
131
  return run_on_fragment(:resolve_with!, object) unless inline?
102
132
 
103
- super if type_klass =~ object
133
+ super if (object.nil? && type_klass&.operational?) || type_klass =~ object
104
134
  end
105
135
 
106
136
  # This will just trigger the selection resolver
@@ -111,7 +141,29 @@ module Rails # :nodoc:
111
141
  # Most of the things that are redirected to the fragment needs to run
112
142
  # inside a arguments scoped
113
143
  def run_on_fragment(method_name, *args)
114
- Request::Arguments.scoped(operation) { fragment.public_send(method_name, *args) }
144
+ Arguments.scoped(operation) { fragment.public_send(method_name, *args) }
145
+ end
146
+
147
+ # Only initialize the fragment once and only when requested for the
148
+ # first time. It also reports to the operation the used variables
149
+ # within the fragment
150
+ def collect_fragment
151
+ node = request.fragments[name]
152
+
153
+ if node.is_a?(Component::Fragment)
154
+ unless (used_variables = node.used_variables).nil?
155
+ operation.used_variables.merge(used_variables)
156
+ end
157
+
158
+ unless (used_fragments = node.used_fragments).nil?
159
+ operation.used_fragments.merge(used_fragments)
160
+ end
161
+
162
+ node
163
+ else
164
+ operation.used_fragments << name
165
+ request.fragments[name] = request.build(Component::Fragment, request, node)
166
+ end
115
167
  end
116
168
  end
117
169
  end
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Rails # :nodoc:
4
- module GraphQL # :nodoc:
5
- class Request # :nodoc:
3
+ module Rails
4
+ module GraphQL
5
+ class Request
6
6
  # = GraphQL Request Component Typename
7
7
  #
8
8
  # Extra component which simulates a field that its only purpose is to
@@ -11,11 +11,9 @@ module Rails # :nodoc:
11
11
  include ValueWriters
12
12
  include Directives
13
13
 
14
- delegate :operation, to: :parent
14
+ delegate :operation, :request, to: :parent
15
15
  delegate :variables, to: :operation
16
16
 
17
- parent_memoize :request
18
-
19
17
  attr_reader :name, :alias_name, :parent
20
18
 
21
19
  # Rewrite the kind to always return +:field+
@@ -23,13 +21,13 @@ module Rails # :nodoc:
23
21
  :field
24
22
  end
25
23
 
26
- def initialize(parent, node, data)
24
+ def initialize(parent, node)
27
25
  @parent = parent
28
26
 
29
- @name = data[:name]
30
- @alias_name = data[:alias]
27
+ @name = node[0]
28
+ @alias_name = node[1]
31
29
 
32
- super(node, data)
30
+ super(node)
33
31
  end
34
32
 
35
33
  # Set the value that the field will be resolved as
@@ -47,7 +45,24 @@ module Rails # :nodoc:
47
45
 
48
46
  # Write the typename information
49
47
  def write_value(value)
50
- response.serialize(Type::Scalar::StringScalar, gql_name, value)
48
+ response.serialize(Type::Scalar::StringScalar, gql_name, value.itself)
49
+ end
50
+
51
+ # Typename is always broadcastable
52
+ def broadcastable?
53
+ true
54
+ end
55
+
56
+ # Prepare is not necessary for this field
57
+ def prepare!
58
+ end
59
+
60
+ # Organize from cache data
61
+ def cache_load(data)
62
+ @name = data[:node][0]
63
+ @alias_name = data[:node][1]
64
+
65
+ super
51
66
  end
52
67
 
53
68
  protected
@@ -59,14 +74,14 @@ module Rails # :nodoc:
59
74
 
60
75
  # Perform the organization step
61
76
  def organize_then(&block)
62
- super(block) { parse_directives(:field) }
77
+ super(block) { parse_directives(@node[3], :field) }
63
78
  end
64
79
 
65
80
  # Go through the right flow to write the value
66
81
  def resolve_then
67
82
  super do
68
83
  typename = @typename || parent.typename
69
- raise InvalidValueError, <<~MSG.squish if typename.blank?
84
+ raise InvalidValueError, (+<<~MSG).squish if typename.blank?
70
85
  The #{gql_name} field value cannot be null.
71
86
  MSG
72
87
 
@@ -1,80 +1,104 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Rails # :nodoc:
4
- module GraphQL # :nodoc:
5
- class Request # :nodoc:
3
+ module Rails
4
+ module GraphQL
5
+ class Request
6
6
  # = GraphQL Request Component
7
7
  #
8
8
  # Component is an abstraction of any possible type of object represented
9
9
  # by a not of the document of a request. This class helps building
10
10
  # cross-component features, like holding event listeners, setting up
11
- # commom initializer and providing helpers
11
+ # common initializer and providing helpers
12
12
  class Component
13
13
  extend ActiveSupport::Autoload
14
14
 
15
15
  include Request::Organizable
16
- include Request::Prepareable
17
- include Request::Resolveable
16
+ include Request::Preparable
17
+ include Request::Resolvable
18
18
 
19
19
  class << self
20
20
  # Return the kind of the component
21
21
  def kind
22
22
  @kind ||= name.demodulize.underscore.to_sym
23
23
  end
24
-
25
- # Helper to memoize results from parent delegation
26
- def parent_memoize(*methods)
27
- methods.each do |method_name|
28
- define_method(method_name) do
29
- result = parent.public_send(method_name)
30
- define_singleton_method(method_name) { result }
31
- result
32
- end
33
- end
34
- end
35
24
  end
36
25
 
37
- attr_reader :data
38
-
39
- delegate :schema, :visitor, :response, :strategy, to: :request
26
+ delegate :visitor, :response, :strategy, to: :request
40
27
  delegate :find_type!, :find_directive!, :trigger_event, to: :strategy
41
- delegate :memo, to: :operation
28
+ delegate :memo, :schema, to: :operation
42
29
  delegate :kind, to: :class
43
30
 
44
31
  alias of_type? is_a?
45
32
 
46
33
  eager_autoload do
47
34
  autoload :Field
48
- autoload :Fragment
49
35
  autoload :Operation
50
- autoload :Spread
51
- autoload :Typename
52
36
  end
53
37
 
54
- def initialize(node, data)
38
+ autoload :Fragment
39
+ autoload :Spread
40
+ autoload :Typename
41
+
42
+ def initialize(node)
55
43
  @node = node
56
- @data = data
57
44
  end
58
45
 
59
46
  # Check if the component is in a invalid state
60
47
  def invalid?
61
- defined?(@invalid) && @invalid
48
+ defined?(@invalid) && @invalid.present?
49
+ end
50
+
51
+ # Check if the component is marked as skipped
52
+ def skipped?
53
+ defined?(@skipped) && @skipped
54
+ end
55
+
56
+ # Just a fancy name for invalid or skipped
57
+ def unresolvable?
58
+ invalid? || skipped?
62
59
  end
63
60
 
64
61
  # Mark the component as invalid
65
- def invalidate!
66
- @invalid = true
62
+ def invalidate!(type = true)
63
+ @invalid = type
64
+ end
65
+
66
+ # Skip the component
67
+ def skip!
68
+ @skipped = true
67
69
  end
68
70
 
69
- # Normaly, components are not assignable, only fields are
71
+ # Normally, components are not assignable, only fields are
70
72
  def assignable?
71
73
  false
72
74
  end
73
75
 
76
+ # Get an identifier of the component
77
+ def hash
78
+ @node.hash
79
+ end
80
+
81
+ # Build the cache object
82
+ def cache_dump
83
+ hash = { node: @node }
84
+ hash[:invalid] = @invalid if defined?(@invalid) && @invalid != :authorization
85
+ hash[:skipped] = @skipped if defined?(@skipped) && @skipped
86
+ hash.merge!(super)
87
+ hash
88
+ end
89
+
90
+ # Organize from cache data
91
+ def cache_load(data)
92
+ @node = data[:node]
93
+ @invalid = data[:invalid] if data.key?(:invalid)
94
+ @skipped = data[:skipped] if data.key?(:skipped)
95
+ super
96
+ end
97
+
74
98
  protected
75
99
 
76
100
  # It's extremely important to have a way to access the current request
77
- # since not all objects stores s direct pointer to it
101
+ # since not all objects stores a direct pointer to it
78
102
  def request
79
103
  raise NotImplementedError
80
104
  end
@@ -86,13 +110,30 @@ module Rails # :nodoc:
86
110
 
87
111
  # Run a given block and ensure to capture exceptions to set them as
88
112
  # errors
89
- def capture_exception(stage, invalidate = false)
90
- yield
91
- rescue StandardError => error
92
- invalidate! if invalidate
113
+ def report_exception(error)
114
+ return if request.rescue_with_handler(error, source: self) == false
115
+
116
+ Backtrace.print(error, self, request)
117
+
93
118
  stack_path = request.stack_to_path
94
119
  stack_path << gql_name if respond_to?(:gql_name) && gql_name.present?
95
- request.exception_to_error(error, @node, path: stack_path, stage: stage.to_s)
120
+ request.exception_to_error(error, self, path: stack_path, stage: strategy.stage.to_s)
121
+ end
122
+
123
+ private
124
+
125
+ # Properly transform values to string gid
126
+ def all_to_gid(enum)
127
+ (enum.is_a?(Enumerable) ? enum : enum.then).each do |item|
128
+ item.to_gid.to_s
129
+ end
130
+ end
131
+
132
+ # Properly recover values from gid string
133
+ def all_from_gid(enum)
134
+ (enum.is_a?(Enumerable) ? enum : enum.then).each do |item|
135
+ GraphQL::GlobalID.find(item)
136
+ end
96
137
  end
97
138
  end
98
139
  end
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Rails # :nodoc:
4
- module GraphQL # :nodoc:
5
- class Request # :nodoc:
3
+ module Rails
4
+ module GraphQL
5
+ class Request
6
6
  # = GraphQL Request Context
7
7
  #
8
8
  # This class is used as context for the response while processing fields,
@@ -23,9 +23,14 @@ module Rails # :nodoc:
23
23
  @stack.shift
24
24
  end
25
25
 
26
- # Find the parent object
27
- def parent
28
- @stack[1]
26
+ # Return a duplicated version of the stack, for safety
27
+ def stack
28
+ @stack.dup
29
+ end
30
+
31
+ # Get a value at the given +index+
32
+ def at(index)
33
+ @stack[index]
29
34
  end
30
35
 
31
36
  # Get all ancestors objects
@@ -33,10 +38,15 @@ module Rails # :nodoc:
33
38
  @stack[1..-1]
34
39
  end
35
40
 
36
- # Get the current value, which basically means basically the first item
37
- # on the current stafck
41
+ # Find the parent object
42
+ def parent
43
+ at(1)
44
+ end
45
+
46
+ # Get the current value, which basically means the first item
47
+ # on the current stack
38
48
  def current_value
39
- @stack[0]
49
+ at(0)
40
50
  end
41
51
 
42
52
  # Change the current value, either form hits or the actual value