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,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 Argument
6
6
  #
7
7
  # This represents an item from the ArgumentsDefinition, which was supposed
@@ -31,6 +31,7 @@ module Rails # :nodoc:
31
31
  # (defaults to nil).
32
32
  class Argument
33
33
  include Helpers::WithValidator
34
+ include Helpers::WithDescription
34
35
 
35
36
  # TODO: When arguments are attached to output fields they can have
36
37
  # directives so add this possibility
@@ -42,19 +43,21 @@ module Rails # :nodoc:
42
43
 
43
44
  def initialize(
44
45
  name,
45
- type,
46
+ type = nil,
46
47
  owner:,
47
48
  null: true,
48
49
  full: false,
49
50
  array: false,
50
51
  nullable: true,
51
52
  default: nil,
52
- desc: nil
53
+ desc: nil,
54
+ description: nil
53
55
  )
54
56
  @owner = owner
55
57
  @name = name.to_s.underscore.to_sym
56
58
  @gql_name = @name.to_s.camelize(:lower)
57
59
 
60
+ type = (name == :id ? :id : :string) if type.nil?
58
61
  if type.is_a?(Module) && type < GraphQL::Type
59
62
  @type_klass = type
60
63
  @type = type.name
@@ -67,7 +70,8 @@ module Rails # :nodoc:
67
70
  @nullable = full ? false : nullable
68
71
 
69
72
  @default = default
70
- @desc = desc&.strip_heredoc&.chomp
73
+ @default = deserialize(@default) if @default.is_a?(::GQLParser::Token)
74
+ self.description = desc || description
71
75
  end
72
76
 
73
77
  def initialize_copy(*)
@@ -114,14 +118,9 @@ module Rails # :nodoc:
114
118
  !!@nullable
115
119
  end
116
120
 
117
- # Return the description of the argument
118
- def description
119
- @desc
120
- end
121
-
122
- # Checks if a description was provided
123
- def description?
124
- !!@desc
121
+ # Override to add the kind
122
+ def description(namespace = nil, *)
123
+ super(namespace || owner.try(:namespaces), :argument)
125
124
  end
126
125
 
127
126
  # Checks if a given default value was provided
@@ -149,7 +148,7 @@ module Rails # :nodoc:
149
148
 
150
149
  # Turn the given value into a ruby object through deserialization
151
150
  def deserialize(value = nil)
152
- value = @default if value.nil?
151
+ value = as_json if value.nil?
153
152
 
154
153
  return if value.nil?
155
154
  return type_klass.deserialize(value) unless array?
@@ -174,19 +173,19 @@ module Rails # :nodoc:
174
173
  def validate!(*)
175
174
  super if defined? super
176
175
 
177
- raise NameError, <<~MSG.squish if gql_name.start_with?('__')
176
+ raise NameError, (+<<~MSG).squish if gql_name.start_with?('__')
178
177
  The name "#{gql_name}" is invalid. Argument names cannot start with "__".
179
178
  MSG
180
179
 
181
- raise ArgumentError, <<~MSG.squish unless type_klass.is_a?(Module)
180
+ raise ArgumentError, (+<<~MSG).squish unless type_klass.is_a?(Module)
182
181
  Unable to find the "#{type.inspect}" input type on GraphQL context.
183
182
  MSG
184
183
 
185
- raise ArgumentError, <<~MSG.squish unless type_klass.input_type?
184
+ raise ArgumentError, (+<<~MSG).squish unless type_klass.input_type?
186
185
  The "#{type_klass.gql_name}" is not a valid input type.
187
186
  MSG
188
187
 
189
- raise ArgumentError, <<~MSG.squish unless default.nil? || valid?(default)
188
+ raise ArgumentError, (+<<~MSG).squish unless default.nil? || valid?(as_json(default))
190
189
  The given default value "#{default.inspect}" is not valid for this argument.
191
190
  MSG
192
191
  end
@@ -198,14 +197,14 @@ module Rails # :nodoc:
198
197
 
199
198
  alias_method :&, :+
200
199
 
201
- def inspect # :nodoc:
202
- result = "#{name}: "
203
- result += '[' if array?
204
- result += type_klass.gql_name
205
- result += '!' if array? && !nullable?
206
- result += ']' if array?
207
- result += '!' unless null?
208
- result += " = #{to_hash.inspect}" if default_value?
200
+ def inspect
201
+ result = +"#{name}: "
202
+ result << '[' if array?
203
+ result << type_klass.gql_name
204
+ result << '!' if array? && !nullable?
205
+ result << ']' if array?
206
+ result << '!' unless null?
207
+ result << " = #{as_json.inspect}" if default_value?
209
208
  result
