graphql-relay 0.3.0 → 0.3.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4a86533b3e1bc7030fe0016ee41e9492e25cc597
4
- data.tar.gz: d80d3d7006d8f37a7f208b49c2fd3dc5f78081c2
3
+ metadata.gz: ae595ba777a0d485536c949f06bc7b0741a97267
4
+ data.tar.gz: 92fb6814d222d1965d9b31bc8c8887a091b19524
5
5
  SHA512:
6
- metadata.gz: 9c0f624fba3f5e81724bdf73459cf72747f8f6f8ca9a569e15b99cd4391a72d25d098ec6f1c3932ce794d4d0db33b1ea2ec0e2292d11e7b79070989de2a0da09
7
- data.tar.gz: 04e49ec547a0930ac9b26a54b5a7107dd5a61fe1f758449915c58b4ea7f4c2bbc4917a49e77fff80f0497a478b83624e1b97461dffc21694fec20d8fa6f55e03
6
+ metadata.gz: 8797f9ab05278a90c3b9ac4a8f6997b2b466a6cc05469ae5833dcff23bdc12570fda500ec7a66e5a05519d101573bdf0e44be809852b495b16d0070abac0a471
7
+ data.tar.gz: 595265221e8f5a9e44b87c1145f4ebe4af9e72a3fc0aed0ba0c72ce6c515f61bed75b7a800c1010f9d316752542f0fe200452853256b758cc7256139261b5653
data/lib/graphql/relay.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'base64'
2
+ require 'graphql'
2
3
  # MONKEY PATCHES 😬
3
4
  require 'graphql/definition_helpers/defined_by_config/definition_config'
4
5
  require_relative './object_type.rb'
@@ -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
@@ -1,5 +1,5 @@
1
1
  module GraphQL
2
2
  module Relay
3
- VERSION = '0.3.0'
3
+ VERSION = '0.3.1'
4
4
  end
5
5
  end
@@ -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["data"]["empire"]["bases"]["edges"].last["cursor"]
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["data"]["empire"]["bases"]["edges"].last["cursor"]
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
@@ -2,7 +2,6 @@ require "codeclimate-test-reporter"
2
2
  CodeClimate::TestReporter.start
3
3
  require "sqlite3"
4
4
  require "active_record"
5
- require "graphql"
6
5
  require "graphql/relay"
7
6
  require "minitest/autorun"
8
7
  require "minitest/focus"
@@ -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, BaseType.connection_type do
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.0
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-25 00:00:00.000000000 Z
11
+ date: 2015-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql