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,35 +1,82 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Rails # :nodoc:
4
- module GraphQL # :nodoc:
3
+ module Rails
4
+ module GraphQL
5
5
  # = GraphQL Output Field
6
6
  #
7
7
  # Most of the fields in a GraphQL operation are output fields or similar or
8
8
  # proxies of it. They can express both leaf and branch data. They can also
9
9
  # be the entry point of a GraphQL request.
10
+ #
11
+ # ==== Options
12
+ #
13
+ # * <tt>:method_name</tt> - The name of the method used to fetch the field data
14
+ # (defaults to nil).
15
+ # * <tt>:deprecated</tt> - A shortcut to adding a deprecated directive to the field
16
+ # (defaults to nil).
10
17
  class Field::OutputField < Field
18
+ # Do not change this order because it can affect how events work. Callback
19
+ # must always come after events
11
20
  include Helpers::WithArguments
12
- include Helpers::WithValidator
13
21
  include Helpers::WithEvents
14
22
  include Helpers::WithCallbacks
15
23
 
24
+ include Helpers::WithGlobalID
25
+ include Helpers::WithValidator
26
+
16
27
  include Field::AuthorizedField
17
28
  include Field::ResolvedField
18
29
  include Field::TypedField
19
30
 
20
31
  module Proxied # :nodoc: all
21
- def initialize(*args, **xargs, &block)
22
- @method_name = xargs.delete(:method_name) if xargs.key?(:method_name)
23
- super(*args, **xargs, &block)
24
- end
32
+ Field.proxyable_methods %w[broadcastable?], klass: self
25
33
 
26
34
  def all_arguments
27
- field.arguments.merge(super)
35
+ inherited = field.all_arguments
36
+ return inherited unless defined?(@arguments)
37
+ inherited.blank? ? super : inherited.merge(super)
28
38
  end
29
39
 
30
40
  def has_argument?(name)
31
41
  super || field.has_argument?(name)
32
42
  end
43
+
44
+ def arguments?
45
+ super || field.arguments?
46
+ end
47
+
48
+ # TODO: Break events into directive/type/local
49
+ # because type events should only be added
50
+ # from the proxy
51
+ def all_events
52
+ if (inherited = super).nil?
53
+ field.all_events
54
+ elsif (proxied = field.all_events).nil?
55
+ inherited
56
+ else
57
+ # The order is reversed because events from
58
+ # the proxy must come first
59
+ Helpers.merge_hash_array(proxied, inherited)
60
+ end
61
+ end
62
+
63
+ def events?
64
+ super || field.events?
65
+ end
66
+
67
+ def all_listeners
68
+ if (inherited = super).nil?
69
+ field.all_listeners
70
+ elsif (proxied = field.all_listeners).nil?
71
+ inherited
72
+ else
73
+ proxied + inherited
74
+ end
75
+ end
76
+
77
+ def listeners?
78
+ super || field.listeners?
79
+ end
33
80
  end
34
81
 
35
82
  redefine_singleton_method(:output_type?) { true }
@@ -37,9 +84,10 @@ module Rails # :nodoc:
37
84
 
38
85
  def initialize(*args, method_name: nil, deprecated: false, **xargs, &block)
39
86
  @method_name = method_name.to_s.underscore.to_sym unless method_name.nil?
87
+ @broadcastable = xargs.delete(:broadcastable) if xargs.key?(:broadcastable)
40
88
 
41
89
  if deprecated.present?
42
- xargs[:directives] = Array.wrap(xargs[:directives])
90
+ xargs[:directives] = ::Array.wrap(xargs[:directives])
43
91
  xargs[:directives] << Directive::DeprecatedDirective.new(
44
92
  reason: (deprecated.is_a?(String) ? deprecated : nil),
45
93
  )
@@ -48,12 +96,23 @@ module Rails # :nodoc:
48
96
  super(*args, **xargs, &block)
49
97
  end
50
98
 
99
+ # Accept changes to the method name through the +apply_changes+
100
+ def apply_changes(**xargs, &block)
101
+ @method_name = xargs.delete(:method_name) if xargs.key?(:method_name)
102
+ super
103
+ end
104
+
105
+ # By default, output fields that belongs to a schema is a query field
106
+ def schema_type
107
+ :query
108
+ end
109
+
51
110
  # Check if the arguments are also equivalent
