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 Field
7
7
  #
8
8
  # This class holds information about a given field that should be
@@ -14,61 +14,73 @@ module Rails # :nodoc:
14
14
  include Directives
15
15
 
16
16
  delegate :decorate, to: :type_klass
17
- delegate :operation, :variables, to: :parent
17
+ delegate :operation, :variables, :request, to: :parent
18
18
  delegate :method_name, :resolver, :performer, :type_klass, :leaf_type?,
19
- :dynamic_resolver?, to: :field
20
-
21
- parent_memoize :request
19
+ :dynamic_resolver?, :mutation?, to: :field
22
20
 
23
21
  attr_reader :name, :alias_name, :parent, :field, :arguments, :current_object
24
22
 
25
23
  alias args arguments
26
24
 
27
- def initialize(parent, node, data)
25
+ def initialize(parent, node)
28
26
  @parent = parent
29
27
 
30
- @name = data[:name]
31
- @alias_name = data[:alias]
32
-
33
- super(node, data)
34
- end
28
+ @name = node[0]
29
+ @alias_name = node[1]
35
30
 
36
- # Return both the field directives and the request directives
37
- def all_directives
38
- field.all_directives + super
31
+ super(node)
39
32
  end
40
33
 
41
34
  # Override that considers the requested field directives and also the
42
35
  # definition field events, both from itself and its directives events
43
36
  def all_listeners
44
- (request.cache(:listeners)[field] ||= field.all_listeners) + super
37
+ request.nested_cache(:listeners, field) do
38
+ if !field.listeners?
39
+ directive_listeners
40
+ elsif !directives?
41
+ field.all_listeners
42
+ else
43
+ local = directive_listeners
44
+ local.empty? ? field.all_listeners : field.all_listeners + local
45
+ end
46
+ end
45
47
  end
46
48
 
47
49
  # Override that considers the requested field directives and also the
48
50
  # definition field events, both from itself and its directives events
49
51
  def all_events
50
- @all_events ||= Helpers.merge_hash_array(
51
- (request.cache(:events)[field] ||= field.all_events),
52
- super,
53
- )
52
+ request.nested_cache(:events, field) do
53
+ if !field.events?
54
+ directive_events
55
+ elsif !directives?
56
+ field.all_events
57
+ else
58
+ Helpers.merge_hash_array(field.all_events, directive_events)
59
+ end
60
+ end
54
61
  end
55
62
 
56
63
  # Get and cache all the arguments for the field
57
64
  def all_arguments
58
- request.cache(:arguments)[field] ||= begin
59
- if (result = field.all_arguments).any?
60
- result.each_value.map(&:gql_name).zip(result.each_value).to_h
61
- else
62
- {}
65
+ return unless field.arguments?
66
+
67
+ request.nested_cache(:arguments, field) do
68
+ field.all_arguments.each_value.with_object({}) do |argument, hash|
69
+ hash[argument.gql_name] = argument
63
70
  end
64
71
  end
65
72
  end
66
73
 
74
+ # Check if the field is using a directive
75
+ def using?(item_or_symbol)
76
+ super || field.using?(item_or_symbol)
77
+ end
78
+
67
79
  # Assign a given +field+ to this class. The field must be an output
68
80
  # field, which means that +output_type?+ must be true. It also must be
69
81
  # called exactly once per field.
70
- def assing_to(field)
71
- raise ArgumentError, <<~MSG.squish if defined?(@assigned)
82
+ def assign_to(field)
83
+ raise ArgumentError, (+<<~MSG).squish if defined?(@assigned)
72
84
  The "#{gql_name}" field is already assigned to #{@field.inspect}.
73
85
  MSG
74
86
 
@@ -85,10 +97,11 @@ module Rails # :nodoc:
85
97
  (try(:current_object) || try(:type_klass))&.gql_name
86
98
  end
87
99
 