210
209
  end
211
210
 
@@ -1,12 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Rails # :nodoc:
4
- module GraphQL # :nodoc:
3
+ module Rails
4
+ module GraphQL
5
5
  # = Rails GraphQL Callback
6
6
  #
7
- # An extra powerfull proc that can handle way more situations than the
7
+ # An extra powerful proc that can handle way more situations than the
8
8
  # original block caller
9
9
  class Callback
10
+ EXCLUSIVE_ARGUMENT = :exclusive_callback
11
+
10
12
  attr_reader :event_name, :block, :target, :filters
11
13
 
12
14
  delegate :event_filters, to: :target
@@ -19,7 +21,7 @@ module Rails # :nodoc:
19
21
  end
20
22
 
21
23
  def initialize(target, event_name, *args, **xargs, &block)
22
- raise ::ArgumentError, <<~MSG.squish if block.nil? && !args.first.present?
24
+ raise ::ArgumentError, (+<<~MSG).squish if block.nil? && !args.first.present?
23
25
  Either provide a block or a method name when setting a #{event_name}
24
26
  callback on #{target.inspect}.
25
27
  MSG
@@ -27,7 +29,7 @@ module Rails # :nodoc:
27
29
  if block.nil?
28
30
  block = args.shift
29
31
  valid_format = block.is_a?(Symbol) || block.is_a?(Proc)
30
- raise ::ArgumentError, <<~MSG.squish unless valid_format
32
+ raise ::ArgumentError, (+<<~MSG).squish unless valid_format
31
33
  The given #{block.class.name} class is not a valid callback.
32
34
  MSG
33
35
  end
@@ -35,11 +37,13 @@ module Rails # :nodoc:
35
37
  @target = target
36
38
  @event_name = event_name
37
39
 
40
+ @exclusive = xargs.delete(EXCLUSIVE_ARGUMENT)
41
+ @exclusive = target.try(:default_exclusive?, event_name) if @exclusive.nil?
42
+ @exclusive = true if @exclusive.nil?
43
+
38
44
  @pre_args = args
39
45
  @pre_xargs = xargs.slice!(*event_filters.keys)
40
- @filters = xargs.map do |key, value|
41
- [key, event_filters[key][:sanitizer]&.call(value) || value]
42
- end.to_h
46
+ @filters = xargs
43
47
 
44
48
  @block = block
45
49
  end
@@ -47,19 +51,24 @@ module Rails # :nodoc:
47
51
  # This does the whole checking and preparation in order to really execute
48
52
  # the callback method
49
53
  def call(event, *args, _callback_context: nil, **xargs)
50
- return unless event.name === event_name && can_run?(event)
54
+ return unless event.event_name === event_name && can_run?(event)
51
55
 
52
56
  block.is_a?(Symbol) \
53
57
  ? call_symbol(event, *args, **xargs) \
54
58
  : call_proc(event, _callback_context, *args, **xargs)
55
59
  end
56
60
 
61
+ # Return if this event is exclusive, so that only the original source of
62
+ # the callback will be allowed to receive it
63
+ def exclusive?
64
+ @exclusive
65
+ end
66
+
57
67
  # Get a described source location for the callback
58
68
  def source_location
59
- block.is_a?(Proc) ? block.source_location : [
60
- "(symbolized-callback/#{target.inspect})",
61
- block,
62
- ]
69
+ block.is_a?(Proc) ? block.source_location : begin
70
+ [+"(symbolized-callback/#{target.inspect})", block]
71
+ end
63
72
  end
64
73
 
65
74
  # This basically allows the class to be passed as +&block+
@@ -80,10 +89,16 @@ module Rails # :nodoc:
80
89
 
81
90
  # Using the filters, check if the current callback can be executed
82
91
  def can_run?(event)
83
- filters.all? { |key, options| event_filters[key][:block].call(options, event) }
92
+ return false if exclusive? && !event.same_source?(target)
93
+
94
+ filters.all? do |key, options|
95
+ target.instance_exec(options, event, &event_filters[key])
96
+ end
84
97
  end
85
98
 
86
99
  # Call the callback block as a symbol
100
+ # TODO: Maybe block calling non-public events
101
+ # TODO: This is the only reason why directives needs an owner
87
102
  def call_symbol(event, *args, **xargs)
88
103
  event.on_instance(owner) do |instance|
89
104
  block = instance.method(@block)
@@ -95,6 +110,7 @@ module Rails # :nodoc:
95
110
  # Call the callback block as a proc
96
111
  def call_proc(event, context = nil, *args, **xargs)
