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
@@ -4,8 +4,8 @@ require 'rails/railtie'
4
4
  require 'action_controller'
5
5
  require 'action_controller/railtie'
6
6
 
7
- module Rails # :nodoc:
8
- module GraphQL # :nodoc:
7
+ module Rails
8
+ module GraphQL
9
9
  # = Rails GraphQL Railtie
10
10
  #
11
11
  # Rails integration and configuration
@@ -21,19 +21,43 @@ module Rails # :nodoc:
21
21
  require_relative './schema'
22
22
  end
23
23
 
24
+ console do
25
+ config.graphql.enable_string_collector = false
26
+ config.graphql.default_response_format = :hash
27
+ end
28
+
24
29
  # Ensure a valid logger
25
- initializer 'graphql.logger' do
30
+ initializer 'graphql.logger' do |app|
26
31
  ActiveSupport.on_load(:graphql) do
27
32
  return if config.logger.present?
28
- if ::Rails.logger.respond_to?(:tagged)
29
- config.logger = ::Rails.logger
33
+
34
+ logger = ::Rails.logger
35
+ if logger.respond_to?(:tagged)
36
+ config.logger = logger
30
37
  else
31
- config.logger = ActiveSupport::TaggedLogging.new(::Rails.logger)
38
+ config.logger = ActiveSupport::TaggedLogging.new(logger)
39
+ end
40
+ end
41
+ end
42
+
43
+ initializer 'graphql.active_record_backtrace_print' do
44
+ if defined?(ActiveRecord)
45
+ base = Module.new
46
+ base.send(:define_method, :to_gql_backtrace) do
47
+ +"#<#{self.class.name} id: #{id}>"
32
48
  end
49
+
50
+ relation = Module.new
51
+ relation.send(:define_method, :to_gql_backtrace) do
52
+ +"[#<#{model.name}>](#{size})"
53
+ end
54
+
55
+ ActiveRecord::Base.include(base)
56
+ ActiveRecord::Relation.include(relation)
33
57
  end
34
58
  end
35
59
 
36
- # Expose database runtime to controller for logging.
60
+ # Expose GraphQL runtime to controller for logging
37
61
  initializer 'graphql.log_runtime' do
38
62
  require_relative './railties/controller_runtime'
39
63
  ActiveSupport.on_load(:action_controller) do
@@ -41,45 +65,77 @@ module Rails # :nodoc:
41
65
  end
42
66
  end
43
67
 
68
+ # Clean up GraphQL params from logger
69
+ initializer 'graphql.params_cleanup' do
70
+ key = 'start_processing.action_controller'
71
+ ActiveSupport::Notifications.subscribe(key) do |*, payload|
72
+ payload[:params].except!(*config.graphql.omit_parameters) \
73
+ if payload[:headers]['action_controller.instance'].is_a?(GraphQL::Controller)
74
+ end
75
+ end
76
+
77
+ # Copy filter params when they are not exclusively set for GraphQL
78
+ initializer 'graphql.params_filter' do |app|
79
+ config.after_initialize do
80
+ config.graphql.filter_parameters ||= app.config.filter_parameters
81
+ end
82
+ end
83
+
44
84
  # Backtrace cleaner for removing gem paths
45
85
  initializer 'graphql.backtrace_cleaner' do
46
86
  require_relative './railties/log_subscriber'
47
87
  ActiveSupport.on_load(:graphql) do
48
- GraphQL::LogSubscriber.backtrace_cleaner = ::Rails.backtrace_cleaner
88
+ LogSubscriber.backtrace_cleaner = ::Rails.backtrace_cleaner
49
89
  end
50
90
  end
51
91
 
52
- # Add reloader ability for files under 'app/graphql'
53
- # TODO: Maybe improve to use rails auto loader
54
- initializer 'graphql.reloader' do
55
- Rails::GraphQL.eager_load!
56
- ActiveSupport::Reloader.to_prepare do
57
- Rails::GraphQL.type_map.use_checkpoint!
58
- Rails::GraphQL.reload_ar_adapters!
59
-
60
- Object.send(:remove_const, :GraphQL) if Object.const_defined?(:GraphQL)
92
+ # Add the GraphQL Global ID serializer to active job serializers
93
+ initializer 'graphql.global_id' do
94
+ ActiveSupport.on_load(:active_job) do
95
+ ActiveJob::Serializers.add_serializers(Rails::GraphQL::GlobalID::Serializer)
96
+ end
97
+ end
61
98
 