88
- # Check if the field is an entry point, meaning that its parent is the
89
- # operation and it is associated to a schema field
100
+ # Check if the field is an entry point, meaning that it is attached to
101
+ # an owner that has Schema Fields
90
102
  def entry_point?
91
- parent.kind === :operation
103
+ return @entry_point if defined?(@entry_point)
104
+ @entry_point = field.entry_point?
92
105
  end
93
106
 
94
107
  # Fields are assignable because they are actually the selection, so they
@@ -97,6 +110,20 @@ module Rails # :nodoc:
97
110
  true
98
111
  end
99
112
 
113
+ # Check if all the sub fields are broadcastable
114
+ # TODO: Maybe check for interfaces and if all types allow broadcast
115
+ def broadcastable?
116
+ value = field.broadcastable?
117
+ value = schema.config.default_subscription_broadcastable if value.nil?
118
+ value != false
119
+ end
120
+
121
+ # Override this to also check if the key would be added to the response
122
+ # again
123
+ def skipped?
124
+ super || response.key?(gql_name)
125
+ end
126
+
100
127
  # A little extension of the +is_a?+ method that allows checking it using
101
128
  # the underlying +field+
102
129
  def of_type?(klass)
@@ -106,7 +133,7 @@ module Rails # :nodoc:
106
133
  # When the field is invalid, there's no much to do
107
134
  # TODO: Maybe add a invalid event trigger here
108
135
  def resolve_invalid(error = nil)
109
- request.exception_to_error(error, @node) if error.present?
136
+ request.exception_to_error(error, self) if error.present?
110
137
 
111
138
  validate_output!(nil)
112
139
  response.safe_add(gql_name, nil)
@@ -118,26 +145,45 @@ module Rails # :nodoc:
118
145
  # field needs to be redirected to the one from the actual resolved
119
146
  # +object+ type
120
147
  def resolve_with!(object)
148
+ return if skipped?
121
149
  return resolve! if invalid?
122
150
 
123
151
  old_field, @field = @field, object[@field.name]
152
+ request.nested_cache(:listeners, field) { strategy.add_listeners_from(self) }
124
153
  @current_object = object
125
154
  resolve!
126
155
  ensure
127
156
  @field, @current_object = old_field, nil
128
157
  end
129
158
 
159
+ # Build the cache object
160
+ # TODO: Add the arguments into the GID, but the problem is variables
161
+ def cache_dump
162
+ super.merge(field: (field && all_to_gid(field)))
163
+ end
164
+
165
+ # Organize from cache data
166
+ def cache_load(data)
167
+ @name = data[:node][0]
168
+ @alias_name = data[:node][1]
169
+ @field = all_from_gid(data[:field])
170
+ super
171
+
172
+ check_authorization! unless unresolvable?
173
+ end
174
+
130
175
  protected
131
176
 
132
177
  # Perform the organization step
133
178
  def organize_then(&block)
134
179
  super(block) do
135
180
  check_assignment!
136
- check_authorization!
137
181
 
138
- parse_arguments
139
- parse_directives
140
- parse_selection
182
+ parse_directives(@node[3])
183
+ parse_arguments(@node[2])
184
+ parse_selection(@node[4])
185
+
186
+ check_authorization!
141
187
  end
142
188
  end
143
189
 
@@ -149,8 +195,7 @@ module Rails # :nodoc:
149
195
  # Perform the resolve step
150
196
  def resolve_then(&block)
151
197
  stacked do
152
- strategy.perform(self) if field.mutation?
153
- send(field.array? ? 'resolve_many' : 'resolve_one', &block)
198
+ send((field.array? ? :resolve_many : :resolve_one), &block)
154
199
  rescue StandardError => error
155
200
  resolve_invalid(error)
156
201
  end
@@ -181,46 +226,29 @@ module Rails # :nodoc:
181
226
  end
182
227
  end
183
228
 