97
112
  args, xargs = collect_parameters(event, [args, xargs])
113
+ args << event unless inject_arguments? || (block.arity - 1) == args.size
98
114
  (context || event).instance_exec(*args, **xargs, &block)
99
115
  end
100
116
 
@@ -1,6 +1,10 @@
1
- module Rails # :nodoc:
2
- module GraphQL # :nodoc:
3
- module Collectors # :nodoc:
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module GraphQL
5
+ module Collectors
6
+ # = GraphQL Hash Collector
7
+ #
4
8
  # This collector helps building a JSON response using the hash approach,
5
9
  # where the value is kept as an hash and later turn into a string
6
10
  class HashCollector
@@ -24,7 +28,12 @@ module Rails # :nodoc:
24
28
 
25
29
  # Add the given +value+ to the given +key+.
26
30
  def add(key, value)
27
- @data.is_a?(Array) ? @data << value : @data[key.to_s] = value
31
+ @data.is_a?(::Array) ? @data << value : @data[key.to_s] = value
32
+ end
33
+
34
+ # Check if a given +key+ has already been added to the current data
35
+ def key?(key)
36
+ !@data.is_a?(::Array) && @data.key?(key)
28
37
  end
29
38
 
30
39
  alias safe_add add
@@ -36,16 +45,22 @@ module Rails # :nodoc:
36
45
 
37
46
  # Mark the start of a new element on the array.
38
47
  def next
39
- return unless @stack.last.is_a?(Array)
48
+ return unless @stack.last.is_a?(::Array)
40
49
  @stack.last << @data
41
50
  @data = {}
42
51
  end
43
52
 
44
- # Append to the responsa data all the errors that happened during the
53
+ # Append to the response all the errors that happened during the
45
54
  # request process
46
55
  def append_errors(errors)
47
56
  return if errors.empty?
48
- @data['errors'] = errors.to_a
57
+ @data['errors'] = errors.as_json
58
+ end
59
+
60
+ # Append to the response anything added to the extensions
61
+ def append_extensions(extensions)
62
+ return if extensions.empty?
63
+ @data['extensions'] = extensions.as_json
49
64
  end
50
65
 
51
66
  # Return the generated object
@@ -53,6 +68,8 @@ module Rails # :nodoc:
53
68
  @data
54
69
  end
55
70
 
71
+ alias as_json to_h
72
+
56
73
  # Generate the JSON string result
57
74
  def to_s
58
75
  GraphQL.config.encode_with_active_support? \
@@ -60,6 +77,8 @@ module Rails # :nodoc:
60
77
  : ::JSON.generate(@data)
61
78
  end
62
79
 
80
+ alias to_json to_s
81
+
63
82
  private
64
83
 
65
84
  # Start a new part of the collector. When set +as_array+, the result
@@ -1,11 +1,14 @@
1
- module Rails # :nodoc:
2
- module GraphQL # :nodoc:
3
- module Collectors # :nodoc:
4
- # This collector helps building a indented string
1
+ module Rails
2
+ module GraphQL
3
+ module Collectors
4
+ # = GraphQL Idented Collector
5
+ #
6
+ # This collector helps building a indented string, most used for
7
+ # displaying a GraphQL schema
5
8
  class IdentedCollector
6
9
  def initialize(initial = 0, size = 2, auto_eol: true)
7
10
  @size = size
8
- @val = [[initial, '']]
11
+ @val = [[initial, +'']]
9
12
  @auto_eol = auto_eol
10
13
  end
11
14
 
@@ -50,13 +53,13 @@ module Rails # :nodoc:
50
53
 
51
54
  def indent
52
55
  return @val.last[0] += @size if blank?
53
- @val << [last_ident + @size, '']
56
+ @val << [last_ident + @size, +'']
54
57
  self
55
58
  end
56
59
 
57
60
  def unindent
58
61
  return @val.last[0] -= @size if blank?
59
- @val << [last_ident - @size, '']
62
+ @val << [last_ident - @size, +'']
60
63
  self
61
64
  end
62
65
 
@@ -1,16 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Rails # :nodoc:
4
- module GraphQL # :nodoc:
5
- module Collectors # :nodoc:
3
+ module Rails
4
+ module GraphQL
5
+ module Collectors
6
+ # = GraphQL JSON Collector
7
+ #
6
8
  # This collector helps building a JSON response using the string approach,
7
9
  # which has better performance, since all the encoding is performed up
8
- # front. The drawback is that it can't return an hash.
10
+ # front. The drawback is that it can't return a hash.
9
11
  class JsonCollector
10
12
  def initialize(request)
11
13
  @request = request
12
14
 