52
111
  def =~(other)
53
112
  super && match_arguments?(other)
54
113
  end
55
114
 
56
- # Checks if a given unserialized value is valid for this field
115
+ # Checks if a given raw value is valid for this field
57
116
  def valid_output?(value, deep: true)
58
117
  return false unless super
59
118
  return null? if value.nil?
@@ -74,11 +133,47 @@ module Rails # :nodoc:
74
133
  def validate!(*)
75
134
  super if defined? super
76
135
 
77
- raise ArgumentError, <<~MSG.squish unless type_klass.output_type?
136
+ raise ArgumentError, (+<<~MSG).squish unless type_klass.output_type?
78
137
  The "#{type_klass.gql_name}" is not a valid output type.
79
138
  MSG
80
139
  end
81
140
 
141
+ def all_events
142
+ if !defined?(@events) || !(local = @events).present?
143
+ super
144
+ elsif (inherited = super).nil?
145
+ local
146
+ else
147
+ Helpers.merge_hash_array(inherited, local)
148
+ end
149
+ end
150
+
151
+ def events?
152
+ super || defined?(@events) && @events.present?
153
+ end
154
+
155
+ def all_listeners
156
+ if !defined?(@listeners) || !(local = @listeners).present?
157
+ super
158
+ elsif (inherited = super).nil?
159
+ local
160
+ else
161
+ inherited + local
162
+ end
163
+ end
164
+
165
+ def listeners?
166
+ super || defined?(@listeners) && @listeners.present?
167
+ end
168
+
169
+ def broadcastable?
170
+ defined?(@broadcastable) && @broadcastable
171
+ end
172
+
173
+ def entry_point?
174
+ owner.is_a?(Helpers::WithSchemaFields)
175
+ end
176
+
82
177
  protected
83
178
 
84
179
  # Check if the given +value+ is a valid array as output
@@ -91,7 +186,12 @@ module Rails # :nodoc:
91
186
  end
92
187
  end
93
188
 
94
- def proxied # :nodoc:
189
+ # Properly display the owner section when the field is owned by a Schema
190
+ def inspect_owner
191
+ entry_point? ? +"#{owner.name}[:#{schema_type}]" : super
192
+ end
193
+
194
+ def proxied
95
195
  super if defined? super
96
196
  extend Field::OutputField::Proxied
97
197
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Rails # :nodoc:
4
- module GraphQL # :nodoc:
3
+ module Rails
4
+ module GraphQL
5
5
  # = GraphQL Proxied Field
6
6
  #
7
7
  # Proxied fields are a soft way to copy a real field. The good part is that
@@ -25,10 +25,9 @@ module Rails # :nodoc:
25
25
  # * <tt>:alias</tt> - Same as the +:as+ key (defaults to nil).
26
26
  module Field::ProxiedField
27
27
  delegate_missing_to :field
28
- delegate :leaf_type?, :array?, :internal?, :valid_input?, :valid_output?,
29
- :to_json, :as_json, :deserialize, :valid?, to: :field
28
+ delegate :leaf_type?, :array?, :internal?, :proxied_owner, to: :field
30
29
 
