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.
- checksums.yaml +4 -4
- data/ext/console.rb +18 -0
- data/ext/extconf.h +3 -0
- data/ext/extconf.rb +1 -54
- data/ext/gql_parser.c +631 -0
- data/ext/gql_parser.h +21 -0
- data/ext/shared.c +477 -0
- data/ext/shared.h +177 -0
- data/lib/generators/graphql/channel_generator.rb +27 -0
- data/lib/generators/graphql/controller_generator.rb +9 -4
- data/lib/generators/graphql/install_generator.rb +49 -0
- data/lib/generators/graphql/schema_generator.rb +9 -4
- data/lib/generators/graphql/templates/channel.erb +7 -0
- data/lib/generators/graphql/templates/config.rb +97 -0
- data/lib/generators/graphql/templates/controller.erb +2 -0
- data/lib/generators/graphql/templates/schema.erb +5 -3
- data/lib/gql_parser.so +0 -0
- data/lib/rails/graphql/adapters/mysql_adapter.rb +59 -0
- data/lib/rails/graphql/adapters/pg_adapter.rb +25 -22
- data/lib/rails/graphql/adapters/sqlite_adapter.rb +17 -14
- data/lib/rails/graphql/alternative/field_set.rb +48 -0
- data/lib/rails/graphql/alternative/mutation.rb +17 -0
- data/lib/rails/graphql/alternative/query.rb +98 -0
- data/lib/rails/graphql/alternative/subscription.rb +18 -0
- data/lib/rails/graphql/alternative.rb +20 -0
- data/lib/rails/graphql/argument.rb +25 -26
- data/lib/rails/graphql/callback.rb +30 -14
- data/lib/rails/graphql/collectors/hash_collector.rb +26 -7
- data/lib/rails/graphql/collectors/idented_collector.rb +10 -7
- data/lib/rails/graphql/collectors/json_collector.rb +43 -17
- data/lib/rails/graphql/collectors.rb +4 -4
- data/lib/rails/graphql/config.rb +154 -23
- data/lib/rails/graphql/directive/cached_directive.rb +33 -0
- data/lib/rails/graphql/directive/deprecated_directive.rb +10 -10
- data/lib/rails/graphql/directive/include_directive.rb +4 -4
- data/lib/rails/graphql/directive/skip_directive.rb +4 -4
- data/lib/rails/graphql/directive/specified_by_directive.rb +24 -0
- data/lib/rails/graphql/directive.rb +134 -73
- data/lib/rails/graphql/errors.rb +33 -4
- data/lib/rails/graphql/event.rb +21 -9
- data/lib/rails/graphql/field/authorized_field.rb +17 -6
- data/lib/rails/graphql/field/input_field.rb +8 -12
- data/lib/rails/graphql/field/mutation_field.rb +43 -9
- data/lib/rails/graphql/field/output_field.rb +112 -12
- data/lib/rails/graphql/field/proxied_field.rb +35 -26
- data/lib/rails/graphql/field/resolved_field.rb +27 -25
- data/lib/rails/graphql/field/scoped_config.rb +10 -4
- data/lib/rails/graphql/field/subscription_field.rb +123 -0
- data/lib/rails/graphql/field/typed_field.rb +69 -24
- data/lib/rails/graphql/field.rb +89 -74
- data/lib/rails/graphql/global_id.rb +89 -0
- data/lib/rails/graphql/helpers/attribute_delegator.rb +5 -5
- data/lib/rails/graphql/helpers/inherited_collection/array.rb +51 -0
- data/lib/rails/graphql/helpers/inherited_collection/base.rb +45 -0
- data/lib/rails/graphql/helpers/inherited_collection/hash.rb +88 -0
- data/lib/rails/graphql/helpers/inherited_collection.rb +25 -76
- data/lib/rails/graphql/helpers/instantiable.rb +15 -0
- data/lib/rails/graphql/helpers/leaf_from_ar.rb +7 -7
- data/lib/rails/graphql/helpers/registerable.rb +44 -62
- data/lib/rails/graphql/helpers/unregisterable.rb +16 -0
- data/lib/rails/graphql/helpers/with_arguments.rb +33 -28
- data/lib/rails/graphql/helpers/with_assignment.rb +6 -6
- data/lib/rails/graphql/helpers/with_callbacks.rb +28 -11
- data/lib/rails/graphql/helpers/with_description.rb +73 -0
- data/lib/rails/graphql/helpers/with_directives.rb +58 -30
- data/lib/rails/graphql/helpers/with_events.rb +22 -23
- data/lib/rails/graphql/helpers/with_fields.rb +86 -26
- data/lib/rails/graphql/helpers/with_global_id.rb +22 -0
- data/lib/rails/graphql/helpers/with_name.rb +44 -0
- data/lib/rails/graphql/helpers/with_namespace.rb +7 -4
- data/lib/rails/graphql/helpers/with_owner.rb +8 -7
- data/lib/rails/graphql/helpers/with_schema_fields.rb +162 -56
- data/lib/rails/graphql/helpers/with_validator.rb +9 -9
- data/lib/rails/graphql/helpers.rb +10 -3
- data/lib/rails/graphql/introspection.rb +43 -36
- data/lib/rails/graphql/railtie.rb +89 -33
- data/lib/rails/graphql/railties/app/base_channel.rb +10 -0
- data/lib/rails/graphql/railties/app/base_controller.rb +12 -0
- data/lib/rails/graphql/railties/app/views/_cable.js.erb +56 -0
- data/lib/rails/graphql/railties/app/views/_fetch.js.erb +20 -0
- data/lib/rails/graphql/railties/app/views/graphiql.html.erb +101 -0
- data/lib/rails/graphql/railties/base_generator.rb +5 -17
- data/lib/rails/graphql/railties/channel.rb +157 -0
- data/lib/rails/graphql/railties/controller.rb +91 -25
- data/lib/rails/graphql/railties/controller_runtime.rb +5 -5
- data/lib/rails/graphql/railties/log_subscriber.rb +81 -14
- data/lib/rails/graphql/request/arguments.rb +26 -50
- data/lib/rails/graphql/request/backtrace.rb +212 -0
- data/lib/rails/graphql/request/component/field.rb +98 -70
- data/lib/rails/graphql/request/component/fragment.rb +80 -26
- data/lib/rails/graphql/request/component/operation/subscription.rb +162 -4
- data/lib/rails/graphql/request/component/operation.rb +73 -34
- data/lib/rails/graphql/request/component/spread.rb +79 -27
- data/lib/rails/graphql/request/component/typename.rb +28 -13
- data/lib/rails/graphql/request/component.rb +77 -36
- data/lib/rails/graphql/request/context.rb +19 -9
- data/lib/rails/graphql/request/errors.rb +16 -6
- data/lib/rails/graphql/request/event.rb +23 -8
- data/lib/rails/graphql/request/helpers/directives.rb +69 -27
- data/lib/rails/graphql/request/helpers/selection_set.rb +57 -25
- data/lib/rails/graphql/request/helpers/value_writers.rb +24 -19
- data/lib/rails/graphql/request/prepared_data.rb +100 -0
- data/lib/rails/graphql/request/steps/authorizable.rb +24 -14
- data/lib/rails/graphql/request/steps/organizable.rb +111 -49
- data/lib/rails/graphql/request/steps/{prepareable.rb → preparable.rb} +21 -8
- data/lib/rails/graphql/request/steps/{resolveable.rb → resolvable.rb} +16 -7
- data/lib/rails/graphql/request/strategy/cached_strategy.rb +64 -0
- data/lib/rails/graphql/request/strategy/dynamic_instance.rb +6 -6
- data/lib/rails/graphql/request/strategy/multi_query_strategy.rb +6 -13
- data/lib/rails/graphql/request/strategy/sequenced_strategy.rb +9 -9
- data/lib/rails/graphql/request/strategy.rb +147 -77
- data/lib/rails/graphql/request/subscription.rb +82 -0
- data/lib/rails/graphql/request.rb +353 -104
- data/lib/rails/graphql/schema.rb +251 -106
- data/lib/rails/graphql/shortcuts.rb +33 -8
- data/lib/rails/graphql/source/active_record/builders.rb +64 -51
- data/lib/rails/graphql/source/active_record_source.rb +158 -82
- data/lib/rails/graphql/source/base.rb +83 -0
- data/lib/rails/graphql/source/builder.rb +115 -0
- data/lib/rails/graphql/source/scoped_arguments.rb +39 -21
- data/lib/rails/graphql/source.rb +90 -228
- data/lib/rails/graphql/subscription/provider/action_cable.rb +113 -0
- data/lib/rails/graphql/subscription/provider/base.rb +192 -0
- data/lib/rails/graphql/subscription/provider.rb +18 -0
- data/lib/rails/graphql/subscription/store/base.rb +141 -0
- data/lib/rails/graphql/subscription/store/memory.rb +136 -0
- data/lib/rails/graphql/subscription/store.rb +19 -0
- data/lib/rails/graphql/subscription.rb +17 -0
- data/lib/rails/graphql/to_gql.rb +29 -32
- data/lib/rails/graphql/type/creator.rb +196 -0
- data/lib/rails/graphql/type/enum/directive_location_enum.rb +11 -11
- data/lib/rails/graphql/type/enum/type_kind_enum.rb +3 -3
- data/lib/rails/graphql/type/enum.rb +44 -50
- data/lib/rails/graphql/type/input.rb +92 -25
- data/lib/rails/graphql/type/interface.rb +29 -28
- data/lib/rails/graphql/type/object/directive_object.rb +10 -9
- data/lib/rails/graphql/type/object/enum_value_object.rb +3 -3
- data/lib/rails/graphql/type/object/field_object.rb +24 -6
- data/lib/rails/graphql/type/object/input_value_object.rb +6 -7
- data/lib/rails/graphql/type/object/schema_object.rb +5 -8
- data/lib/rails/graphql/type/object/type_object.rb +62 -25
- data/lib/rails/graphql/type/object.rb +34 -26
- data/lib/rails/graphql/type/scalar/any_scalar.rb +30 -0
- data/lib/rails/graphql/type/scalar/bigint_scalar.rb +5 -5
- data/lib/rails/graphql/type/scalar/binary_scalar.rb +5 -3
- data/lib/rails/graphql/type/scalar/boolean_scalar.rb +8 -8
- data/lib/rails/graphql/type/scalar/date_scalar.rb +5 -3
- data/lib/rails/graphql/type/scalar/date_time_scalar.rb +5 -3
- data/lib/rails/graphql/type/scalar/decimal_scalar.rb +5 -3
- data/lib/rails/graphql/type/scalar/float_scalar.rb +5 -5
- data/lib/rails/graphql/type/scalar/id_scalar.rb +6 -5
- data/lib/rails/graphql/type/scalar/int_scalar.rb +6 -5
- data/lib/rails/graphql/type/scalar/json_scalar.rb +41 -0
- data/lib/rails/graphql/type/scalar/string_scalar.rb +18 -4
- data/lib/rails/graphql/type/scalar/time_scalar.rb +8 -6
- data/lib/rails/graphql/type/scalar.rb +26 -23
- data/lib/rails/graphql/type/union.rb +21 -18
- data/lib/rails/graphql/type.rb +43 -26
- data/lib/rails/graphql/type_map.rb +268 -165
- data/lib/rails/graphql/uri.rb +167 -0
- data/lib/rails/graphql/version.rb +19 -3
- data/lib/rails/graphql.rake +3 -0
- data/lib/rails/graphql.rb +91 -56
- data/lib/rails-graphql.rb +1 -1
- data/test/assets/en.yml +29 -0
- data/test/assets/introspection-mem.txt +1 -1
- data/test/assets/introspection.gql +2 -0
- data/test/assets/mem.gql +86 -99
- data/test/assets/mysql.gql +406 -0
- data/test/assets/sqlite.gql +96 -73
- data/test/assets/translate.gql +346 -0
- data/test/config.rb +19 -8
- data/test/graphql/schema_test.rb +14 -50
- data/test/graphql/source_test.rb +8 -85
- data/test/graphql/type/enum_test.rb +207 -203
- data/test/graphql/type/input_test.rb +14 -9
- data/test/graphql/type/interface_test.rb +12 -9
- data/test/graphql/type/object_test.rb +8 -2
- data/test/graphql/type/scalar/any_scalar_test.rb +38 -0
- data/test/graphql/type/scalar/boolean_scalar_test.rb +6 -3
- data/test/graphql/type/scalar/json_scalar_test.rb +23 -0
- data/test/graphql/type_map_test.rb +63 -81
- data/test/graphql/type_test.rb +0 -19
- data/test/graphql_test.rb +1 -1
- data/test/integration/{authorization/authorization_test.rb → authorization_test.rb} +40 -14
- data/test/integration/config.rb +36 -3
- data/test/integration/customization_test.rb +39 -0
- data/test/integration/global_id_test.rb +99 -0
- data/test/integration/memory/star_wars_introspection_test.rb +24 -16
- data/test/integration/memory/star_wars_query_test.rb +54 -3
- data/test/integration/memory/star_wars_validation_test.rb +3 -3
- data/test/integration/mysql/star_wars_introspection_test.rb +25 -0
- data/test/integration/persisted_query_test.rb +87 -0
- data/test/integration/resolver_precedence_test.rb +154 -0
- data/test/integration/schemas/memory.rb +24 -10
- data/test/integration/schemas/mysql.rb +62 -0
- data/test/integration/schemas/sqlite.rb +21 -12
- data/test/integration/sqlite/star_wars_global_id_test.rb +89 -0
- data/test/integration/sqlite/star_wars_introspection_test.rb +10 -0
- data/test/integration/sqlite/star_wars_query_test.rb +14 -1
- data/test/integration/translate_test.rb +73 -0
- data/test/test_ext.rb +16 -13
- metadata +125 -161
- data/ext/depend +0 -3
- data/ext/graphqlparser/Ast.cpp +0 -346
- data/ext/graphqlparser/Ast.h +0 -1214
- data/ext/graphqlparser/AstNode.h +0 -36
- data/ext/graphqlparser/AstVisitor.h +0 -137
- data/ext/graphqlparser/GraphQLParser.cpp +0 -76
- data/ext/graphqlparser/GraphQLParser.h +0 -55
- data/ext/graphqlparser/JsonVisitor.cpp +0 -161
- data/ext/graphqlparser/JsonVisitor.cpp.inc +0 -456
- data/ext/graphqlparser/JsonVisitor.h +0 -121
- data/ext/graphqlparser/JsonVisitor.h.inc +0 -110
- data/ext/graphqlparser/VERSION +0 -1
- data/ext/graphqlparser/c/GraphQLAst.cpp +0 -324
- data/ext/graphqlparser/c/GraphQLAst.h +0 -180
- data/ext/graphqlparser/c/GraphQLAstForEachConcreteType.h +0 -44
- data/ext/graphqlparser/c/GraphQLAstNode.cpp +0 -25
- data/ext/graphqlparser/c/GraphQLAstNode.h +0 -33
- data/ext/graphqlparser/c/GraphQLAstToJSON.cpp +0 -21
- data/ext/graphqlparser/c/GraphQLAstToJSON.h +0 -24
- data/ext/graphqlparser/c/GraphQLAstVisitor.cpp +0 -55
- data/ext/graphqlparser/c/GraphQLAstVisitor.h +0 -53
- data/ext/graphqlparser/c/GraphQLParser.cpp +0 -35
- data/ext/graphqlparser/c/GraphQLParser.h +0 -54
- data/ext/graphqlparser/dump_json_ast.cpp +0 -48
- data/ext/graphqlparser/lexer.lpp +0 -324
- data/ext/graphqlparser/parser.ypp +0 -693
- data/ext/graphqlparser/parsergen/lexer.cpp +0 -2633
- data/ext/graphqlparser/parsergen/lexer.h +0 -528
- data/ext/graphqlparser/parsergen/location.hh +0 -189
- data/ext/graphqlparser/parsergen/parser.tab.cpp +0 -3300
- data/ext/graphqlparser/parsergen/parser.tab.hpp +0 -646
- data/ext/graphqlparser/parsergen/position.hh +0 -179
- data/ext/graphqlparser/parsergen/stack.hh +0 -156
- data/ext/graphqlparser/syntaxdefs.h +0 -19
- data/ext/libgraphqlparser/AstNode.h +0 -36
- data/ext/libgraphqlparser/CMakeLists.txt +0 -148
- data/ext/libgraphqlparser/CONTRIBUTING.md +0 -23
- data/ext/libgraphqlparser/GraphQLParser.cpp +0 -76
- data/ext/libgraphqlparser/GraphQLParser.h +0 -55
- data/ext/libgraphqlparser/JsonVisitor.cpp +0 -161
- data/ext/libgraphqlparser/JsonVisitor.h +0 -121
- data/ext/libgraphqlparser/LICENSE +0 -22
- data/ext/libgraphqlparser/README.clang-tidy +0 -7
- data/ext/libgraphqlparser/README.md +0 -84
- data/ext/libgraphqlparser/ast/ast.ast +0 -203
- data/ext/libgraphqlparser/ast/ast.py +0 -61
- data/ext/libgraphqlparser/ast/c.py +0 -100
- data/ext/libgraphqlparser/ast/c.pyc +0 -0
- data/ext/libgraphqlparser/ast/c_impl.py +0 -61
- data/ext/libgraphqlparser/ast/c_impl.pyc +0 -0
- data/ext/libgraphqlparser/ast/c_visitor_impl.py +0 -39
- data/ext/libgraphqlparser/ast/c_visitor_impl.pyc +0 -0
- data/ext/libgraphqlparser/ast/casing.py +0 -26
- data/ext/libgraphqlparser/ast/casing.pyc +0 -0
- data/ext/libgraphqlparser/ast/cxx.py +0 -197
- data/ext/libgraphqlparser/ast/cxx.pyc +0 -0
- data/ext/libgraphqlparser/ast/cxx_impl.py +0 -61
- data/ext/libgraphqlparser/ast/cxx_impl.pyc +0 -0
- data/ext/libgraphqlparser/ast/cxx_json_visitor_header.py +0 -42
- data/ext/libgraphqlparser/ast/cxx_json_visitor_header.pyc +0 -0
- data/ext/libgraphqlparser/ast/cxx_json_visitor_impl.py +0 -80
- data/ext/libgraphqlparser/ast/cxx_json_visitor_impl.pyc +0 -0
- data/ext/libgraphqlparser/ast/cxx_visitor.py +0 -64
- data/ext/libgraphqlparser/ast/cxx_visitor.pyc +0 -0
- data/ext/libgraphqlparser/ast/js.py +0 -65
- data/ext/libgraphqlparser/ast/license.py +0 -10
- data/ext/libgraphqlparser/ast/license.pyc +0 -0
- data/ext/libgraphqlparser/c/GraphQLAstNode.cpp +0 -25
- data/ext/libgraphqlparser/c/GraphQLAstNode.h +0 -33
- data/ext/libgraphqlparser/c/GraphQLAstToJSON.cpp +0 -21
- data/ext/libgraphqlparser/c/GraphQLAstToJSON.h +0 -24
- data/ext/libgraphqlparser/c/GraphQLAstVisitor.cpp +0 -55
- data/ext/libgraphqlparser/c/GraphQLAstVisitor.h +0 -53
- data/ext/libgraphqlparser/c/GraphQLParser.cpp +0 -35
- data/ext/libgraphqlparser/c/GraphQLParser.h +0 -54
- data/ext/libgraphqlparser/clang-tidy-all.sh +0 -3
- data/ext/libgraphqlparser/cmake/version.cmake +0 -16
- data/ext/libgraphqlparser/dump_json_ast.cpp +0 -48
- data/ext/libgraphqlparser/go/README.md +0 -20
- data/ext/libgraphqlparser/go/callbacks.go +0 -18
- data/ext/libgraphqlparser/go/gotest.go +0 -64
- data/ext/libgraphqlparser/lexer.lpp +0 -324
- data/ext/libgraphqlparser/libgraphqlparser.pc.in +0 -11
- data/ext/libgraphqlparser/parser.ypp +0 -693
- data/ext/libgraphqlparser/parsergen/lexer.cpp +0 -2633
- data/ext/libgraphqlparser/parsergen/lexer.h +0 -528
- data/ext/libgraphqlparser/parsergen/location.hh +0 -189
- data/ext/libgraphqlparser/parsergen/parser.tab.cpp +0 -3300
- data/ext/libgraphqlparser/parsergen/parser.tab.hpp +0 -646
- data/ext/libgraphqlparser/parsergen/position.hh +0 -179
- data/ext/libgraphqlparser/parsergen/stack.hh +0 -156
- data/ext/libgraphqlparser/python/CMakeLists.txt +0 -14
- data/ext/libgraphqlparser/python/README.md +0 -5
- data/ext/libgraphqlparser/python/example.py +0 -31
- data/ext/libgraphqlparser/syntaxdefs.h +0 -19
- data/ext/libgraphqlparser/test/BuildCAPI.c +0 -5
- data/ext/libgraphqlparser/test/CMakeLists.txt +0 -25
- data/ext/libgraphqlparser/test/JsonVisitorTests.cpp +0 -28
- data/ext/libgraphqlparser/test/ParserTests.cpp +0 -352
- data/ext/libgraphqlparser/test/kitchen-sink.graphql +0 -59
- data/ext/libgraphqlparser/test/kitchen-sink.json +0 -1
- data/ext/libgraphqlparser/test/schema-kitchen-sink.graphql +0 -78
- data/ext/libgraphqlparser/test/schema-kitchen-sink.json +0 -1
- data/ext/libgraphqlparser/test/valgrind.supp +0 -33
- data/ext/version.cpp +0 -21
- data/lib/graphqlparser.so +0 -0
- data/lib/rails/graphql/native/functions.rb +0 -38
- data/lib/rails/graphql/native/location.rb +0 -41
- data/lib/rails/graphql/native/pointers.rb +0 -23
- data/lib/rails/graphql/native/visitor.rb +0 -349
- data/lib/rails/graphql/native.rb +0 -56
- 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
|
8
|
-
module GraphQL
|
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
|
-
|
29
|
-
|
33
|
+
|
34
|
+
logger = ::Rails.logger
|
35
|
+
if logger.respond_to?(:tagged)
|
36
|
+
config.logger = logger
|
30
37
|
else
|
31
|
-
config.logger = ActiveSupport::TaggedLogging.new(
|
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
|
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
|
-
|
88
|
+
LogSubscriber.backtrace_cleaner = ::Rails.backtrace_cleaner
|
49
89
|
end
|
50
90
|
end
|
51
91
|
|
52
|
-
# Add
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
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
|
-
|
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
|
-
|
65
|
-
|
66
|
-
|
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
|
-
#
|
74
|
-
#
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
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,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…
|
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
|
4
|
-
module GraphQL
|
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
|