graphql-relay 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae595ba777a0d485536c949f06bc7b0741a97267
|
4
|
+
data.tar.gz: 92fb6814d222d1965d9b31bc8c8887a091b19524
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8797f9ab05278a90c3b9ac4a8f6997b2b466a6cc05469ae5833dcff23bdc12570fda500ec7a66e5a05519d101573bdf0e44be809852b495b16d0070abac0a471
|
7
|
+
data.tar.gz: 595265221e8f5a9e44b87c1145f4ebe4af9e72a3fc0aed0ba0c72ce6c515f61bed75b7a800c1010f9d316752542f0fe200452853256b758cc7256139261b5653
|
data/lib/graphql/relay.rb
CHANGED
@@ -18,13 +18,14 @@ module GraphQL
|
|
18
18
|
CONNECTION_IMPLEMENTATIONS = {}
|
19
19
|
|
20
20
|
# Create a connection which exposes edges of this type
|
21
|
-
def self.create_type(wrapped_type)
|
21
|
+
def self.create_type(wrapped_type, &block)
|
22
22
|
edge_type = Edge.create_type(wrapped_type)
|
23
23
|
|
24
24
|
connection_type = ObjectType.define do
|
25
25
|
name("#{wrapped_type.name}Connection")
|
26
26
|
field :edges, types[edge_type]
|
27
27
|
field :pageInfo, PageInfo, property: :page_info
|
28
|
+
block && instance_eval(&block)
|
28
29
|
end
|
29
30
|
|
30
31
|
connection_type
|
@@ -10,11 +10,15 @@ describe GraphQL::Relay::RelationConnection do
|
|
10
10
|
result["data"]["empire"]["bases"]["pageInfo"]
|
11
11
|
end
|
12
12
|
|
13
|
+
def get_last_cursor(result)
|
14
|
+
result["data"]["empire"]["bases"]["edges"].last["cursor"]
|
15
|
+
end
|
16
|
+
|
13
17
|
describe "results" do
|
14
18
|
let(:query_string) {%|
|
15
|
-
query getShips($first: Int, $after: String, $last: Int, $before: String, $order: String){
|
19
|
+
query getShips($first: Int, $after: String, $last: Int, $before: String, $order: String, $nameIncludes: String){
|
16
20
|
empire {
|
17
|
-
bases(first: $first, after: $after, last: $last, before: $before, order: $order) {
|
21
|
+
bases(first: $first, after: $after, last: $last, before: $before, order: $order, nameIncludes: $nameIncludes) {
|
18
22
|
... basesConnection
|
19
23
|
pageInfo {
|
20
24
|
hasNextPage
|
@@ -24,6 +28,7 @@ describe GraphQL::Relay::RelationConnection do
|
|
24
28
|
}
|
25
29
|
|
26
30
|
fragment basesConnection on BaseConnection {
|
31
|
+
totalCount,
|
27
32
|
edges {
|
28
33
|
cursor
|
29
34
|
node {
|
@@ -32,6 +37,7 @@ describe GraphQL::Relay::RelationConnection do
|
|
32
37
|
}
|
33
38
|
}
|
34
39
|
|}
|
40
|
+
|
35
41
|
it 'limits the result' do
|
36
42
|
result = query(query_string, "first" => 2)
|
37
43
|
assert_equal(2, get_names(result).length)
|
@@ -48,12 +54,20 @@ describe GraphQL::Relay::RelationConnection do
|
|
48
54
|
assert_equal(false, get_page_info(result)["hasNextPage"])
|
49
55
|
end
|
50
56
|
|
57
|
+
it 'provides custom fileds on the connection type' do
|
58
|
+
result = query(query_string, "first" => 2)
|
59
|
+
assert_equal(
|
60
|
+
Base.where(faction_id: 2).count,
|
61
|
+
result["data"]["empire"]["bases"]["totalCount"]
|
62
|
+
)
|
63
|
+
end
|
64
|
+
|
51
65
|
it 'slices the result' do
|
52
66
|
result = query(query_string, "first" => 2)
|
53
67
|
assert_equal(["Death Star", "Shield Generator"], get_names(result))
|
54
68
|
|
55
69
|
# After the last result, find the next 2:
|
56
|
-
last_cursor = result
|
70
|
+
last_cursor = get_last_cursor(result)
|
57
71
|
|
58
72
|
result = query(query_string, "after" => last_cursor, "first" => 2)
|
59
73
|
assert_equal(["Headquarters"], get_names(result))
|
@@ -67,7 +81,7 @@ describe GraphQL::Relay::RelationConnection do
|
|
67
81
|
assert_equal(["Death Star", "Headquarters"], get_names(result))
|
68
82
|
|
69
83
|
# After the last result, find the next 2:
|
70
|
-
last_cursor = result
|
84
|
+
last_cursor = get_last_cursor(result)
|
71
85
|
|
72
86
|
result = query(query_string, "after" => last_cursor, "first" => 2, "order" => "name")
|
73
87
|
assert_equal(["Shield Generator"], get_names(result))
|
@@ -77,5 +91,30 @@ describe GraphQL::Relay::RelationConnection do
|
|
77
91
|
result = query(query_string, "first" => 2, "order" => "-name")
|
78
92
|
assert_equal(["Shield Generator", "Headquarters"], get_names(result))
|
79
93
|
end
|
94
|
+
|
95
|
+
it 'paginates with order' do
|
96
|
+
result = query(query_string, "first" => 2, "order" => "name")
|
97
|
+
assert_equal(["Death Star", "Headquarters"], get_names(result))
|
98
|
+
|
99
|
+
# After the last result, find the next 2:
|
100
|
+
last_cursor = result["data"]["empire"]["bases"]["edges"].last["cursor"]
|
101
|
+
|
102
|
+
result = query(query_string, "after" => last_cursor, "first" => 2, "order" => "name")
|
103
|
+
assert_equal(["Shield Generator"], get_names(result))
|
104
|
+
end
|
105
|
+
|
106
|
+
it "applies custom arguments" do
|
107
|
+
result = query(query_string, "first" => 1, "nameIncludes" => "ea")
|
108
|
+
assert_equal(["Death Star"], get_names(result))
|
109
|
+
|
110
|
+
after = get_last_cursor(result)
|
111
|
+
|
112
|
+
result = query(query_string, "first" => 2, "nameIncludes" => "ea", "after" => after )
|
113
|
+
assert_equal(["Headquarters"], get_names(result))
|
114
|
+
before = get_last_cursor(result)
|
115
|
+
|
116
|
+
result = query(query_string, "last" => 1, "nameIncludes" => "ea", "before" => before)
|
117
|
+
assert_equal(["Death Star"], get_names(result))
|
118
|
+
end
|
80
119
|
end
|
81
120
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -31,6 +31,16 @@ BaseType = GraphQL::ObjectType.define do
|
|
31
31
|
field :planet, types.String
|
32
32
|
end
|
33
33
|
|
34
|
+
# Define a connection which will wrap an ActiveRecord::Relation.
|
35
|
+
# We use an optional block to add fields to the connection type:
|
36
|
+
BaseConnection = GraphQL::Relay::RelationConnection.create_type(BaseType) do
|
37
|
+
field :totalCount do
|
38
|
+
type types.Int
|
39
|
+
resolve -> (obj, args, ctx) { obj.object.count }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
|
34
44
|
Faction = GraphQL::ObjectType.define do
|
35
45
|
name "Faction"
|
36
46
|
interfaces [NodeInterface]
|
@@ -49,12 +59,17 @@ Faction = GraphQL::ObjectType.define do
|
|
49
59
|
# You can define arguments here and use them in the connection
|
50
60
|
argument :nameIncludes, types.String
|
51
61
|
end
|
52
|
-
connection :bases,
|
62
|
+
connection :bases, BaseConnection do
|
53
63
|
# Resolve field should return an Array, the Connection
|
54
64
|
# will do the rest!
|
55
65
|
resolve -> (obj, args, ctx) {
|
56
|
-
Base.where(id: obj.bases)
|
66
|
+
all_bases = Base.where(id: obj.bases)
|
67
|
+
if args[:nameIncludes]
|
68
|
+
all_bases = all_bases.where("name LIKE ?", "%#{args[:nameIncludes]}%")
|
69
|
+
end
|
70
|
+
all_bases
|
57
71
|
}
|
72
|
+
argument :nameIncludes, types.String
|
58
73
|
end
|
59
74
|
end
|
60
75
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphql-relay
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Mosolgo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: graphql
|