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