184
- # This override allows reasigned fields to perform events. This
185
- # happens when fields are originally organized from interfaces. If
186
- # the event is stopped for the object, then it doesn't proceed to the
187
- # strategy implementation, ensuring compatibility
188
- def trigger_event(event_name, **xargs)
189
- return super if !defined?(@current_object) || @current_object.nil?
190
-
191
- listeners = request.cache(:listeners)[field] ||= field.all_listeners
192
- return super unless listeners.include?(event_name)
193
-
194
- callbacks = request.cache(:events)[field] ||= field.all_events
195
- old_events, @all_events = @all_events, callbacks
196
- super
197
- ensure
198
- @all_events = old_events
199
- end
200
-
201
229
  # Check if the field was assigned correctly to an output field
202
230
  def check_assignment!
203
- raise MissingFieldError, <<~MSG.squish if field.nil?
204
- Unable to find a field named "#{gql_name}" on
205
- #{entry_point? ? operation.kind : parent.type_klass.name}.
231
+ raise MissingFieldError, (+<<~MSG).squish if field.nil?
232
+ Unable to find a field named "#{gql_name}" on #{parent.typename}.
206
233
  MSG
207
234
 
208
- raise FieldError, <<~MSG.squish unless field.output_type?
235
+ raise FieldError, (+<<~MSG).squish unless field.output_type?
209
236
  The "#{gql_name}" was assigned to a non-output type of field: #{field.inspect}.
210
237
  MSG
211
238
 
212
- empty_selection = data[:selection].nil? || data[:selection].null?
213
- raise FieldError, <<~MSG.squish if field.leaf_type? && !empty_selection
214
- The "#{gql_name}" was assigned to the #{type_klass.gql_name} which
215
- is a leaf type and does not have nested fields.
216
- MSG
217
-
218
- raise FieldError, <<~MSG.squish if !field.leaf_type? && empty_selection
219
- The "#{gql_name}" was assigned to the #{type_klass.gql_name} which
220
- is not a leaf type and requires a selection of fields.
221
- MSG
239
+ if @node[4].nil?
240
+ raise FieldError, (+<<~MSG).squish if !field.leaf_type?
241
+ The "#{gql_name}" was assigned to the #{type_klass.gql_name} which
242
+ is not a leaf type and requires a selection of fields.
243
+ MSG
244
+ else
245
+ raise FieldError, (+<<~MSG).squish if field.leaf_type?
246
+ The "#{gql_name}" was assigned to the #{type_klass.gql_name} which
247
+ is a leaf type and does not have nested fields.
248
+ MSG
249
+ end
222
250
 
223
- raise DisabledFieldError, <<~MSG.squish if field.disabled?
251
+ raise DisabledFieldError, (+<<~MSG).squish if field.disabled?
224
252
  The "#{gql_name}" was found but it is marked as disabled.
225
253
  MSG
226
254
  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 Fragment
7
7
  #
8
8
  # This class holds information about a given fragment defined using the
@@ -12,19 +12,28 @@ module Rails # :nodoc:
12
12
  include SelectionSet
13
13
  include Directives
14
14
 
15
- attr_reader :name, :type_klass, :request
15
+ attr_reader :name, :type_klass, :request, :current_object
16
16
 
17
- def initialize(request, node, data)
18
- @name = data[:name]
17
+ def initialize(request, node)
18
+ @name = node[0]
19
19
  @request = request
20
20
 
21
- super(node, data)
21
+ super(node)
22
22
 
23
23
  check_duplicated_fragment!
24
24
  end
25
25
 
26
+ # Check if all the sub fields are broadcastable
27
+ def broadcastable?
28
+ selection.each_value.all?(&:broadcastable?)
29
+ end
30
+
31
+ # Check if the fragment has been prepared already
32
+ def prepared?
33
+ defined?(@prepared) && @prepared
34
+ end
35
+
26
36
  # Return a lazy loaded variable proc
27
- # TODO: Mark all the dependent variables
28
37
  def variables
29
38
  Request::Arguments.lazy
30
39
  end
@@ -34,6 +43,16 @@ module Rails # :nodoc:
34
43
  Request::Arguments.operation
35
44
  end
36
45
 
46
+ # Stores all the used variables
47
+ def used_variables
48
+ return @used_variables if defined?(@used_variables)
49
+ end
50
+
51
+ # Stores all the used nested fragments
52
+ def used_fragments
53
+ return @used_fragments if defined?(@used_fragments)
54
+ end
55
+
37
56
  # Spread should always be performed with a current object, thus the
38
57
  # typename comes from it
39
58
  def typename
@@ -42,7 +61,7 @@ module Rails # :nodoc:
42
61
 
43
62
  # Only resolve if the +type_klass+ is equivalent to the given +object+
44
63
  def resolve_with!(object)
45
- return if invalid?
64
+ return if unresolvable?
46
65
 
47
66
  @current_object = object
48
67
  resolve!
@@ -50,6 +69,19 @@ module Rails # :nodoc:
50
69
  @current_object = nil
51
70
  end
52
71
 
72
+ # Build the cache object
73
+ def cache_dump
74
+ super.merge(type_klass: all_to_gid(type_klass))
75
+ end
76
+
77
+ # Organize from cache data
78
+ def cache_load(data)
79
+ @name = data[:node][0]
80
+ @type_klass = all_from_gid(data[:type_klass])
81
+
82
+ super
83
+ end
84
+
53
85
  protected
54
86
 
55
87
  # Fragments always resolve selection unstacked on response, meaning
@@ -58,23 +90,29 @@ module Rails # :nodoc:
58
90
  false
59
91
  end
60
92
 
93
+ # Wrap the field organization with the collection of variables
94
+ def organize
95
+ organize_then { collect_usages { organize_fields } }
96
+ end
97
+
61
98
  # Perform the organization step
62
99
  def organize_then(&block)
63
100
  super(block) do
64
- @type_klass = find_type!(data[:type])
65
- parse_directives(:fragment_definition)
101
+ @type_klass = find_type!(@node[1])
102
+ parse_directives(@node[2], :fragment_definition)
66
103
 
67
104
  check_assignment!
68
- parse_selection
105
+ parse_selection(@node[3])
69
106
  end
70
107
  end
71
108
 
72
109
  # Resolve the spread operation
73
110
  def resolve
74
- return if invalid?
111
+ return if unresolvable?
75
112
 
76
- object = @current_object || type_klass
77
- resolve_then if type_klass =~ object
113
+ type = type_klass
114
+ object = @current_object
115
+ resolve_then if (object.nil? && type&.operational?) || type =~ object
78
116
  end
79
117
 
80
118
  # This will just trigger the selection resolver
@@ -86,32 +124,48 @@ module Rails # :nodoc:
86
124
 
87
125
  # Check if the field was assigned correctly to an output field
88
126
  def check_assignment!
89
- raise ExecutionError, <<~MSG.squish unless type_klass.output_type?
90
- Unable to assing #{type_klass.gql_name} to "#{name}" fragment because
127
+ raise ExecutionError, (+<<~MSG).squish unless type_klass.output_type?
128
+ Unable to assign #{type_klass.gql_name} to "#{name}" fragment because
91
129
  it is not a output type.
92
130
  MSG
93
131
 
94
- raise ExecutionError, <<~MSG.squish if type_klass.leaf_type?
95
- Unable to assing #{type_klass.gql_name} to "#{name}" fragment because
96
- a "#{type_klass.kind}" type can not be the source of a fragmnet.
132
+ raise ExecutionError, (+<<~MSG).squish if type_klass.leaf_type?
133
+ Unable to assign #{type_klass.gql_name} to "#{name}" fragment because
134
+ a "#{type_klass.kind}" type can not be the source of a fragment.
97
135
  MSG