62
- load "#{__dir__}/shortcuts.rb"
99
+ # Simply switch to hash output if rails is running on test mode
100
+ initializer 'graphql.tests' do
101
+ if Rails.env.test?
102
+ config.graphql.enable_string_collector = false
103
+ config.graphql.default_response_format = :hash
104
+ end
105
+ end
63
106
 
64
- $LOAD_PATH.each do |path|
65
- next unless path =~ %r{\/app\/graphql$}
66
- Dir.glob("#{path}/**/*.rb").sort.each(&method(:load))
107
+ # Set GraphQL cache store same as rails default cache store, unless the
108
+ # default value is a Null Cache, then use the fallback instead
109
+ initializer 'graphql.cache', after: :initialize_cache do
110
+ config.graphql.cache ||= begin
111
+ if !::Rails.cache.is_a?(::ActiveSupport::Cache::NullStore)
112
+ ::Rails.cache
113
+ elsif config.graphql.cache_fallback.is_a?(Proc)
114
+ config.graphql.cache_fallback.call
115
+ else
116
+ config.graphql.cache_fallback
67
117
  end
68
-
69
- GraphQL::Source.send(:build_pending!)
70
118
  end
71
119
  end
72
120
 
73
- # initializer "active_record.set_reloader_hooks" do
74
- # ActiveSupport.on_load(:active_record) do
75
- # ActiveSupport::Reloader.before_class_unload do
76
- # if ActiveRecord::Base.connected?
77
- # ActiveRecord::Base.clear_cache!
78
- # ActiveRecord::Base.clear_reloadable_connections!
79
- # end
80
- # end
81
- # end
82
- # end
121
+ # Properly setup how GraphQL reload itself
122
+ # TODO: Check proper support for Rails engines
123
+ initializer 'graphql.reloader', before: :load_config_initializers do |app|
124
+ next unless (path = app.root.join('app', 'graphql')).exist?
125
+
126
+ children = config.graphql.paths.to_a.join(',')
127
+ autoloader = app.respond_to?(:autoloaders) ? app.autoloaders : Rails.autoloaders
128
+ autoloader = autoloader.main
129
+
130
+ ActiveSupport::Dependencies.autoload_paths.delete(path.to_s)
131
+ autoloader.collapse(path.glob("**/{#{children}}").select(&:directory?))
132
+ autoloader.push_dir(path, namespace: ::GraphQL)
133
+ config.watchable_dirs[path.to_s] = [:rb]
134
+
135
+ autoloader.on_unload do |_, value, _|
136
+ value.unregister! if value.is_a?(Helpers::Unregisterable)
137
+ end
138
+ end
83
139
  end
84
140
  end
85
141
  end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ base = ActionCable::Channel::Base