13
- @current_value = String.new
15
+ @current_keys = Set.new
16
+ @stack_keys = []
17
+
18
+ @current_value = StringIO.new
14
19
  @stack_value = []
15
20
 
16
21
  @current_array = false
@@ -30,31 +35,41 @@ module Rails # :nodoc:
30
35
  pop_size = array && !plain ? 2 : 1
31
36
  @current_value = @stack_value.pop(pop_size).first
32
37
  @current_array = @stack_array.pop(pop_size).first
38
+ @current_keys = @stack_keys.pop
33
39
  raise
34
40
  end
35
41
 
36
- # Append to the responsa data all the errors that happened during the
42
+ # Append to the response data all the errors that happened during the
37
43
  # request process.
38
44
  def append_errors(errors)
39
45
  return if errors.empty?
40
46
  add('errors', errors.to_json)
41
47
  end
42
48
 
49
+ # Append to the response anything added to the extensions
50
+ def append_extensions(extensions)
51
+ return if extensions.empty?
52
+ add('extensions', extensions.to_json)
53
+ end
54
+
43
55
  # Add the given +value+ to the given +key+. Ensure to encode the value
44
56
  # before calling this function.
45
57
  def add(key, value)
46
- (@current_value << ',') unless @current_value.blank?
58
+ (@current_value << ',') if @current_value.pos > 0
59
+ @current_keys << key
47
60
 
48
61
  if @current_array
49
62
  @current_value << value
50
63
  else
51
- @current_value << '"'
52
- @current_value << key.to_s
53
- @current_value << '":'
54
- @current_value << value.to_s
64
+ @current_value << '"' << +key.to_s << '"' << ':' << +value.to_s
55
65
  end
56
66
  end
57
67
 
68
+ # Check if the given +key+ has been added already
69
+ def key?(key)
70
+ @current_keys.include?(key)
71
+ end
72
+
58
73
  # Same as +add+ but this always encode the +value+ beforehand.
59
74
  def safe_add(key, value)
60
75
  add(key, value.nil? ? 'null' : value.to_json)
@@ -68,32 +83,42 @@ module Rails # :nodoc:
68
83
  # Mark the start of a new element on the array.
69
84
  def next
70
85
  return unless @stack_array.last === :complex
71
- (@stack_value.last << ',') unless @stack_value.last.blank?
86
+ (@stack_value.last << ',') if @stack_value.last.pos > 0
72
87
  @stack_value.last << to_s
73
- @current_value = String.new
88
+
89
+ @current_value = StringIO.new
90
+ @current_keys = Set.new
74
91
  end
75
92
 
76
93
  # Get the current result
77
94
  def to_s
78
- @current_array ? "[#{@current_value}]" : "{#{@current_value}}"
95
+ if @current_array
96
+ +'[' << @current_value.string << ']'
97
+ else
98
+ +'{' << @current_value.string << '}'
99
+ end
79
100
  end
80
101
 
102
+ alias to_json to_s
103
+
81
104
  private
82
105
 
83
106
  # Start a new part of the collector. When set +as_array+, the result
84
- # of the stack will be encolsed by +[]+.
107
+ # of the stack will be enclosed by +[]+.
85
108
  def start_stack(as_array = false, plain_array = false)
86
109
  @stack_value << @current_value
87
110
  @stack_array << @current_array
111
+ @stack_keys << @current_keys
88
112
 
89
113
  if as_array && !plain_array
90
- @stack_value << String.new
114
+ @stack_value << StringIO.new
91
115
  @stack_array << :complex
92
116
  as_array = false
93
117
  end
94
118
 
95
- @current_value = String.new
119
+ @current_value = StringIO.new
96
120
  @current_array = as_array
121
+ @current_keys = Set.new
97
122
  end
98
123
 
99
124
  # Finalize a stack and set the result on the given +key+.
@@ -106,6 +131,7 @@ module Rails # :nodoc:
106
131
  result = to_s
107
132
  @current_value = @stack_value.pop
108
133
  @current_array = @stack_array.pop
134
+ @current_keys = @stack_keys.pop
109
135
  add(key, result)
110
136
  end
111
137
  end
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Rails # :nodoc:
4
- module GraphQL # :nodoc:
5
- module Collectors # :nodoc:
3
+ module Rails
4
+ module GraphQL
5
+ # All the possible collectors that uses the reverse visit approach
6
+ module Collectors
6
7
  extend ActiveSupport::Autoload
7
8
 
8
9
  autoload :HashCollector
9
10
  autoload :IdentedCollector
10
11
  autoload :JsonCollector
11
-
12
12
  end
13
13
  end
14
14
  end