rails-graphql 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +31 -0
- data/ext/depend +3 -0
- data/ext/extconf.rb +57 -0
- data/ext/graphqlparser/Ast.cpp +346 -0
- data/ext/graphqlparser/Ast.h +1214 -0
- data/ext/graphqlparser/AstNode.h +36 -0
- data/ext/graphqlparser/AstVisitor.h +137 -0
- data/ext/graphqlparser/GraphQLParser.cpp +76 -0
- data/ext/graphqlparser/GraphQLParser.h +55 -0
- data/ext/graphqlparser/JsonVisitor.cpp +161 -0
- data/ext/graphqlparser/JsonVisitor.cpp.inc +456 -0
- data/ext/graphqlparser/JsonVisitor.h +121 -0
- data/ext/graphqlparser/JsonVisitor.h.inc +110 -0
- data/ext/graphqlparser/VERSION +1 -0
- data/ext/graphqlparser/c/GraphQLAst.cpp +324 -0
- data/ext/graphqlparser/c/GraphQLAst.h +180 -0
- data/ext/graphqlparser/c/GraphQLAstForEachConcreteType.h +44 -0
- data/ext/graphqlparser/c/GraphQLAstNode.cpp +25 -0
- data/ext/graphqlparser/c/GraphQLAstNode.h +33 -0
- data/ext/graphqlparser/c/GraphQLAstToJSON.cpp +21 -0
- data/ext/graphqlparser/c/GraphQLAstToJSON.h +24 -0
- data/ext/graphqlparser/c/GraphQLAstVisitor.cpp +55 -0
- data/ext/graphqlparser/c/GraphQLAstVisitor.h +53 -0
- data/ext/graphqlparser/c/GraphQLParser.cpp +35 -0
- data/ext/graphqlparser/c/GraphQLParser.h +54 -0
- data/ext/graphqlparser/dump_json_ast.cpp +48 -0
- data/ext/graphqlparser/lexer.lpp +324 -0
- data/ext/graphqlparser/parser.ypp +693 -0
- data/ext/graphqlparser/parsergen/lexer.cpp +2633 -0
- data/ext/graphqlparser/parsergen/lexer.h +528 -0
- data/ext/graphqlparser/parsergen/location.hh +189 -0
- data/ext/graphqlparser/parsergen/parser.tab.cpp +3300 -0
- data/ext/graphqlparser/parsergen/parser.tab.hpp +646 -0
- data/ext/graphqlparser/parsergen/position.hh +179 -0
- data/ext/graphqlparser/parsergen/stack.hh +156 -0
- data/ext/graphqlparser/syntaxdefs.h +19 -0
- data/ext/libgraphqlparser/AstNode.h +36 -0
- data/ext/libgraphqlparser/CMakeLists.txt +148 -0
- data/ext/libgraphqlparser/CONTRIBUTING.md +23 -0
- data/ext/libgraphqlparser/GraphQLParser.cpp +76 -0
- data/ext/libgraphqlparser/GraphQLParser.h +55 -0
- data/ext/libgraphqlparser/JsonVisitor.cpp +161 -0
- data/ext/libgraphqlparser/JsonVisitor.h +121 -0
- data/ext/libgraphqlparser/LICENSE +22 -0
- data/ext/libgraphqlparser/README.clang-tidy +7 -0
- data/ext/libgraphqlparser/README.md +84 -0
- data/ext/libgraphqlparser/ast/ast.ast +203 -0
- data/ext/libgraphqlparser/ast/ast.py +61 -0
- data/ext/libgraphqlparser/ast/c.py +100 -0
- data/ext/libgraphqlparser/ast/c.pyc +0 -0
- data/ext/libgraphqlparser/ast/c_impl.py +61 -0
- data/ext/libgraphqlparser/ast/c_impl.pyc +0 -0
- data/ext/libgraphqlparser/ast/c_visitor_impl.py +39 -0
- data/ext/libgraphqlparser/ast/c_visitor_impl.pyc +0 -0
- data/ext/libgraphqlparser/ast/casing.py +26 -0
- data/ext/libgraphqlparser/ast/casing.pyc +0 -0
- data/ext/libgraphqlparser/ast/cxx.py +197 -0
- data/ext/libgraphqlparser/ast/cxx.pyc +0 -0
- data/ext/libgraphqlparser/ast/cxx_impl.py +61 -0
- data/ext/libgraphqlparser/ast/cxx_impl.pyc +0 -0
- data/ext/libgraphqlparser/ast/cxx_json_visitor_header.py +42 -0
- data/ext/libgraphqlparser/ast/cxx_json_visitor_header.pyc +0 -0
- data/ext/libgraphqlparser/ast/cxx_json_visitor_impl.py +80 -0
- data/ext/libgraphqlparser/ast/cxx_json_visitor_impl.pyc +0 -0
- data/ext/libgraphqlparser/ast/cxx_visitor.py +64 -0
- data/ext/libgraphqlparser/ast/cxx_visitor.pyc +0 -0
- data/ext/libgraphqlparser/ast/js.py +65 -0
- data/ext/libgraphqlparser/ast/license.py +10 -0
- data/ext/libgraphqlparser/ast/license.pyc +0 -0
- data/ext/libgraphqlparser/c/GraphQLAstNode.cpp +25 -0
- data/ext/libgraphqlparser/c/GraphQLAstNode.h +33 -0
- data/ext/libgraphqlparser/c/GraphQLAstToJSON.cpp +21 -0
- data/ext/libgraphqlparser/c/GraphQLAstToJSON.h +24 -0
- data/ext/libgraphqlparser/c/GraphQLAstVisitor.cpp +55 -0
- data/ext/libgraphqlparser/c/GraphQLAstVisitor.h +53 -0
- data/ext/libgraphqlparser/c/GraphQLParser.cpp +35 -0
- data/ext/libgraphqlparser/c/GraphQLParser.h +54 -0
- data/ext/libgraphqlparser/clang-tidy-all.sh +3 -0
- data/ext/libgraphqlparser/cmake/version.cmake +16 -0
- data/ext/libgraphqlparser/dump_json_ast.cpp +48 -0
- data/ext/libgraphqlparser/go/README.md +20 -0
- data/ext/libgraphqlparser/go/callbacks.go +18 -0
- data/ext/libgraphqlparser/go/gotest.go +64 -0
- data/ext/libgraphqlparser/lexer.lpp +324 -0
- data/ext/libgraphqlparser/libgraphqlparser.pc.in +11 -0
- data/ext/libgraphqlparser/parser.ypp +693 -0
- data/ext/libgraphqlparser/parsergen/lexer.cpp +2633 -0
- data/ext/libgraphqlparser/parsergen/lexer.h +528 -0
- data/ext/libgraphqlparser/parsergen/location.hh +189 -0
- data/ext/libgraphqlparser/parsergen/parser.tab.cpp +3300 -0
- data/ext/libgraphqlparser/parsergen/parser.tab.hpp +646 -0
- data/ext/libgraphqlparser/parsergen/position.hh +179 -0
- data/ext/libgraphqlparser/parsergen/stack.hh +156 -0
- data/ext/libgraphqlparser/python/CMakeLists.txt +14 -0
- data/ext/libgraphqlparser/python/README.md +5 -0
- data/ext/libgraphqlparser/python/example.py +31 -0
- data/ext/libgraphqlparser/syntaxdefs.h +19 -0
- data/ext/libgraphqlparser/test/BuildCAPI.c +5 -0
- data/ext/libgraphqlparser/test/CMakeLists.txt +25 -0
- data/ext/libgraphqlparser/test/JsonVisitorTests.cpp +28 -0
- data/ext/libgraphqlparser/test/ParserTests.cpp +352 -0
- data/ext/libgraphqlparser/test/kitchen-sink.graphql +59 -0
- data/ext/libgraphqlparser/test/kitchen-sink.json +1 -0
- data/ext/libgraphqlparser/test/schema-kitchen-sink.graphql +78 -0
- data/ext/libgraphqlparser/test/schema-kitchen-sink.json +1 -0
- data/ext/libgraphqlparser/test/valgrind.supp +33 -0
- data/ext/version.cpp +21 -0
- data/lib/generators/graphql/controller_generator.rb +22 -0
- data/lib/generators/graphql/schema_generator.rb +22 -0
- data/lib/generators/graphql/templates/controller.erb +5 -0
- data/lib/generators/graphql/templates/schema.erb +6 -0
- data/lib/graphqlparser.so +0 -0
- data/lib/rails-graphql.rb +2 -0
- data/lib/rails/graphql.rake +1 -0
- data/lib/rails/graphql.rb +185 -0
- data/lib/rails/graphql/adapters/mysql_adapter.rb +0 -0
- data/lib/rails/graphql/adapters/pg_adapter.rb +50 -0
- data/lib/rails/graphql/adapters/sqlite_adapter.rb +39 -0
- data/lib/rails/graphql/argument.rb +220 -0
- data/lib/rails/graphql/callback.rb +124 -0
- data/lib/rails/graphql/collectors.rb +14 -0
- data/lib/rails/graphql/collectors/hash_collector.rb +83 -0
- data/lib/rails/graphql/collectors/idented_collector.rb +73 -0
- data/lib/rails/graphql/collectors/json_collector.rb +114 -0
- data/lib/rails/graphql/config.rb +61 -0
- data/lib/rails/graphql/directive.rb +203 -0
- data/lib/rails/graphql/directive/deprecated_directive.rb +59 -0
- data/lib/rails/graphql/directive/include_directive.rb +24 -0
- data/lib/rails/graphql/directive/skip_directive.rb +24 -0
- data/lib/rails/graphql/errors.rb +42 -0
- data/lib/rails/graphql/event.rb +141 -0
- data/lib/rails/graphql/field.rb +318 -0
- data/lib/rails/graphql/field/input_field.rb +92 -0
- data/lib/rails/graphql/field/mutation_field.rb +52 -0
- data/lib/rails/graphql/field/output_field.rb +96 -0
- data/lib/rails/graphql/field/proxied_field.rb +131 -0
- data/lib/rails/graphql/field/resolved_field.rb +96 -0
- data/lib/rails/graphql/field/scoped_config.rb +22 -0
- data/lib/rails/graphql/field/typed_field.rb +104 -0
- data/lib/rails/graphql/helpers.rb +40 -0
- data/lib/rails/graphql/helpers/attribute_delegator.rb +39 -0
- data/lib/rails/graphql/helpers/inherited_collection.rb +152 -0
- data/lib/rails/graphql/helpers/leaf_from_ar.rb +141 -0
- data/lib/rails/graphql/helpers/registerable.rb +103 -0
- data/lib/rails/graphql/helpers/with_arguments.rb +125 -0
- data/lib/rails/graphql/helpers/with_assignment.rb +113 -0
- data/lib/rails/graphql/helpers/with_callbacks.rb +55 -0
- data/lib/rails/graphql/helpers/with_directives.rb +126 -0
- data/lib/rails/graphql/helpers/with_events.rb +81 -0
- data/lib/rails/graphql/helpers/with_fields.rb +141 -0
- data/lib/rails/graphql/helpers/with_namespace.rb +40 -0
- data/lib/rails/graphql/helpers/with_owner.rb +35 -0
- data/lib/rails/graphql/helpers/with_schema_fields.rb +230 -0
- data/lib/rails/graphql/helpers/with_validator.rb +52 -0
- data/lib/rails/graphql/introspection.rb +53 -0
- data/lib/rails/graphql/native.rb +56 -0
- data/lib/rails/graphql/native/functions.rb +38 -0
- data/lib/rails/graphql/native/location.rb +41 -0
- data/lib/rails/graphql/native/pointers.rb +23 -0
- data/lib/rails/graphql/native/visitor.rb +349 -0
- data/lib/rails/graphql/railtie.rb +85 -0
- data/lib/rails/graphql/railties/base_generator.rb +35 -0
- data/lib/rails/graphql/railties/controller.rb +101 -0
- data/lib/rails/graphql/railties/controller_runtime.rb +40 -0
- data/lib/rails/graphql/railties/log_subscriber.rb +62 -0
- data/lib/rails/graphql/request.rb +343 -0
- data/lib/rails/graphql/request/arguments.rb +93 -0
- data/lib/rails/graphql/request/component.rb +100 -0
- data/lib/rails/graphql/request/component/field.rb +225 -0
- data/lib/rails/graphql/request/component/fragment.rb +118 -0
- data/lib/rails/graphql/request/component/operation.rb +178 -0
- data/lib/rails/graphql/request/component/operation/subscription.rb +16 -0
- data/lib/rails/graphql/request/component/spread.rb +119 -0
- data/lib/rails/graphql/request/component/typename.rb +82 -0
- data/lib/rails/graphql/request/context.rb +51 -0
- data/lib/rails/graphql/request/errors.rb +54 -0
- data/lib/rails/graphql/request/event.rb +112 -0
- data/lib/rails/graphql/request/helpers/directives.rb +64 -0
- data/lib/rails/graphql/request/helpers/selection_set.rb +87 -0
- data/lib/rails/graphql/request/helpers/value_writers.rb +115 -0
- data/lib/rails/graphql/request/steps/organizable.rb +146 -0
- data/lib/rails/graphql/request/steps/prepareable.rb +33 -0
- data/lib/rails/graphql/request/steps/resolveable.rb +32 -0
- data/lib/rails/graphql/request/strategy.rb +249 -0
- data/lib/rails/graphql/request/strategy/dynamic_instance.rb +41 -0
- data/lib/rails/graphql/request/strategy/multi_query_strategy.rb +36 -0
- data/lib/rails/graphql/request/strategy/sequenced_strategy.rb +28 -0
- data/lib/rails/graphql/schema.rb +272 -0
- data/lib/rails/graphql/shortcuts.rb +77 -0
- data/lib/rails/graphql/source.rb +371 -0
- data/lib/rails/graphql/source/active_record/builders.rb +154 -0
- data/lib/rails/graphql/source/active_record_source.rb +231 -0
- data/lib/rails/graphql/source/scoped_arguments.rb +87 -0
- data/lib/rails/graphql/to_gql.rb +368 -0
- data/lib/rails/graphql/type.rb +138 -0
- data/lib/rails/graphql/type/enum.rb +206 -0
- data/lib/rails/graphql/type/enum/directive_location_enum.rb +30 -0
- data/lib/rails/graphql/type/enum/type_kind_enum.rb +57 -0
- data/lib/rails/graphql/type/input.rb +134 -0
- data/lib/rails/graphql/type/interface.rb +82 -0
- data/lib/rails/graphql/type/object.rb +111 -0
- data/lib/rails/graphql/type/object/directive_object.rb +34 -0
- data/lib/rails/graphql/type/object/enum_value_object.rb +25 -0
- data/lib/rails/graphql/type/object/field_object.rb +54 -0
- data/lib/rails/graphql/type/object/input_value_object.rb +49 -0
- data/lib/rails/graphql/type/object/schema_object.rb +40 -0
- data/lib/rails/graphql/type/object/type_object.rb +136 -0
- data/lib/rails/graphql/type/scalar.rb +71 -0
- data/lib/rails/graphql/type/scalar/bigint_scalar.rb +34 -0
- data/lib/rails/graphql/type/scalar/binary_scalar.rb +30 -0
- data/lib/rails/graphql/type/scalar/boolean_scalar.rb +37 -0
- data/lib/rails/graphql/type/scalar/date_scalar.rb +34 -0
- data/lib/rails/graphql/type/scalar/date_time_scalar.rb +32 -0
- data/lib/rails/graphql/type/scalar/decimal_scalar.rb +35 -0
- data/lib/rails/graphql/type/scalar/float_scalar.rb +32 -0
- data/lib/rails/graphql/type/scalar/id_scalar.rb +39 -0
- data/lib/rails/graphql/type/scalar/int_scalar.rb +36 -0
- data/lib/rails/graphql/type/scalar/string_scalar.rb +28 -0
- data/lib/rails/graphql/type/scalar/time_scalar.rb +40 -0
- data/lib/rails/graphql/type/union.rb +87 -0
- data/lib/rails/graphql/type_map.rb +347 -0
- data/lib/rails/graphql/version.rb +7 -0
- data/test/assets/introspection-db.json +0 -0
- data/test/assets/introspection-mem.txt +1 -0
- data/test/assets/introspection.gql +91 -0
- data/test/assets/luke.jpg +0 -0
- data/test/assets/mem.gql +428 -0
- data/test/assets/sqlite.gql +423 -0
- data/test/config.rb +80 -0
- data/test/graphql/request/context_test.rb +70 -0
- data/test/graphql/schema_test.rb +190 -0
- data/test/graphql/source_test.rb +237 -0
- data/test/graphql/type/enum_test.rb +203 -0
- data/test/graphql/type/input_test.rb +138 -0
- data/test/graphql/type/interface_test.rb +72 -0
- data/test/graphql/type/object_test.rb +104 -0
- data/test/graphql/type/scalar/bigint_scalar_test.rb +42 -0
- data/test/graphql/type/scalar/binary_scalar_test.rb +17 -0
- data/test/graphql/type/scalar/boolean_scalar_test.rb +40 -0
- data/test/graphql/type/scalar/date_scalar_test.rb +29 -0
- data/test/graphql/type/scalar/date_time_scalar_test.rb +29 -0
- data/test/graphql/type/scalar/decimal_scalar_test.rb +28 -0
- data/test/graphql/type/scalar/float_scalar_test.rb +22 -0
- data/test/graphql/type/scalar/id_scalar_test.rb +26 -0
- data/test/graphql/type/scalar/int_scalar_test.rb +26 -0
- data/test/graphql/type/scalar/string_scalar_test.rb +17 -0
- data/test/graphql/type/scalar/time_scalar_test.rb +36 -0
- data/test/graphql/type/scalar_test.rb +45 -0
- data/test/graphql/type/union_test.rb +82 -0
- data/test/graphql/type_map_test.rb +362 -0
- data/test/graphql/type_test.rb +68 -0
- data/test/graphql_test.rb +55 -0
- data/test/integration/config.rb +56 -0
- data/test/integration/memory/star_wars_introspection_test.rb +144 -0
- data/test/integration/memory/star_wars_query_test.rb +184 -0
- data/test/integration/memory/star_wars_validation_test.rb +99 -0
- data/test/integration/schemas/memory.rb +232 -0
- data/test/integration/schemas/sqlite.rb +82 -0
- data/test/integration/sqlite/star_wars_introspection_test.rb +15 -0
- data/test/integration/sqlite/star_wars_mutation_test.rb +82 -0
- data/test/integration/sqlite/star_wars_query_test.rb +71 -0
- data/test/test_ext.rb +48 -0
- metadata +509 -0
@@ -0,0 +1,362 @@
|
|
1
|
+
require 'config'
|
2
|
+
|
3
|
+
class GraphQL_TypeMapTest < GraphQL::TestCase
|
4
|
+
DESCRIBED_CLASS = Rails::GraphQL::TypeMap
|
5
|
+
SAMPLE_INDEX = {
|
6
|
+
base: { Type: { string: -> { 1 }, boolean: -> { 4 } } },
|
7
|
+
other: { Type: { string: -> { 2 }, number: -> { 3 } } },
|
8
|
+
}.freeze
|
9
|
+
|
10
|
+
def after
|
11
|
+
@subject = nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def subject
|
15
|
+
@subject ||= DESCRIBED_CLASS.new
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_loaded_bang
|
19
|
+
DESCRIBED_CLASS.stub(:base_classes, { Type: false }) do
|
20
|
+
refute(DESCRIBED_CLASS.base_classes[:Type])
|
21
|
+
DESCRIBED_CLASS.loaded!(:Type)
|
22
|
+
assert(DESCRIBED_CLASS.base_classes[:Type])
|
23
|
+
end
|
24
|
+
|
25
|
+
DESCRIBED_CLASS.stub(:base_classes, { Type: true }) do
|
26
|
+
assert(DESCRIBED_CLASS.base_classes[:Type])
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_reset_bang
|
31
|
+
checkpoint = registered_double
|
32
|
+
subject.instance_variable_set(:@objects, 1)
|
33
|
+
subject.instance_variable_set(:@pending, 2)
|
34
|
+
subject.instance_variable_set(:@callbacks, 3)
|
35
|
+
subject.instance_variable_set(:@skip_register, 4)
|
36
|
+
subject.instance_variable_set(:@index, 5)
|
37
|
+
subject.instance_variable_set(:@checkpoint, [checkpoint])
|
38
|
+
subject.reset!
|
39
|
+
|
40
|
+
refute_equal(1, subject.instance_variable_get(:@objects))
|
41
|
+
refute_equal(2, subject.instance_variable_get(:@pending))
|
42
|
+
refute_equal(3, subject.instance_variable_get(:@callbacks))
|
43
|
+
refute_equal(4, subject.instance_variable_get(:@skip_register))
|
44
|
+
refute_equal(5, subject.instance_variable_get(:@index))
|
45
|
+
assert_predicate(checkpoint, :registered?)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_use_checkpoint_bang
|
49
|
+
subject.stub(:objects, [1]) do
|
50
|
+
assert_registered do
|
51
|
+
subject.use_checkpoint!
|
52
|
+
assert_equal([1], subject.instance_variable_get(:@checkpoint))
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
reset = false
|
57
|
+
subject.stub(:reset!, -> { reset = true }) do
|
58
|
+
subject.use_checkpoint!
|
59
|
+
assert(reset)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_objects
|
64
|
+
assert_empty(subject.objects)
|
65
|
+
subject.stub_ivar(:@index, { a: { b: { c: -> { 1 } } } }) do
|
66
|
+
assert_empty(subject.objects)
|
67
|
+
end
|
68
|
+
|
69
|
+
item = registered_double
|
70
|
+
subject.stub_ivar(:@index, { a: { b: { c: -> { item } }, d: { e: -> { 1 } } } }) do
|
71
|
+
assert_empty(subject.objects(namespaces: :z))
|
72
|
+
assert_empty(subject.objects(namespaces: :a, base_classes: :z))
|
73
|
+
assert_empty(subject.objects(namespaces: :a, base_classes: :d))
|
74
|
+
|
75
|
+
result = subject.objects
|
76
|
+
refute_includes(result, 1)
|
77
|
+
assert_includes(result, item)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_fetch_bang
|
82
|
+
subject.stub(:fetch, passallthrough) do
|
83
|
+
assert_equal([1, { a: 2, base_class: :Type }], subject.fetch!(1, a: 2))
|
84
|
+
assert_equal([1, { a: 2, base_class: 3 }], subject.fetch!(1, base_class: 3, a: 2))
|
85
|
+
end
|
86
|
+
|
87
|
+
loaded = false
|
88
|
+
subject.stub(:fetch, ->(*x) { x if loaded }) do
|
89
|
+
subject.stub(:base_classes, { Type: true }) do
|
90
|
+
assert_raises(StandardError) { subject.fetch!(1) }
|
91
|
+
end
|
92
|
+
|
93
|
+
subject.stub(:base_classes, { Type: false }) do
|
94
|
+
Rails::GraphQL.stub_const(:Type, double(eager_load!: -> { loaded = true })) do
|
95
|
+
assert_equal([1, { base_class: :Type }], subject.fetch!(1))
|
96
|
+
assert(loaded)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_fetch
|
103
|
+
stub_dig do
|
104
|
+
assert_registered(9) do
|
105
|
+
assert_nil(subject.fetch(:number))
|
106
|
+
assert_nil(subject.fetch(:number, base_class: :Other))
|
107
|
+
|
108
|
+
assert_equal(1, subject.fetch(:string))
|
109
|
+
assert_equal(1, subject.fetch(:string, prevent_register: true))
|
110
|
+
assert_equal(1, subject.fetch(:number, fallback: :string))
|
111
|
+
|
112
|
+
assert_equal(2, subject.fetch(:string, namespaces: :other))
|
113
|
+
assert_equal(3, subject.fetch(:number, namespaces: Set[:other]))
|
114
|
+
|
115
|
+
assert_equal(4, subject.fetch(:boolean))
|
116
|
+
assert_equal(4, subject.fetch(:boolean, namespaces: :other))
|
117
|
+
assert_nil(subject.fetch(:boolean, namespaces: :other, exclusive: true))
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_exist_ask
|
123
|
+
stub_dig do
|
124
|
+
assert(subject.exist?(:string))
|
125
|
+
assert(subject.exist?(:string, namespaces: :other))
|
126
|
+
|
127
|
+
assert(subject.exist?(:boolean, namespaces: Set[:other]))
|
128
|
+
refute(subject.exist?(:boolean, namespaces: :other, exclusive: true))
|
129
|
+
|
130
|
+
refute(subject.exist?(:number))
|
131
|
+
refute(subject.exist?(:number, base_class: :Other))
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_object_exist_ask
|
136
|
+
subject.stub(:exist?, passallthrough) do
|
137
|
+
subject.stub(:find_base_class, passthrough) do
|
138
|
+
object = double(namespaces: :a)
|
139
|
+
xargs = { base_class: object, namespaces: :a, other: 1 }
|
140
|
+
assert_equal([object, xargs], subject.object_exist?(object, other: 1))
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_postpone_registration
|
146
|
+
result = subject.get_reset_ivar(:@pending, []) { postpone_registration(1) }
|
147
|
+
assert_instance_of(String, result[0][1])
|
148
|
+
assert_equal(1, result[0][0])
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_register
|
152
|
+
subject.stub(:find_base_class, passthrough) do
|
153
|
+
subject.stub(:ensure_base_class!, ->(*) { raise }) do
|
154
|
+
assert_raises(StandardError) { subject.register(1) }
|
155
|
+
end
|
156
|
+
|
157
|
+
subject.stub(:ensure_base_class!, passthrough) do
|
158
|
+
added = []
|
159
|
+
fake_add = ->(*args) { added << args }
|
160
|
+
|
161
|
+
subject.stub(:add, fake_add) do
|
162
|
+
object1 = double(gql_name: 'string', to_sym: :string, namespaces: nil)
|
163
|
+
result = subject.register(object1)
|
164
|
+
|
165
|
+
assert_equal(2, added.size)
|
166
|
+
assert_equal(object1, result)
|
167
|
+
assert_equal([:base, object1, :string], added[0][0..-2])
|
168
|
+
assert_equal([:base, object1, 'string'], added[1][0..-2])
|
169
|
+
|
170
|
+
subject.stub(:fetch, passallthrough) do
|
171
|
+
xargs = { base_class: object1, namespaces: :base, exclusive: true }
|
172
|
+
assert_equal([:string, xargs], added[1][-1].call)
|
173
|
+
assert_equal(object1, added[0][-1].call)
|
174
|
+
end
|
175
|
+
|
176
|
+
assert_equal(1, subject.instance_variable_get(:@objects))
|
177
|
+
|
178
|
+
added.clear
|
179
|
+
subject.register(object2 = double(
|
180
|
+
gql_name: 'number',
|
181
|
+
to_sym: :number,
|
182
|
+
namespaces: [:a, :b, :c],
|
183
|
+
aliases: [:num, :n],
|
184
|
+
))
|
185
|
+
|
186
|
+
object2.namespaces.each do |ns|
|
187
|
+
assert_equal([ns, object2, :number], added.shift[0..-2])
|
188
|
+
assert_equal([ns, object2, 'number'], added.shift[0..-2])
|
189
|
+
assert_equal([ns, object2, :num], added.shift[0..-2])
|
190
|
+
assert_equal([ns, object2, :n], added.shift[0..-2])
|
191
|
+
end
|
192
|
+
|
193
|
+
assert_equal(2, subject.instance_variable_get(:@objects))
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
def test_register_alias
|
200
|
+
assert_raises(StandardError) { subject.register_alias(:a) }
|
201
|
+
subject.stub(:ensure_base_class!, ->(*) { raise }) do
|
202
|
+
assert_raises(StandardError) { subject.register_alias(:a, key: :a) }
|
203
|
+
assert_raises(StandardError) { subject.register_alias(:a) { :b } }
|
204
|
+
end
|
205
|
+
|
206
|
+
subject.stub(:ensure_base_class!, passthrough) do
|
207
|
+
added = []
|
208
|
+
fake_add = ->(*args) { added << args }
|
209
|
+
|
210
|
+
subject.stub(:add, fake_add) do
|
211
|
+
subject.register_alias(:a, :string)
|
212
|
+
assert_equal([:base, :Type, :a], added[0][0..-2])
|
213
|
+
|
214
|
+
subject.stub(:fetch, passallthrough) do
|
215
|
+
xargs = { base_class: :Type, namespaces: [:base], exclusive: true }
|
216
|
+
assert_equal([:string, xargs], added[0][-1].call)
|
217
|
+
end
|
218
|
+
|
219
|
+
subject.register_alias(:b) { :c }
|
220
|
+
assert_equal([:base, :Type, :b], added[1][0..-2])
|
221
|
+
assert_equal(:c, added[1][-1].call)
|
222
|
+
|
223
|
+
subject.register_alias(:d, base_class: :Other, namespace: :sample) { :e }
|
224
|
+
assert_equal([:sample, :Other, :d], added[2][0..-2])
|
225
|
+
assert_equal(:e, added[2][-1].call)
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
def test_each_from
|
231
|
+
assert_registered(4) do
|
232
|
+
subject.stub_ivar(:@index, SAMPLE_INDEX) do
|
233
|
+
items = [1, 4]
|
234
|
+
subject.each_from(:base) { |x| assert_equal(items.shift, x) }
|
235
|
+
|
236
|
+
assert_equal([2, 3, 1, 4], subject.each_from(:other).entries)
|
237
|
+
assert_equal([2, 3], subject.each_from(:other, exclusive: true).entries)
|
238
|
+
assert_equal([], subject.each_from(:other, base_class: :Other).entries)
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
def test_after_register
|
244
|
+
subject.stub(:fetch, passallthrough) do
|
245
|
+
result = subject.after_register(:a, &passthrough)
|
246
|
+
xargs = { prevent_register: true, base_class: :Type, namespaces: :base }
|
247
|
+
assert_equal([:a, xargs], result)
|
248
|
+
end
|
249
|
+
|
250
|
+
register = Hash.new { |h, k| h[k] = [] }
|
251
|
+
checker = ->(x) { assert_equal(:called, x) }
|
252
|
+
|
253
|
+
subject.stub(:fetch, ->(*) {}) do
|
254
|
+
subject.stub(:callbacks, register) do
|
255
|
+
subject.after_register(:a, &checker)
|
256
|
+
subject.after_register(:a, base_class: :Other, &checker)
|
257
|
+
subject.after_register(:a, namespaces: :other, &checker)
|
258
|
+
subject.after_register(:a, namespaces: Set[:other], &checker)
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
assert_equal(1, register.size)
|
263
|
+
assert_equal(4, register[:a].size)
|
264
|
+
|
265
|
+
[
|
266
|
+
[:base, :Type],
|
267
|
+
[:base, :Other],
|
268
|
+
[:other, :Type],
|
269
|
+
[:other, :Type],
|
270
|
+
].each_with_index do |(ns, bc), idx|
|
271
|
+
assert_nil(register[:a][idx].call(:x, bc, :non_called))
|
272
|
+
assert_nil(register[:a][idx].call(ns, :y, :non_called))
|
273
|
+
assert_equal(idx, register[:a][idx].call(ns, bc, :called))
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
def test_inspect
|
278
|
+
assert_equal(<<~INFO.chomp, subject.inspect)
|
279
|
+
#<Rails::GraphQL::TypeMap [index] @namespaces=0 @base_classes=3 @objects=0 @pending=0>
|
280
|
+
INFO
|
281
|
+
end
|
282
|
+
|
283
|
+
def test_add
|
284
|
+
item = {}
|
285
|
+
last = []
|
286
|
+
|
287
|
+
subject.stub_ivar(:@index, ->(*args) { last = args; item }.curry(2)) do
|
288
|
+
subject.send(:add, 1, 2, 3, 4)
|
289
|
+
assert_equal({ 3 => 4 }, item)
|
290
|
+
assert_equal([1, 2], last)
|
291
|
+
|
292
|
+
callbacks = { 7 => [
|
293
|
+
->(*args) { assert_equal([5, 6, :a], args); 0 },
|
294
|
+
->(*args) { assert_equal([5, 6, :a], args); nil },
|
295
|
+
->(*args) { assert_equal([5, 6, :a], args); 2 },
|
296
|
+
]}
|
297
|
+
|
298
|
+
subject.stub(:callbacks, callbacks) do
|
299
|
+
subject.send(:add, 5, 6, 7, -> { :a })
|
300
|
+
assert_equal(:a, item.values.last.call)
|
301
|
+
assert_equal(7, item.keys.last)
|
302
|
+
assert_equal([5, 6], last)
|
303
|
+
|
304
|
+
assert_equal(1, callbacks.size)
|
305
|
+
assert_equal(1, callbacks[7].size)
|
306
|
+
assert_nil(callbacks[7][0].call(5, 6, :a))
|
307
|
+
end
|
308
|
+
|
309
|
+
callbacks = { 0 => [->(*args) { assert_equal([8, 9, :b], args); 0 }]}
|
310
|
+
subject.stub(:callbacks, callbacks) do
|
311
|
+
subject.send(:add, 8, 9, 0, -> { :b })
|
312
|
+
assert_equal(:b, item.values.last.call)
|
313
|
+
assert_equal(0, item.keys.last)
|
314
|
+
assert_equal([8, 9], last)
|
315
|
+
|
316
|
+
assert_equal(0, callbacks.size)
|
317
|
+
end
|
318
|
+
end
|
319
|
+
end
|
320
|
+
|
321
|
+
def test_register_pending_bang
|
322
|
+
registered = false
|
323
|
+
object = double(register!: -> { -> { registered = true } }, registered?: false)
|
324
|
+
skipped = double(registered?: false)
|
325
|
+
other = double(registered?: true)
|
326
|
+
|
327
|
+
subject.stub(:skip_register, [skipped]) do
|
328
|
+
subject.stub_ivar(:@pending, [[skipped, :a], [object, :b], [other, :c]]) do
|
329
|
+
subject.send(:register_pending!)
|
330
|
+
assert(registered)
|
331
|
+
|
332
|
+
items = subject.instance_variable_get(:@pending)
|
333
|
+
assert_equal(1, items.size)
|
334
|
+
assert_equal([skipped, :a], items[0])
|
335
|
+
end
|
336
|
+
end
|
337
|
+
end
|
338
|
+
|
339
|
+
def test_find_base_class
|
340
|
+
assert_equal(:a, subject.send(:find_base_class, double(base_type_class: :a)))
|
341
|
+
assert_equal(:String, subject.send(:find_base_class, String))
|
342
|
+
|
343
|
+
klass = Rails::GraphQL::Type::Scalar::StringScalar
|
344
|
+
assert_equal(:Type, subject.send(:find_base_class, klass))
|
345
|
+
end
|
346
|
+
|
347
|
+
protected
|
348
|
+
|
349
|
+
def assert_registered(times = 1, &block)
|
350
|
+
counter = 0
|
351
|
+
subject.stub(:register_pending!, -> { counter += 1 }, &block)
|
352
|
+
assert_equal(times, counter)
|
353
|
+
end
|
354
|
+
|
355
|
+
def stub_dig(index = SAMPLE_INDEX, &block)
|
356
|
+
subject.stub(:dig, index.method(:dig), &block)
|
357
|
+
end
|
358
|
+
|
359
|
+
def registered_double
|
360
|
+
double(register!: -> { define_singleton_method(:registered?) { true } })
|
361
|
+
end
|
362
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'config'
|
2
|
+
|
3
|
+
class GraphQL_TypeTest < GraphQL::TestCase
|
4
|
+
class DESCRIBED_CLASS < Rails::GraphQL::Type; end
|
5
|
+
|
6
|
+
def test_inherited
|
7
|
+
assert_equal(DESCRIBED_CLASS, DESCRIBED_CLASS.base_type)
|
8
|
+
assert_predicate(DESCRIBED_CLASS, :described_class?)
|
9
|
+
assert_predicate(DESCRIBED_CLASS, :spec_object?)
|
10
|
+
assert_predicate(DESCRIBED_CLASS, :base_object?)
|
11
|
+
assert_predicate(DESCRIBED_CLASS, :abstract?)
|
12
|
+
|
13
|
+
other_class = unmapped_class(DESCRIBED_CLASS)
|
14
|
+
assert_equal(DESCRIBED_CLASS, other_class.base_type)
|
15
|
+
assert_predicate(other_class, :described_class?)
|
16
|
+
refute_predicate(other_class, :spec_object?)
|
17
|
+
refute_predicate(other_class, :base_object?)
|
18
|
+
refute_predicate(other_class, :abstract?)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_equivalence
|
22
|
+
assert_operator(Rails::GraphQL::Type, :=~, DESCRIBED_CLASS)
|
23
|
+
assert_operator(Rails::GraphQL::Type, :=~, DESCRIBED_CLASS.new)
|
24
|
+
assert_operator(DESCRIBED_CLASS, :=~, DESCRIBED_CLASS.new)
|
25
|
+
|
26
|
+
refute_operator(Rails::GraphQL::Type::Scalar, :=~, DESCRIBED_CLASS)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_kind
|
30
|
+
assert_equal(:described_class, DESCRIBED_CLASS.kind)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_kind_enum
|
34
|
+
assert_equal('DESCRIBED_CLASS', DESCRIBED_CLASS.kind_enum)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_decorate
|
38
|
+
assert_equal(1, DESCRIBED_CLASS.decorate(1))
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_gql_resolver_ask
|
42
|
+
DESCRIBED_CLASS.stub(:base_object?, false) do
|
43
|
+
refute(DESCRIBED_CLASS.gql_resolver?(:calculate))
|
44
|
+
DESCRIBED_CLASS.send(:define_method, :calculate) {}
|
45
|
+
|
46
|
+
assert(DESCRIBED_CLASS.gql_resolver?(:calculate))
|
47
|
+
|
48
|
+
other_class = unmapped_class(DESCRIBED_CLASS)
|
49
|
+
assert(other_class.gql_resolver?(:calculate))
|
50
|
+
|
51
|
+
other_class.stub(:base_object?, true) do
|
52
|
+
refute(other_class.gql_resolver?(:calculate))
|
53
|
+
end
|
54
|
+
|
55
|
+
DESCRIBED_CLASS.send(:undef_method, :calculate)
|
56
|
+
refute(DESCRIBED_CLASS.gql_resolver?(:calculate))
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_kind_ask
|
61
|
+
assert_respond_to(DESCRIBED_CLASS, :scalar?)
|
62
|
+
assert_respond_to(DESCRIBED_CLASS, :object?)
|
63
|
+
assert_respond_to(DESCRIBED_CLASS, :interface?)
|
64
|
+
assert_respond_to(DESCRIBED_CLASS, :union?)
|
65
|
+
assert_respond_to(DESCRIBED_CLASS, :enum?)
|
66
|
+
assert_respond_to(DESCRIBED_CLASS, :input?)
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'config'
|
2
|
+
|
3
|
+
class GraphQLTest < GraphQL::TestCase
|
4
|
+
DESCRIBED_CLASS = Rails::GraphQL
|
5
|
+
|
6
|
+
def test_type_map
|
7
|
+
DESCRIBED_CLASS.stub_cvar(:@@type_map, 1) do
|
8
|
+
assert_equal(1, DESCRIBED_CLASS.type_map)
|
9
|
+
end
|
10
|
+
|
11
|
+
assert_instance_of(Rails::GraphQL::TypeMap, DESCRIBED_CLASS.type_map)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_ar_adapter_key
|
15
|
+
DESCRIBED_CLASS.stub(:config, double(ar_adapters: { 'a' => 1 })) do
|
16
|
+
assert_equal(1, DESCRIBED_CLASS.ar_adapter_key('a'))
|
17
|
+
assert_nil(DESCRIBED_CLASS.ar_adapter_key('b'))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_enable_ar_adapter
|
22
|
+
DESCRIBED_CLASS.stub_cvar(:@@loaded_adapters, Set[1]) do
|
23
|
+
DESCRIBED_CLASS.enable_ar_adapter(1)
|
24
|
+
pass 'Accepted any value set on loaded_adapters'
|
25
|
+
end
|
26
|
+
|
27
|
+
value = DESCRIBED_CLASS.get_reset_cvar(:@@loaded_adapters) do
|
28
|
+
DESCRIBED_CLASS.enable_ar_adapter('PostgreSQL')
|
29
|
+
end
|
30
|
+
|
31
|
+
assert_includes(value, 'PostgreSQL')
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_reload_ar_adapters_bang
|
35
|
+
result = []
|
36
|
+
DESCRIBED_CLASS.stub(:enable_ar_adapter, ->(x) { result << x }) do
|
37
|
+
DESCRIBED_CLASS.stub_cvar(:@@loaded_adapters, Set[]) do
|
38
|
+
DESCRIBED_CLASS.reload_ar_adapters!
|
39
|
+
assert_empty(result)
|
40
|
+
end
|
41
|
+
|
42
|
+
DESCRIBED_CLASS.stub_cvar(:@@loaded_adapters, Set[1]) do
|
43
|
+
DESCRIBED_CLASS.reload_ar_adapters!
|
44
|
+
assert_equal([1], result)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_to_gql
|
50
|
+
DESCRIBED_CLASS.stub_const(:ToGQL, double(compile: passallthrough)) do
|
51
|
+
assert_equal([1], DESCRIBED_CLASS.to_gql(1))
|
52
|
+
assert_equal([1, { a: 1 }], DESCRIBED_CLASS.to_gql(1, a: 1))
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|