98
136
  end
99
137
 
100
138
  # If there is another fragment with the same name already defined,
101
139
  # raise an error
102
140
  def check_duplicated_fragment!
103
- return unless request.fragments.key?(name)
141
+ other = request.document[1].find do |other|
142
+ other != @node && name == other[0]
143
+ end
104
144
 
145
+ return if other.nil?
105
146
  invalidate!
106
147
 
107
- other_node = request.fragments[name].instance_variable_get(:@node)
108
- location = GraphQL::Native.get_location(other_node)
109
-
110
- request.report_node_error(<<~MSG.squish, @node)
148
+ request.report_node_error((+<<~MSG).squish, self)
111
149
  Duplicated fragment named "#{name}" defined on
112
- line #{location.begin_line}:#{location.begin_column}
150
+ line #{other.begin_line}:#{other.begin_column}
113
151
  MSG
114
152
  end
153
+
154
+ # Use the information on the operation to collect all the variables
155
+ # and nested fragments that were used inside a fragment
156
+ def collect_usages
157
+ vars_total = operation.used_variables.size
158
+ frag_total = operation.used_fragments.size
159
+ yield
160
+ ensure
161
+ if operation.used_variables.size > vars_total
162
+ @used_variables = Set.new(operation.used_variables.to_enum.drop(vars_total))
163
+ end
164
+
165
+ if operation.used_fragments.size > frag_total
166
+ @used_fragments = Set.new(operation.used_fragments.to_enum.drop(frag_total))
167
+ end
168
+ end
115
169
  end
116
170
  end
117
171
  end
@@ -1,14 +1,172 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Rails # :nodoc:
4
- module GraphQL # :nodoc:
5
- class Request # :nodoc:
6
- class Component # :nodoc:
3
+ module Rails
4
+ module GraphQL
5
+ class Request
6
+ class Component
7
7
  # = GraphQL Request Component Subscription Operation
8
8
  #
9
9
  # Handles a subscription operation inside a request.
10
10
  class Operation::Subscription < Operation
11
+ UNSUBSCRIBED_PAYLOAD = { 'more' => false }.freeze
12
+ UNSUBSCRIBED_RESULT = Object.new
13
+
11
14
  redefine_singleton_method(:subscription?) { true }
15
+
16
+ def initialize(*)
17
+ @initial = true
18
+
19
+ super
20
+ end
21
+
22
+ # Fetch the subscription only when necessary
23
+ def subscription
24
+ return unless defined?(@subscription)
25
+ return @subscription if @subscription.is_a?(Request::Subscription)
26
+ @subscription = schema.subscription_provider.fetch(@subscription)
27
+ end
28
+
29
+ # Check if the operation is running in its first iteration, which will
30
+ # produce a subscription to the field
31
+ def subscribing?
32
+ @initial
33
+ end
34
+
35
+ # Check if the current operation is running under broadcasting mode
36
+ def broadcasting?
37
+ request.context.broadcasting == true
38
+ end
39
+
40
+ # Check if the operation can be broadcasted
41
+ def broadcastable?
42
+ return @broadcastable if defined?(@broadcastable)
43
+ @broadcastable = selection.each_value.all?(&:broadcastable?)
44
+ end
45
+
46
+ # Either throw back an empty result or skip the operation
47
+ def no_update!
48
+ subscribing? ? skip! : throw(:skip_subscription_update, EMPTY_HASH)
49
+ end
50
+
51
+ # If unsubscribe is called during an update, skip and return a proper
52
+ # result
53
+ def unsubscribe!
54
+ if subscribing?
55
+ schema.remove_subscriptions(subscription.sid) if subscription.present?
56
+ else
57
+ throw(:skip_subscription_update, UNSUBSCRIBED_RESULT)
58
+ end
59
+ end
60
+
61
+ # Build the cache object
62
+ def cache_dump(initial = true)
63
+ hash = super()
64
+ hash[:initial] = initial
65
+ hash[:broadcastable] = broadcastable?
66
+
67
+ unless initial
68
+ hash[:sid] = @subscription&.sid
69
+ hash[:variables] = @variables
70
+ end
71
+
72
+ hash
73
+ end
74
+
75
+ # Organize from cache data
76
+ def cache_load(data)
77
+ @initial = data[:initial]
78
+ @broadcastable = data[:broadcastable]
79
+
80
+ unless subscribing?
81
+ @variables = data[:variables]
82
+ @subscription = data[:sid]
83
+ end
84
+
85
+ super
86
+ end
87
+
88
+ protected
89
+
90
+ # Rewrite this method so that the subscription can be generated in
91
+ # the right place
92
+ def resolve_then(&block)
93
+ super do
94
+ save_subscription
95
+ trigger_event(:subscribed, subscription: subscription)
96
+ block.call if block.present?
97
+ end
98
+ end
99
+
100
+ # Save the subscription using the schema subscription provider
101
+ def save_subscription
102
+ return unless subscribing? && !(invalid? ||skipped?)
103
+ check_invalid_subscription!
104
+
105
+ @subscription = Request::Subscription.new(request, self)
106
+ request.write_cache_request(subscription.sid, build_subscription_cache)
107
+ schema.add_subscriptions(subscription)
108
+
109
+ request.subscriptions[subscription.sid] = subscription
110
+ rescue => e
111
+ if subscription.present?
112
+ schema.delete_from_cache(subscription.sid)
113
+ schema.remove_subscriptions(subscription.sid)
114
+ end
115
+
116
+ raise SubscriptionError, (+<<~MSG).squish
117
+ Unable to proper setup a subscription on #{display_name}: #{e.message}
118
+ MSG
119
+ end
120
+
121
+ # A subscription is invalid if more than one field was requested or
122
+ # if the only thing in the selection is a spread
123
+ def check_invalid_subscription!
124
+ raise ValidationError, (+<<~MSG).squish if @selection.size != 1
125
+ It has #{@selection.size} and must have 1 single field on it.
126
+ MSG
127
+
128
+ element = @selection.each_value.first
129
+ raise ValidationError, (+<<~MSG).squish unless element.kind == :field
130
+ The only element inside of it must be a field, #{element.kind} found.
131
+ MSG
132
+
133
+ raise ValidationError, (+<<~MSG).squish if element.is_a?(Component::Typename)
134
+ Unable to subscribe to a __typeName field.
135
+ MSG
136
+
137
+ raise ValidationError, (+<<~MSG).squish if element.unresolvable?
138
+ Field is unresolvable.
139
+ MSG
140
+
141
+ raise ValidationError, (+<<~MSG).squish unless schema.accepts_subscription?(self)
142
+ Operation unaccepted.
143
+ MSG
144
+ end
145
+
146
+ # Very similar to the +cache_dump+ from the request, however this
147
+ # will chop the parts exclusively for the subscription
148
+ def build_subscription_cache
149
+ # Prepare each used fragment to be set on cache and on the document
150
+ frag_nodes = []
151
+ fragments = used_fragments.empty? ? nil : begin
152
+ request.fragments.slice(*used_fragments).transform_values do |frag|
153
+ frag_nodes << frag.instance_variable_get(:@node)
154
+ frag.try(:cache_dump)
155
+ end.compact
156
+ end
157
+
158
+ # Return the cache result
159
+ {
160
+ strategy: { class: request.strategy.class },
161
+ operation_name: request.operation_name,
162
+ type_map_version: request.schema.version,
163
+ document: [[@node], frag_nodes.presence],
164
+ errors: request.errors.cache_dump,
165
+ operations: { name => cache_dump(false) },
166
+ fragments: fragments,
167
+ }
168
+ end
169
+
12
170
  end
13
171
  end
14
172
  end