5
+ base = ApplicationCable::Channel if defined?(ApplicationCable::Channel)
6
+
7
+ BaseChannel = Class.new(base) do
8
+ include ::Rails::GraphQL::Channel
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ base = ActionController::Base
5
+ base = ApplicationController if defined?(ApplicationController)
6
+
7
+ BaseController = Class.new(base) do
8
+ include ::Rails::GraphQL::Controller
9
+
10
+ skip_before_action :verify_authenticity_token
11
+ end
12
+ end
@@ -0,0 +1,56 @@
1
+ var queue = [];
2
+ var current = null;
3
+ var identifier = JSON.stringify({ channel: '<%= channel %>' });
4
+ var socket = new WebSocket("ws://" + window.location.hostname + "/<%= url %>");
5
+
6
+ // TOOD: This is a temporary implementation
7
+ socket.onopen = function(event) {
8
+ const msg = { command: 'subscribe', identifier: identifier };
9
+ socket.send(JSON.stringify(msg));
10
+ };
11
+
12
+ socket.onmessage = function(event) {
13
+ const msg = JSON.parse(event.data);
14
+ if (msg.type === "ping") {
15
+ return;
16
+ }
17
+
18
+ if (msg.type === "confirm_subscription") {
19
+ execute_next();
20
+ return;
21
+ }
22
+
23
+ if (msg.message && current) {
24
+ current.resolve(msg.message.result);
25
+ current = null;
26
+ execute_next();
27
+ } else {
28
+ console.dir(msg);
29
+ }
30
+ };
31
+
32
+ function execute_next() {
33
+ if (socket.readyState != '1' || queue.length === 0 || current) {
34
+ return;
35
+ }
36
+
37
+ current = queue.shift();
38
+ socket.send(JSON.stringify({
39
+ command: 'message',
40
+ identifier: identifier,
41
+ data: JSON.stringify({ action: 'execute', ...current.data }),
42
+ }));
43
+ }
44
+
45
+ function graphQLFetcher(graphQLParams) {
46
+ var resolve;
47
+ var promise = new Promise((success) => {
48
+ resolve = success;
49
+ });
50
+
51
+ var item = { data: graphQLParams, promise, resolve };
52
+
53
+ queue.push(item);
54
+ execute_next();
55
+ return promise;
56
+ };
@@ -0,0 +1,20 @@
1
+ function graphQLFetcher(graphQLParams) {
2
+ // This example expects a GraphQL server at the path /graphql.
3
+ // Change this to point wherever you host your GraphQL server.
4
+ return fetch('<%= url %>', {
5
+ method: 'post',
6
+ headers: {
7
+ 'Accept': 'application/json',
8
+ 'Content-Type': 'application/json'
9
+ },
10
+ body: JSON.stringify(graphQLParams),
11
+ }).then(function (response) {
12
+ return response.text();
13
+ }).then(function (responseBody) {
14
+ try {
15
+ return JSON.parse(responseBody);
16
+ } catch (error) {
17
+ return responseBody;
18
+ }
19
+ });
20
+ }
@@ -0,0 +1,101 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta name="robots" content="noindex" />
6
+ <meta name="referrer" content="origin" />
7
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
8
+ <title>SWAPI GraphQL API</title>
9
+ <style>
10
+ body {
11
+ height: 100vh;
12
+ margin: 0;
13
+ overflow: hidden;
14
+ }
15
+ #splash {
16
+ color: #333;
17
+ display: flex;
18
+ flex-direction: column;
19
+ font-family: system, -apple-system, "San Francisco", ".SFNSDisplay-Regular", "Segoe UI", Segoe, "Segoe WP", "Helvetica Neue", helvetica, "Lucida Grande", arial, sans-serif;
20
+ height: 100vh;
21
+ justify-content: center;
22
+ text-align: center;
23
+ }
24
+ </style>
25
+ <link rel="icon" href="favicon.ico">
26
+ <link type="text/css" href="//unpkg.com/graphiql/graphiql.min.css" rel="stylesheet" />
27
+ </head>
28
+ <body>
29
+ <div id="splash">
30
+ Loading&hellip;
31
+ </div>
32
+ <script src="//cdn.jsdelivr.net/es6-promise/4.0.5/es6-promise.auto.min.js"></script>
33
+ <script src="https://cdn.jsdelivr.net/npm/react/umd/react.production.min.js"></script>
34
+ <script src="https://cdn.jsdelivr.net/npm/react-dom/umd/react-dom.production.min.js"></script>
35
+ <script src="//unpkg.com/graphiql/graphiql.min.js"></script>
36
+ <script>
37
+ // Parse the search string to get url parameters.
38
+ var search = window.location.search;
39
+ var parameters = {};
40
+ search.substr(1).split('&').forEach(function (entry) {
41
+ var eq = entry.indexOf('=');
42
+ if (eq >= 0) {
43
+ parameters[decodeURIComponent(entry.slice(0, eq))] =
44
+ decodeURIComponent(entry.slice(eq + 1));
45
+ }
46
+ });
47
+
48
+ // if variables was provided, try to format it.
49
+ if (parameters.variables) {
50
+ try {
51
+ parameters.variables =
52
+ JSON.stringify(JSON.parse(parameters.variables), null, 2);
53
+ } catch (e) {
54
+ // Do nothing, we want to display the invalid JSON as a string, rather
55
+ // than present an error.
56
+ }
57
+ }
58
+
59
+ // When the query and variables string is edited, update the URL bar so
60
+ // that it can be easily shared
61
+ function onEditQuery(newQuery) {
62
+ parameters.query = newQuery;
63
+ updateURL();
64
+ }
65
+ function onEditVariables(newVariables) {
66
+ parameters.variables = newVariables;
67
+ updateURL();
68
+ }
69
+ function onEditOperationName(newOperationName) {
70
+ parameters.operationName = newOperationName;
71
+ updateURL();
72
+ }
73
+ function updateURL() {
74
+ var newSearch = '?' + Object.keys(parameters).filter(function (key) {
75
+ return Boolean(parameters[key]);
76
+ }).map(function (key) {
77
+ return encodeURIComponent(key) + '=' +
78
+ encodeURIComponent(parameters[key]);
79
+ }).join('&');
80
+ history.replaceState(null, null, newSearch);
81
+ }
82
+
83
+ <%= render partial: "/#{settings[:mode]}", formats: :js, locals: settings %>
84
+
85
+ // Render <GraphiQL /> into the body.
86
+ ReactDOM.render(
87
+ React.createElement(GraphiQL, {
88
+ fetcher: graphQLFetcher,
89
+ query: parameters.query,
90
+ variables: parameters.variables,
91
+ operationName: parameters.operationName,
92
+ onEditQuery: onEditQuery,
93
+ onEditVariables: onEditVariables,
94
+ onEditOperationName: onEditOperationName
95
+ }),
96
+ document.body,
97
+ );
98
+ </script>
99
+ </body>
100
+ </html>
101
+
@@ -1,35 +1,23 @@
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 Base Generator
6
6
  #