31
- Field.proxyable_methods %w[name gql_name method_name resolver description
30
+ Field.proxyable_methods %w[name gql_name method_name description
32
31
  null? nullable? enabled?], klass: self
33
32
 
34
33
  def initialize(field, owner:, **xargs, &block)
@@ -43,19 +42,27 @@ module Rails # :nodoc:
43
42
  true
44
43
  end
45
44
 
46
- # Allow chaging most of the general kind-independent initialize settings
45
+ # Make sure to check the original field as well
46
+ # TODO: Maybe not use the +==+ method, but so far so good
47
+ def ==(other)
48
+ super || field == other
49
+ end
50
+
51
+ # Allow changing most of the general kind-independent initialize settings
47
52
  def apply_changes(**xargs, &block)
48
53
  if (deprecated = xargs[:deprecated])
49
- xargs[:directives] = Array.wrap(xargs[:directives])
54
+ xargs[:directives] = ::Array.wrap(xargs[:directives])
50
55
  xargs[:directives] << Directive::DeprecatedDirective.new(
51
56
  reason: (deprecated.is_a?(String) ? deprecated : nil),
52
57
  )
53
58
  end
54
59
 
60
+ # TODO: Replace by a proper method to build and set @directives
55
61
  @directives = GraphQL.directives_to_set(xargs[:directives], source: self) \
56
62
  if xargs.key?(:directives)
57
63
 
58
- @desc = xargs[:desc]&.strip_heredoc&.chomp if xargs.key?(:desc)
64
+ self.description = xargs[:desc] if xargs.key?(:desc)
65
+ self.description = xargs[:description] if xargs.key?(:description)
59
66
  @enabled = xargs.fetch(:enabled, !xargs.fetch(:disabled, false)) \
60
67
  if xargs.key?(:enabled) || xargs.key?(:disabled)
61
68
 
@@ -63,11 +70,6 @@ module Rails # :nodoc:
63
70
  super
64
71
  end
65
72
 
66
- # Return the original owner from +field+
67
- def proxied_owner
68
- field.owner
69
- end
70
-
71
73
  # Override this to include proxied owners
72
74
  def all_owners
73
75
  super + proxied_owner.all_owners
@@ -78,34 +80,41 @@ module Rails # :nodoc:
78
80
  @field
79
81
  end
80
82
 
81
- def disable! # :nodoc:
83
+ # Just ensure that when the field is proxied to an interface it does not
84
+ # allow disabling
85
+ def disable!
82
86
  super unless non_interface_proxy!('disable')
83
87
  end
84
88
 
85
- def enable! # :nodoc:
89
+ # Just ensure that when the field is proxied to an interface it does not
90
+ # allow enabling
91
+ def enable!
86
92
  super unless non_interface_proxy!('enable')
87
93
  end
88
94
 
89
- def all_directives # :nodoc:
90
- field.all_directives + super
95
+ # Prepend the proxy directives and then the source directives
96
+ def all_directives
97
+ inherited = field.all_directives
98
+ return inherited unless defined?(@directives)
99
+ inherited.present? ? inherited + super : super
100
+ end
101
+
102
+ # Check if the field has directives locally or in the proxied field
103
+ def directives?
104
+ super || field.directives?
91
105
  end
92
106
 
93
- # It is important to ensure that the proxied field is also valid. If the
94
- # proxied owner is registered, then it is safe to assume that it is valid
107
+ # It is important to ensure that the proxied field is also valid
95
108
  def validate!(*)
96
109
  super if defined? super
97
- field.validate! unless GraphQL.type_map.object_exist?(
98
- proxied_owner,
99
- namespaces: namespaces,
100
- exclusive: true,
101
- )
110
+ field.validate!
102
111
  end
103
112
 
104
113
  protected
105
114
 
106
115
  alias field proxied_field
107
116
 
108
- def normalize_name(value) # :nodoc:
117
+ def normalize_name(value)
109
118
  super unless value.blank? || non_interface_proxy!('rename')
110
119
  end
111
120
 
@@ -124,7 +133,7 @@ module Rails # :nodoc:
124
133
 
125
134
  # Display the source of the proxy for inspection
126
135
  def inspect_source
127
- "@source=#{proxied_owner.name}[:#{field.name}] [proxied]"
136
+ +"@source=#{field.owner.name}[:#{field.name}] [proxied]"
128
137
  end
129
138
 
130
139
  # This is trigerred when the field is proxied
@@ -1,23 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Rails # :nodoc:
4
- module GraphQL # :nodoc:
3
+ module Rails
4
+ module GraphQL
5
5
  # This provides ways for fields to be resolved manually, by adding callbacks
6
6
  # and additional configurations in order to resolve a field value during a
7
7
  # request
8
8
  module Field::ResolvedField
9
9
  module Proxied # :nodoc: all
10
- def all_listeners
11
- field.all_listeners + super
12
- end
13
-
14
- def all_events
15
- events = defined?(@events) ? @events : {}
16
- Helpers.merge_hash_array(field.all_events, events).transform_values do |arr|
17
- arr.sort_by { |cb| cb.try(:target).is_a?(GraphQL::Field) ? 0 : 1 }
18
- end
19
- end
20
-
21
10
  def resolver
22
11
  super || field.resolver
23
12
  end
@@ -29,7 +18,7 @@ module Rails # :nodoc:
29
18
 
30
19
  # Just add the callbacks setup to the field
31
20
  def self.included(other)
32
- other.event_types(:prepare, :finalize, append: true, expose: true)
21
+ other.send(:expose_events!, :organized, :finalize, :prepared, :prepare)
33
22
  other.alias_method(:before_resolve, :prepare)
34
23
  other.alias_method(:after_resolve, :finalize)
35
24
  end
@@ -65,26 +54,39 @@ module Rails # :nodoc:
65
54
 
66
55
  # Store this result for performance purposes
67
56
  @dynamic_resolver = dynamic_resolver?
68
- return unless defined? @events
57
+ return unless defined?(@events)
69
58
 
70
- invalid = @events.each_value.reject do |callback|
71
- callback.block.is_a?(Proc) || callable?(callback.block)
72
- end
59
+ # TODO: Change how events are validated. Relying on the +source_location
60
+ # uses inspect, which is not a good approach
61
+
62
+ # invalid = @events.each_pair.each_with_object({}) do |(key, events), hash|
63
+ # events.each do |event|
64
+ # _, method_name = event.source_location
65
+ # next if method_name.nil? || callable?(method_name)
66
+
67
+ # (hash[key] ||= []).push(method_name)
68
+ # end
69
+ # end
70
+
71
+ # return if invalid.empty?
73
72
 
74
- raise ArgumentError, <<~MSG.squish if invalid.present?
75
- The "#{owner.name}" class does not define the following methods needed
76
- for performing hooks: #{invalid.map(&:block).to_sentence}.
77
- MSG
73
+ # invalid = invalid.map { |key, list| (+"#{key} => [#{list.join(', ')}]") }
74
+ # raise ArgumentError, (+<<~MSG).squish if invalid.present?
75
+ # The "#{owner.name}" class does not define the following methods needed
76
+ # for performing callbacks: #{invalid.join(', ')}.
77
+ # MSG
78
78
  end
79
79
 
80
80
  protected
81
81
 
82
- # Chedck if a given +method_name+ is callable from the owner perspective
82
+ # Check if the method is defined and does not belong to a method defined
83
+ # by the gem itself
83
84
  def callable?(method_name)
84
- owner.is_a?(Class) && owner.try(:gql_resolver?, method_name)
85
+ owner.is_a?(Class) && owner.public_method_defined?(method_name) &&
86
+ !owner.public_instance_method(method_name).owner.try(:abstract?)
85
87
  end
86
88
 
87
- def proxied # :nodoc:
89
+ def proxied
88
90
  super if defined? super
89
91
  extend Field::ResolvedField::Proxied
90
92
  end
@@ -1,22 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Rails # :nodoc:
4
- module GraphQL # :nodoc:
3
+ module Rails
4
+ module GraphQL
5
5
  # Helper class to be used while configuring a field using a block. An
6
6
  # instance of this class works as proxy for changes to the actual field.
7
- Field::ScopedConfig = Struct.new(:field, :receiver) do
7
+ class Field::ScopedConfig < Struct.new(:field, :receiver)
8
8
  delegate :argument, :ref_argument, :id_argument, :use, :internal?, :disabled?,
9
9
  :enabled?, :disable!, :enable!, :authorize, to: :field
10
10
 
11
11
  delegate_missing_to :receiver
12
12
 
13
+ def rename!(name)
14
+ field.instance_variable_set(:@gql_name, name.to_s)
15
+ end
16
+
13
17
  def method_name(value)
14
18
  field.instance_variable_set(:@method_name, value.to_sym)
15
19
  end
16
20
 
17
21
  def desc(value)
18
- field.instance_variable_set(:@desc, value.strip_heredoc.chomp)
22
+ field.description = value
19
23
  end
24
+
25
+ alias description desc
20
26
  end
21
27
  end
22
28
  end
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module GraphQL
5
+ # = GraphQL Subscription Field
6
+ #
7
+ # This is an extension of a normal output field, which will sign
8
+ # the request for updates of the field when the scope and arguments
9
+ # are the same.
10
+ class Field::SubscriptionField < Field::OutputField
11
+ redefine_singleton_method(:subscription?) { true }
12
+ event_types(:subscribed, append: true)
13
+
14
+ module Proxied # :nodoc: all
15
+ def full_scope
16
+ field.full_scope + super
17
+ end
18
+ end
19
+
20
+ # Just add the callbacks setup to the field
21
+ def self.included(other)
22
+ other.send(:expose_events!, :subscribed)
23
+ end
24
+
25
+ # Intercept the initializer to maybe set the +scope+
26
+ def initialize(*args, scope: nil, **xargs, &block)
27
+ @scope = Array.wrap(scope).freeze unless scope.nil?
28
+ super(*args, **xargs, &block)
29
+ end
30
+
31
+ # Change the schema type of the field
32
+ def schema_type
33
+ :subscription
34
+ end
35
+
36
+ # Set the parts of the scope of the subscription
37
+ def scope(*parts)
38
+ (defined?(@scope) ? (@scope += parts) : (@scope = parts)).freeze
39
+ self
40
+ end
41
+
42
+ # Get the full scope of the field
43
+ def full_scope
44
+ defined?(@scope) ? @scope : EMPTY_ARRAY
45
+ end
46
+
47
+ # A shortcut for trigger when everything is related to the provided object
48
+ def trigger_for(object, and_prepare: true, **xargs)
49
+ xargs[:args] ||= extract_args_from(object)
50
+
51
+ if and_prepare
52
+ xargs[:data_for] ||= {}
53
+ xargs[:data_for][+"subscription.#{gql_name}"] = object
54
+ end
55
+
56
+ trigger(**xargs)
57
+ end
58
+
59
+ # A shortcut for unsubscribe when everything is related to the provided
60
+ # object
61
+ def unsubscribe_from(object, **xargs)
62
+ xargs[:args] ||= extract_args_from(object)
63
+ unsubscribe(**xargs)
64
+ end
65
+
66
+ # Trigger an update to the subscription
67
+ def trigger(**xargs)
68
+ provider = owner.subscription_provider
69
+ provider.search_and_update(field: self, **xargs)
70
+ end
71
+
72
+ # Force matching subscriptions to be removed
73
+ def unsubscribe(**xargs)
74
+ provider = owner.subscription_provider
75
+ provider.search_and_remove(field: self, **xargs)
76
+ end
77
+
78
+ protected
79
+
80
+ # Match any argument with properties from the given +object+ so it
81
+ # produces all the possibilities of an update
82
+ def extract_args_from(object, iterate = true)
83
+ return unless arguments?
84
+
85
+ # If can iterate and the provided object is an enumerable, then
86
+ # call itself with each item
87
+ if iterate && object.is_a?(Enumerable)
88
+ return object.each_with_object([]) do |item, result|
89
+ result.concat(extract_args_from(item, false))
90
+ end
91
+ end
92
+
93
+ # Prepare all the possibilities
94
+ keys = []
95
+ hash_like = object.respond_to?(:[])
96
+ possibilities = all_arguments.each_value.with_object([]) do |arg, list|
97
+ keys << arg.name
98
+ list << []
99
+
100
+ value =
101
+ if object.respond_to?(arg.name)
102
+ object.public_send(arg.name)
103
+ elsif hash_like && (object.key?(arg.name) || object.key?(arg.name.to_s))
104
+ object[arg.name] || arg.name[arg.name.to_s]
105
+ end
106
+
107
+ list.last << value unless value.nil?
108
+ list.last << nil if arg.null?
109
+ end
110
+
111
+ # Now turn them into actual possible args
112
+ possibilities.reduce(:product).flatten.each_slice(keys.size).map do |items|
113
+ keys.zip(items).to_h
114
+ end
115
+ end
116
+
117
+ def proxied
118
+ super if defined? super
119
+ extend Field::SubscriptionField::Proxied
120
+ end
121
+ end
122
+ end
123
+ end