graphql_client 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +5 -0
- data/.rubocop-http---shopify-github-io-ruby-style-guide-rubocop-yml +1133 -0
- data/.rubocop.yml +24 -0
- data/.travis.yml +6 -0
- data/CHANGELOG.md +3 -0
- data/CODE_OF_CONDUCT.md +45 -0
- data/CONTRIBUTING.md +28 -0
- data/CONTRIBUTING_DEVELOPER_CERTIFICATE_OF_ORIGIN.txt +37 -0
- data/Gemfile +8 -0
- data/LICENSE.md +21 -0
- data/README.md +96 -0
- data/Rakefile +13 -0
- data/bin/graphql-client +79 -0
- data/bin/rake +17 -0
- data/circle.yml +3 -0
- data/dev.yml +7 -0
- data/graphql_ruby_client.gemspec +26 -0
- data/lib/graphql_client.rb +46 -0
- data/lib/graphql_client/adapters/http_adapter.rb +72 -0
- data/lib/graphql_client/base.rb +53 -0
- data/lib/graphql_client/config.rb +42 -0
- data/lib/graphql_client/deserialization.rb +36 -0
- data/lib/graphql_client/error.rb +22 -0
- data/lib/graphql_client/graph_connection.rb +21 -0
- data/lib/graphql_client/graph_node.rb +24 -0
- data/lib/graphql_client/graph_object.rb +56 -0
- data/lib/graphql_client/introspection_query.rb +80 -0
- data/lib/graphql_client/query/add_inline_fragment.rb +42 -0
- data/lib/graphql_client/query/argument.rb +30 -0
- data/lib/graphql_client/query/document.rb +86 -0
- data/lib/graphql_client/query/field.rb +91 -0
- data/lib/graphql_client/query/fragment.rb +41 -0
- data/lib/graphql_client/query/has_selection_set.rb +72 -0
- data/lib/graphql_client/query/inline_fragment.rb +35 -0
- data/lib/graphql_client/query/mutation_document.rb +20 -0
- data/lib/graphql_client/query/operation.rb +46 -0
- data/lib/graphql_client/query/operations/mutation_operation.rb +17 -0
- data/lib/graphql_client/query/operations/query_operation.rb +17 -0
- data/lib/graphql_client/query/query_document.rb +20 -0
- data/lib/graphql_client/query/selection_set.rb +53 -0
- data/lib/graphql_client/response.rb +21 -0
- data/lib/graphql_client/response_connection.rb +18 -0
- data/lib/graphql_client/response_object.rb +32 -0
- data/lib/graphql_client/schema_patches.rb +17 -0
- data/lib/graphql_client/version.rb +7 -0
- data/shipit.rubygems.yml +1 -0
- data/shipit.yml +4 -0
- data/test/graphql_client/adapters/http_adapter_test.rb +111 -0
- data/test/graphql_client/config_test.rb +68 -0
- data/test/graphql_client/graph_connection_test.rb +8 -0
- data/test/graphql_client/graph_node_test.rb +8 -0
- data/test/graphql_client/graph_object_query_transforming_test.rb +156 -0
- data/test/graphql_client/graph_object_test.rb +142 -0
- data/test/graphql_client/graphql_client_test.rb +41 -0
- data/test/graphql_client/http_client_test.rb +52 -0
- data/test/graphql_client/query/add_inline_fragment_test.rb +57 -0
- data/test/graphql_client/query/arguments_test.rb +89 -0
- data/test/graphql_client/query/document_test.rb +246 -0
- data/test/graphql_client/query/field_test.rb +163 -0
- data/test/graphql_client/query/fragment_test.rb +45 -0
- data/test/graphql_client/query/inline_fragment_test.rb +37 -0
- data/test/graphql_client/query/mutation_document_test.rb +30 -0
- data/test/graphql_client/query/mutation_operation_test.rb +89 -0
- data/test/graphql_client/query/query_document_test.rb +30 -0
- data/test/graphql_client/query/query_operation_test.rb +262 -0
- data/test/graphql_client/query/selection_set_test.rb +116 -0
- data/test/graphql_client/response_connection_test.rb +50 -0
- data/test/graphql_client/response_object_test.rb +109 -0
- data/test/graphql_client/response_test.rb +67 -0
- data/test/support/fixtures/schema.json +13710 -0
- data/test/test_helper.rb +37 -0
- metadata +227 -0
@@ -0,0 +1,142 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module GraphQL
|
4
|
+
module Client
|
5
|
+
class GraphObjectTest < Minitest::Test
|
6
|
+
def setup
|
7
|
+
@schema = GraphQLSchema.new(schema_fixture('schema.json'))
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_builds_graph_objects_from_hashes
|
11
|
+
query = Query::QueryDocument.new(@schema)
|
12
|
+
shop = query.add_field('shop', as: 'myshop')
|
13
|
+
shop.add_field('name')
|
14
|
+
|
15
|
+
data = {
|
16
|
+
'myshop' => {
|
17
|
+
'name' => 'My Shop',
|
18
|
+
}
|
19
|
+
}
|
20
|
+
|
21
|
+
result = GraphObject.new(data: data, query: query)
|
22
|
+
myshop = result.myshop
|
23
|
+
|
24
|
+
assert_equal data.fetch('myshop'), myshop.data
|
25
|
+
assert_equal shop, myshop.query
|
26
|
+
assert_equal result, myshop.parent
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_builds_graph_objects_from_arrays
|
30
|
+
query = Query::QueryDocument.new(@schema)
|
31
|
+
shop = query.add_field('shop')
|
32
|
+
product = shop.add_field('productByHandle', handle: 'handle')
|
33
|
+
product.add_field('tags')
|
34
|
+
|
35
|
+
data = {
|
36
|
+
'shop' => {
|
37
|
+
'productByHandle' => {
|
38
|
+
'tags' => [
|
39
|
+
'tag1',
|
40
|
+
'tag2',
|
41
|
+
]
|
42
|
+
}
|
43
|
+
}
|
44
|
+
}
|
45
|
+
|
46
|
+
result = GraphObject.new(data: data, query: query)
|
47
|
+
|
48
|
+
assert_equal ['tag1', 'tag2'], result.shop.product_by_handle.tags
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_instantiates_a_graph_connection_for_connection_fields
|
52
|
+
query = Query::QueryDocument.new(@schema)
|
53
|
+
shop = query.add_field('shop')
|
54
|
+
|
55
|
+
data = {
|
56
|
+
'shop' => {}
|
57
|
+
}
|
58
|
+
|
59
|
+
shop.stub(:connection?, true) do
|
60
|
+
result = GraphObject.new(data: data, query: query)
|
61
|
+
assert_instance_of GraphConnection, result.shop
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_instantiates_a_graph_node_for_node_types
|
66
|
+
query = Query::QueryDocument.new(@schema)
|
67
|
+
shop = query.add_field('shop')
|
68
|
+
|
69
|
+
data = {
|
70
|
+
'shop' => {}
|
71
|
+
}
|
72
|
+
|
73
|
+
shop.stub(:node?, true) do
|
74
|
+
result = GraphObject.new(data: data, query: query)
|
75
|
+
assert_instance_of GraphNode, result.shop
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_defines_methods_from_data
|
80
|
+
query = Query::QueryDocument.new(@schema)
|
81
|
+
shop = query.add_field('shop', as: 'myshop')
|
82
|
+
shop.add_fields('name', 'termsOfService')
|
83
|
+
|
84
|
+
data = {
|
85
|
+
'myshop' => {
|
86
|
+
'name' => 'My Shop',
|
87
|
+
'termsOfService' => false,
|
88
|
+
}
|
89
|
+
}
|
90
|
+
|
91
|
+
result = GraphObject.new(data: data, query: query)
|
92
|
+
myshop = result.myshop
|
93
|
+
|
94
|
+
assert_equal 'My Shop', myshop.name
|
95
|
+
assert_equal false, myshop.terms_of_service
|
96
|
+
assert_equal false, myshop.termsOfService
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_defines_instance_variable_during_method_access
|
100
|
+
query = Query::QueryDocument.new(@schema)
|
101
|
+
shop = query.add_field('shop')
|
102
|
+
shop.add_field('name')
|
103
|
+
|
104
|
+
data = {
|
105
|
+
'shop' => {
|
106
|
+
'name' => 'My Shop',
|
107
|
+
}
|
108
|
+
}
|
109
|
+
|
110
|
+
result = GraphObject.new(data: data, query: query)
|
111
|
+
refute result.instance_variable_defined?("@shop")
|
112
|
+
|
113
|
+
result.shop
|
114
|
+
assert result.instance_variable_defined?("@shop")
|
115
|
+
assert_equal result.shop, result.instance_variable_get("@shop")
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_build_minimal_query_creates_a_new_query_operation_for_the_root_object
|
119
|
+
query = Query::QueryDocument.new(@schema) do |root|
|
120
|
+
root.add_field('shop') do |non_node|
|
121
|
+
non_node.add_field('name')
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
data = {
|
126
|
+
'shop' => {
|
127
|
+
'name' => 'Foo'
|
128
|
+
}
|
129
|
+
}
|
130
|
+
|
131
|
+
graph = GraphObject.new(data: data, query: query)
|
132
|
+
root = nil
|
133
|
+
|
134
|
+
graph.build_minimal_query do |context|
|
135
|
+
root = context
|
136
|
+
end
|
137
|
+
|
138
|
+
assert_instance_of Query::QueryOperation, root
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module GraphQL
|
4
|
+
module Client
|
5
|
+
class ClientTest < Minitest::Test
|
6
|
+
TestAdapter = Struct.new(:config) do
|
7
|
+
def request(_query_string)
|
8
|
+
Response.new('{ "data": { } }')
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_dump_schema_writes_a_schema_file_from_introspection_query
|
13
|
+
adapter = TestAdapter.new(Config.new)
|
14
|
+
|
15
|
+
Tempfile.create('temp_schema.json') do |f|
|
16
|
+
Client.dump_schema(f, adapter: adapter)
|
17
|
+
|
18
|
+
assert_equal(JSON.pretty_generate(data: {}), File.read(f))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_new_instantiates_base
|
23
|
+
client = Client.new(schema_fixture('schema.json'))
|
24
|
+
|
25
|
+
assert_instance_of Base, client
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_new_accepts_a_block_for_httpclient
|
29
|
+
url = URI('http://example.com')
|
30
|
+
|
31
|
+
client = Client.new(schema_fixture('schema.json')) do
|
32
|
+
configure do |c|
|
33
|
+
c.url = url
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
assert_equal url, client.config.url
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module GraphQL
|
4
|
+
module Client
|
5
|
+
class BaseTest < Minitest::Test
|
6
|
+
def test_configure_yields_the_config
|
7
|
+
client = Base.new(schema_fixture('schema.json'))
|
8
|
+
|
9
|
+
client.configure do |c|
|
10
|
+
assert_equal c, client.config
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_query_calls_adapter_request_with_query_builder_instance_and_creates_a_graph_object
|
15
|
+
config = Config.new(url: 'http://example.com')
|
16
|
+
|
17
|
+
query = Minitest::Mock.new
|
18
|
+
query.expect(:to_query, 'query shopQuery { shop }')
|
19
|
+
|
20
|
+
adapter = Minitest::Mock.new
|
21
|
+
adapter.expect(:request, Response.new('{}'), [
|
22
|
+
'query shopQuery { shop }',
|
23
|
+
operation_name: 'shopQuery',
|
24
|
+
variables: {}
|
25
|
+
])
|
26
|
+
|
27
|
+
mock = Minitest::Mock.new
|
28
|
+
mock.expect(:call, nil, [data: nil, query: query])
|
29
|
+
|
30
|
+
GraphObject.stub(:new, mock) do
|
31
|
+
client = Base.new(schema_fixture('schema.json'), config: config, adapter: adapter)
|
32
|
+
client.query(query, operation_name: 'shopQuery')
|
33
|
+
|
34
|
+
mock.verify
|
35
|
+
query.verify
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_raw_query_calls_adapter_request_with_query_string
|
40
|
+
config = Config.new(url: 'http://example.com')
|
41
|
+
|
42
|
+
adapter = Minitest::Mock.new
|
43
|
+
adapter.expect(:request, Response.new('{}'), ['query { shop }', operation_name: nil, variables: {}])
|
44
|
+
|
45
|
+
client = Base.new(schema_fixture('schema.json'), config: config, adapter: adapter)
|
46
|
+
client.raw_query('query { shop }')
|
47
|
+
|
48
|
+
adapter.verify
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module GraphQL
|
4
|
+
module Client
|
5
|
+
module Query
|
6
|
+
class AddInlineFragmentTest < Minitest::Test
|
7
|
+
def setup
|
8
|
+
@schema = GraphQLSchema.new(schema_fixture('schema.json'))
|
9
|
+
@document = Document.new(@schema)
|
10
|
+
|
11
|
+
shop = @schema.query_root.field('shop')
|
12
|
+
field_defn = schema_type(shop.type).field('productByHandle')
|
13
|
+
@field = Field.new(field_defn, document: @document, arguments: { handle: 'test' })
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_add_inline_fragment_yields_inline_fragment
|
17
|
+
inline_fragment_object = nil
|
18
|
+
|
19
|
+
inline_fragment = @field.add_inline_fragment('Product') do |f|
|
20
|
+
inline_fragment_object = f
|
21
|
+
end
|
22
|
+
|
23
|
+
assert_equal inline_fragment_object, inline_fragment
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_add_inline_fragment_creates_inline_fragment_with_explicit_type
|
27
|
+
inline_fragment = @field.add_inline_fragment('Product')
|
28
|
+
|
29
|
+
assert_equal @schema.type('Product'), inline_fragment.type
|
30
|
+
assert_equal [inline_fragment], @field.selection_set.inline_fragments
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_add_inline_fragment_creates_inline_fragment_with_explicit_interface_type
|
34
|
+
inline_fragment = @field.add_inline_fragment('Node')
|
35
|
+
|
36
|
+
assert_equal @schema.type('Node'), inline_fragment.type
|
37
|
+
assert_equal [inline_fragment], @field.selection_set.inline_fragments
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_add_inline_fragment_creates_inline_fragment_with_implicit_type
|
41
|
+
inline_fragment = @field.add_inline_fragment
|
42
|
+
|
43
|
+
assert_equal @schema.type('Product'), inline_fragment.type
|
44
|
+
assert_equal [inline_fragment], @field.selection_set.inline_fragments
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_add_inline_fragment_raises_exception_for_invalid_target_type
|
48
|
+
assert_raises AddInlineFragment::INVALID_FRAGMENT_TARGET do |e|
|
49
|
+
@field.add_inline_fragment('Image')
|
50
|
+
|
51
|
+
assert_equal "invalid target type 'Image' for fragment of type 'Product'", e.message
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module GraphQL
|
4
|
+
module Client
|
5
|
+
module Query
|
6
|
+
class ArgumentTest < Minitest::Test
|
7
|
+
def test_to_query_formats_arguments
|
8
|
+
arguments = Argument.new(2)
|
9
|
+
|
10
|
+
assert_equal '2', arguments.to_query
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_to_query_quotes_string_values
|
14
|
+
arguments = Argument.new('2')
|
15
|
+
|
16
|
+
assert_equal '"2"', arguments.to_query
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_to_query_boolean
|
20
|
+
arguments = Argument.new(true)
|
21
|
+
|
22
|
+
assert_equal 'true', arguments.to_query
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_to_query_array
|
26
|
+
arguments = Argument.new([1, 'two'])
|
27
|
+
|
28
|
+
assert_equal '[1, "two"]', arguments.to_query
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_to_query_hash
|
32
|
+
arguments = Argument.new(name: 'Foo', ids: [1, 2], variable: '$variableName')
|
33
|
+
|
34
|
+
assert_equal '{ name: "Foo", ids: [1,2], variable: $variableName }', arguments.to_query
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_to_query_float
|
38
|
+
arguments = Argument.new(4.7e-24)
|
39
|
+
|
40
|
+
assert_equal '4.7e-24', arguments.to_query
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_to_query_escapes_control_characters
|
44
|
+
arguments = Argument.new("control\ncharacter")
|
45
|
+
|
46
|
+
assert_equal '"control\\ncharacter"', arguments.to_query
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_to_query_unicode
|
50
|
+
arguments = Argument.new('☀︎🏆 ¶')
|
51
|
+
|
52
|
+
assert_equal '"☀︎🏆 ¶"', arguments.to_query
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_to_query_escaped_unicode
|
56
|
+
arguments = Argument.new("\u0012")
|
57
|
+
|
58
|
+
assert_equal '"\\u0012"', arguments.to_query
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_to_query_escapes_characters
|
62
|
+
arguments = Argument.new("foo\"bar")
|
63
|
+
|
64
|
+
assert_equal '"foo\"bar"', arguments.to_query
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_to_query_passes_through_variables
|
68
|
+
arguments = Argument.new('$variableName')
|
69
|
+
|
70
|
+
assert_equal '$variableName', arguments.to_query
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_argument_instances_are_equal_for_same_value
|
74
|
+
argument = Argument.new('foo')
|
75
|
+
other_argument = Argument.new('foo')
|
76
|
+
|
77
|
+
assert_equal argument, other_argument
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_argument_instances_are_not_equal_for_different_values
|
81
|
+
argument = Argument.new('foo')
|
82
|
+
other_argument = Argument.new('var')
|
83
|
+
|
84
|
+
refute_equal argument, other_argument
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,246 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module GraphQL
|
4
|
+
module Client
|
5
|
+
module Query
|
6
|
+
class DocumentTest < Minitest::Test
|
7
|
+
def setup
|
8
|
+
@schema = GraphQLSchema.new(schema_fixture('schema.json'))
|
9
|
+
@graphql_schema = GraphQL::Schema::Loader.load(schema_fixture('schema.json'))
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_initialize_yields_self
|
13
|
+
document_object = nil
|
14
|
+
|
15
|
+
document = Document.new(@schema) do |d|
|
16
|
+
document_object = d
|
17
|
+
end
|
18
|
+
|
19
|
+
assert_equal document_object, document
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_add_mutation_creates_a_mutation_operation
|
23
|
+
document = Document.new(@schema)
|
24
|
+
mutation = document.add_mutation('createUser')
|
25
|
+
|
26
|
+
assert_instance_of MutationOperation, mutation
|
27
|
+
assert_equal({ 'createUser' => mutation }, document.operations)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_add_query_creates_a_query_operation
|
31
|
+
document = Document.new(@schema)
|
32
|
+
query = document.add_query('getUser')
|
33
|
+
|
34
|
+
assert_instance_of QueryOperation, query
|
35
|
+
assert_equal({ 'getUser' => query }, document.operations)
|
36
|
+
assert_equal 1, document.operations.size
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_add_operation_sets_default_name
|
40
|
+
document = Document.new(@schema)
|
41
|
+
query = document.add_query
|
42
|
+
|
43
|
+
assert_instance_of QueryOperation, query
|
44
|
+
assert_equal({ 'default' => query }, document.operations)
|
45
|
+
assert_equal 1, document.operations.size
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_add_operation_yields_block
|
49
|
+
document = Document.new(@schema)
|
50
|
+
query_object = nil
|
51
|
+
|
52
|
+
query = document.add_query do |q|
|
53
|
+
query_object = q
|
54
|
+
end
|
55
|
+
|
56
|
+
assert_equal query_object, query
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_add_operation_supports_multiple_unique_operations
|
60
|
+
document = Document.new(@schema)
|
61
|
+
document.add_query('getUser')
|
62
|
+
document.add_query('getPosts')
|
63
|
+
|
64
|
+
assert_equal 2, document.operations.size
|
65
|
+
assert_equal %w(getUser getPosts), document.operations.keys
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_add_operation_enforces_unique_names
|
69
|
+
document = Document.new(@schema)
|
70
|
+
document.add_query('getUser')
|
71
|
+
|
72
|
+
assert_raises Document::DUPLICATE_OPERATION_NAME do
|
73
|
+
document.add_query('getUser')
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_add_operation_requires_a_document_with_multiple_operations_to_all_be_named
|
78
|
+
document = Document.new(@schema)
|
79
|
+
document.add_query
|
80
|
+
|
81
|
+
assert_raises Document::INVALID_DOCUMENT do
|
82
|
+
document.add_query('getUser')
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_define_fragment_creates_a_fragment
|
87
|
+
document = Document.new(@schema)
|
88
|
+
|
89
|
+
fragment = document.define_fragment('imageFields', on: 'Image')
|
90
|
+
|
91
|
+
assert_equal 'imageFields', fragment.name
|
92
|
+
assert_equal @schema.type('Image'), fragment.type
|
93
|
+
assert_equal document, fragment.document
|
94
|
+
assert_equal({ 'imageFields' => fragment }, document.fragments)
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_define_fragment_yields_fragment
|
98
|
+
document = Document.new(@schema)
|
99
|
+
fragment_object = nil
|
100
|
+
|
101
|
+
fragment = document.define_fragment('imageFields', on: 'Image') do |f|
|
102
|
+
fragment_object = f
|
103
|
+
end
|
104
|
+
|
105
|
+
assert_equal fragment_object, fragment
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_define_fragment_raises_exception_for_invalid_targets
|
109
|
+
document = Document.new(@schema)
|
110
|
+
|
111
|
+
assert_raises Document::INVALID_FRAGMENT_TARGET do
|
112
|
+
document.define_fragment('imageFields', on: 'String')
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_fragment_definitions_is_the_fragments_definition_string
|
117
|
+
document = Document.new(@schema)
|
118
|
+
|
119
|
+
document.define_fragment('imageFields', on: 'Image') do |f|
|
120
|
+
f.add_field('src')
|
121
|
+
end
|
122
|
+
|
123
|
+
document.define_fragment('shopName', on: 'Shop') do |f|
|
124
|
+
f.add_field('name')
|
125
|
+
end
|
126
|
+
|
127
|
+
fragment_string = <<~QUERY
|
128
|
+
fragment imageFields on Image {
|
129
|
+
src
|
130
|
+
}
|
131
|
+
|
132
|
+
fragment shopName on Shop {
|
133
|
+
name
|
134
|
+
}
|
135
|
+
QUERY
|
136
|
+
|
137
|
+
assert_equal fragment_string, document.fragment_definitions
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_to_query_joins_all_operations
|
141
|
+
document = Document.new(@schema) do |d|
|
142
|
+
d.add_query('shopQuery') do |q|
|
143
|
+
q.add_field('shop') do |shop|
|
144
|
+
shop.add_field('name')
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
d.add_mutation('customers') do |c|
|
149
|
+
c.add_field('customerCreate', input: { email: 'email', password: 'password' }) do |create|
|
150
|
+
create.add_field('customer') do |customer|
|
151
|
+
customer.add_field('email')
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
query_string = <<~QUERY
|
158
|
+
query shopQuery {
|
159
|
+
shop {
|
160
|
+
name
|
161
|
+
}
|
162
|
+
}
|
163
|
+
|
164
|
+
mutation customers {
|
165
|
+
customerCreate(input: { email: \"email\", password: \"password\" }) {
|
166
|
+
customer {
|
167
|
+
email
|
168
|
+
}
|
169
|
+
}
|
170
|
+
}
|
171
|
+
QUERY
|
172
|
+
|
173
|
+
assert_equal query_string, document.to_query
|
174
|
+
assert_valid_query query_string, @graphql_schema, operation_name: 'shopQuery'
|
175
|
+
assert_valid_query query_string, @graphql_schema, operation_name: 'customers'
|
176
|
+
end
|
177
|
+
|
178
|
+
def test_to_query_includes_fragment_definitions
|
179
|
+
document = Document.new(@schema) do |d|
|
180
|
+
d.define_fragment('imageFields', on: 'Image') do |f|
|
181
|
+
f.add_field('src')
|
182
|
+
end
|
183
|
+
|
184
|
+
d.add_query('getShop') do |q|
|
185
|
+
q.add_field('shop') do |shop|
|
186
|
+
shop.add_field('name')
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
d.add_query('getProductImages') do |q|
|
191
|
+
q.add_field('shop') do |shop|
|
192
|
+
shop.add_field('productByHandle', handle: 'test') do |product|
|
193
|
+
product.add_connection('images', first: 10) do |connection|
|
194
|
+
connection.add_fragment('imageFields')
|
195
|
+
|
196
|
+
connection.add_inline_fragment do |f|
|
197
|
+
f.add_field('altText')
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
query_string = <<~QUERY
|
206
|
+
fragment imageFields on Image {
|
207
|
+
src
|
208
|
+
}
|
209
|
+
|
210
|
+
query getShop {
|
211
|
+
shop {
|
212
|
+
name
|
213
|
+
}
|
214
|
+
}
|
215
|
+
|
216
|
+
query getProductImages {
|
217
|
+
shop {
|
218
|
+
productByHandle(handle: \"test\") {
|
219
|
+
id
|
220
|
+
images(first: 10) {
|
221
|
+
edges {
|
222
|
+
cursor
|
223
|
+
node {
|
224
|
+
...imageFields
|
225
|
+
... on Image {
|
226
|
+
altText
|
227
|
+
}
|
228
|
+
}
|
229
|
+
}
|
230
|
+
pageInfo {
|
231
|
+
hasPreviousPage
|
232
|
+
hasNextPage
|
233
|
+
}
|
234
|
+
}
|
235
|
+
}
|
236
|
+
}
|
237
|
+
}
|
238
|
+
QUERY
|
239
|
+
|
240
|
+
assert_equal query_string, document.to_query
|
241
|
+
assert_valid_query query_string, @graphql_schema, operation_name: 'getShop'
|
242
|
+
end
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|