7
7
  # A module to help generators to operate
8
8
  module BaseGenerator
9
9
  TEMPALTES_PATH = '../../../generators/graphql/templates'
10
+ APP_MODULE_NAME = Rails.application.class.name.chomp('::Application')
10
11
 
11
12
  def self.included(base)
13
+ base.const_set(:APP_MODULE_NAME, APP_MODULE_NAME)
12
14
  base.send(:namespace, "graphql:#{base.name.demodulize.underscore[0..-11]}")
13
15
  base.send(:source_root, File.expand_path(TEMPALTES_PATH, __dir__))
14
- base.send(:class_option, :directory,
15
- type: :string,
16
+ base.send(:class_option, :directory, type: :string,
16
17
  default: 'app/graphql',
17
18
  desc: 'Directory where generated files should be saved',
18
19
  )
19
20
  end
20
-
21
- protected
22
-
23
- def app_module_name
24
- require File.expand_path('config/application', destination_root)
25
-
26
- app_class = Rails.application.class
27
- source_name = app_class.respond_to?(:module_parent_name) \
28
- ? :module_parent_name \
29
- : :parent_name
30
-
31
- app_class.send(source_name)
32
- end
33
21
  end
34
22
  end
35
23
  end
@@ -0,0 +1,157 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module GraphQL
5
+ # = GraphQL Channel
6
+ #
7
+ # The channel helper methods that allow GraphQL to be performed on an
8
+ # Action Cable channel. It also provides structure fro working with
9
+ # subscriptions
10
+ module Channel
11
+ extend ActiveSupport::Concern
12
+
13
+ included do
14
+ # Each channel is assigned to a GraphQL schema on which the requests
15
+ # will be performed from. It can be a string or the class
16
+ class_attribute :gql_schema, instance_accessor: false
17
+
18
+ # Set it up a callback after unsubscribed so that all the subscriptions
19
+ # can be properly unsubscribed
20
+ after_unsubscribe :gql_clear_subscriptions
21
+ end
22
+
23
+ # The default action of the helper to perform GraphQL requests. Any other
24
+ # action cab be added and use the granular methods here provided
25
+ def execute(data)
26
+ transmit(gql_request_response(data))
27
+ end
28
+
29
+ protected
30
+
31
+ # Identifies if the received request within +data+ should be threated as
32
+ # a compiled request
33
+ def gql_compiled_request?(*)
34
+ false
35
+ end
36
+
37
+ # Get the response of a request withing the given +data+
38
+ def gql_request_response(data)
39
+ xargs = gql_params(data)
40
+ schema, context, query = xargs.extract!(:schema, :context, :query).values
41
+
42
+ request = gql_request(schema)
43
+ request.context = context
44
+ request.execute(query, **xargs)
45
+
46
+ gql_merge_subscriptions(request)
47
+ gql_response(request)
48
+ end
49
+
50
+ # Merge the subscriptions in this channel and the ones that were added
51
+ # by the last request
52
+ def gql_merge_subscriptions(request)
53
+ gql_subscriptions.merge!(request.subscriptions)
54
+ end
55
+
56
+ # Properly format the response of the provided +request+ so it can be
57
+ # transmitted
58
+ def gql_response(request)
59
+ { result: request.response.as_json, more: request.subscriptions? }
60
+ end
61
+
62
+ # Build the necessary params from the provided data
63
+ def gql_params(data)
64
+ cache_key = gql_query_cache_key(
65
+ data['query_cache_key'],
66
+ data['query_cache_version'],
67
+ )
68
+
69
+ {
70
+ query: data['query'],
71
+ origin: self,
72
+ variables: gql_variables(data),
73
+ operation_name: data['operation_name'] || data['operationName'],
74
+ compiled: gql_compiled_request?(data),
75
+ context: gql_context(data),
76
+ schema: gql_schema(data),
77
+ hash: cache_key,
78
+ as: :hash,
79
+ }
80
+ end
81
+
82
+ # The instance of a GraphQL request. It can't simply perform using
83
+ # +execute+, because it is important to check if any subscription was
84
+ # generated
85
+ def gql_request(schema = gql_schema)
86
+ @gql_request ||= ::Rails::GraphQL::Request.new(schema)
87
+ end
88
+
89
+ # Get the cache key of the query for persisted queries
90
+ def gql_query_cache_key(key = nil, version = nil)
91
+ CacheKey.new(key, version) if key.present?
92
+ end
93
+
94
+ # The schema on which the requests will be performed from
95
+ def gql_schema(*)
96
+ schema = self.class.gql_schema
97
+ schema = schema.constantize if schema.is_a?(String)
98
+ schema ||= gql_application_default_schema
99
+ return schema if schema.is_a?(Module) && schema < ::Rails::GraphQL::Schema
100
+
101
+ raise ExecutionError, (+<<~MSG).squish
102
+ Unable to find a valid schema for #{self.class.name},
103
+ defined value: #{schema.inspect}.
104
+ MSG
105
+ end
106
+
107
+ # Get the GraphQL context for a requests
108
+ # +ActionCable::Channel::Base#extract_action+
109
+ def gql_context(data)
110
+ { action: (data['action'] || :receive).to_sym }
111
+ end
112
+
113
+ # Get the GraphQL variables for a request
114
+ def gql_variables(data, variables = nil)
115
+ variables ||= data['variables']
116
+
117
+ case variables
118
+ when ::ActionController::Parameters then variables.permit!.to_h
119
+ when String then variables.present? ? JSON.parse(variables) : {}
120
+ when Hash then variables
121
+ else {}
122
+ end
123
+ end
124
+
125
+ # The list of ids of subscription and to which field they are
126
+ # associated with
127
+ def gql_subscriptions
128
+ @gql_subscriptions ||= {}
129
+ end
130
+
131
+ # Remove all subscriptions
132
+ def gql_clear_subscriptions
133
+ gql_remove_subscription(*gql_subscriptions.keys) unless gql_subscriptions.empty?
134
+ end
135
+
136
+ # Remove one or more subscriptions
137
+ def gql_remove_subscriptions(*sids)
138
+ gql_schema.remove_subscriptions(*sids)
139
+ end
140
+
141
+ alias gql_remove_subscription gql_remove_subscriptions
142
+
143
+ private
144
+
145
+ # Find the default application schema
146
+ def gql_application_default_schema
147
+ app_class = Rails.application.class
148
+ source_name = app_class.respond_to?(:module_parent_name) \
149
+ ? :module_parent_name \
150
+ : :parent_name
151
+
152
+ klass = "::GraphQL::#{app_class.public_send(source_name)}Schema".constantize
153
+ self.class.gql_schema = klass
154
+ end
155
+ end
156
+ end
157
+ end