graphql-relay 0.6.2 → 0.7.0

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: 72df43a2a611dfb93bb640fa8796d5afc308355b
4
- data.tar.gz: c9bd23e5f3afd777bf8e43d538b9f53a26556a3c
3
+ metadata.gz: a058f8b464d8faae1b7937bee2f0da2d2f821d3b
4
+ data.tar.gz: 224cf4b1ea77755508505d43ae5ed15fbd9a3b0b
5
5
  SHA512:
6
- metadata.gz: c276a95e9777af270a4c0affa60904cb3312c1c1a8f82031c46ffb1268bb7509ddf80275992719cb735e3f5e6de96274b9848c0bc3235c8063d4b702bcfc4459
7
- data.tar.gz: eafed64a5566fa095032681a62e22527255700abf4e225abd89da7d0c5d379d43fd4641746b8afd8e1840c40ea6af23e9e2a01df16c68f4322682b0f183a123e
6
+ metadata.gz: 959da8064bacec3964fe0e449bcf142547ac792604ceed31636281476d817f216538fc86275b72883ae0a028bb47bfcb96219d305edd266da29317073bbb0632
7
+ data.tar.gz: 35373b6b9c72e102c54f23a9abc2d9dc6271c2e0d4ef3cc43e8b899cc39ce7843a71ca242653eaad77d98752d0e8f2b9aacc7e804db8dff10cb1796423bccbff
data/README.md CHANGED
@@ -126,6 +126,14 @@ connection :featured_comments, CommentType.connection_type do
126
126
  end
127
127
  ```
128
128
 
129
+ #### Maximum Page Size
130
+
131
+ You can limit the number of results with `max_page_size:`:
132
+
133
+ ```ruby
134
+ connection :featured_comments, CommentType.connection_type, max_page_size: 50
135
+ ```
136
+
129
137
  #### Connection types
130
138
 
131
139
  You can customize a connection type with `.define_connection`:
@@ -146,9 +154,9 @@ Now, `PostType.connection_type` will include a `totalCount` field.
146
154
 
147
155
  Maybe you need to make a connection object yourself (for example, to return a connection type from a mutation). You can create a connection object like this:
148
156
 
149
- ```
150
- items = ... # your collection objects
151
- args = {} # stub out arguments for this connection object
157
+ ```ruby
158
+ items = [...] # your collection objects
159
+ args = {} # stub out arguments for this connection object
152
160
  connection_class = GraphQL::Relay::BaseConnection.connection_for_items(items)
153
161
  connection_class.new(items, args)
154
162
  ```
@@ -310,7 +318,6 @@ https://medium.com/@gauravtiwari/graphql-and-relay-on-rails-first-relay-powered-
310
318
 
311
319
  ## Todo
312
320
 
313
- - Add a `max_page_size` config for connections?
314
321
  - Refactor some RelationConnection issues:
315
322
  - fix [unbounded count in page info](https://github.com/rmosolgo/graphql-relay-ruby/blob/88b3d94f75a6dd4c8b2604743108db31f66f8dcc/lib/graphql/relay/base_connection.rb#L79-L86), [details](https://github.com/rmosolgo/graphql-relay-ruby/issues/1)
316
323
 
@@ -16,8 +16,9 @@ module GraphQL
16
16
  def paged_nodes
17
17
  @paged_nodes = begin
18
18
  items = sliced_nodes
19
- first && items = items.first(first)
20
- last && items.length > last && items.last(last)
19
+ limit = [first, last, max_page_size].compact.min
20
+ first && items = items.first(limit)
21
+ last && items.length > last && items.last(limit)
21
22
  items
22
23
  end
23
24
  end
@@ -8,6 +8,7 @@ module GraphQL
8
8
  # In a subclass, you have access to
9
9
  # - {#object}, the object which the connection will wrap
10
10
  # - {#first}, {#after}, {#last}, {#before} (arguments passed to the field)
11
+ # - {#max_page_size} (the specified maximum page size that can be returned from a connection)
11
12
  #
12
13
  class BaseConnection
13
14
  # Just to encode data in the cursor, use something that won't conflict
@@ -60,11 +61,16 @@ module GraphQL
60
61
  CONNECTION_IMPLEMENTATIONS[items_class.name] = connection_class
61
62
  end
62
63
 
63
- attr_reader :object, :arguments
64
+ attr_reader :object, :arguments, :max_page_size
64
65
 
65
- def initialize(object, arguments)
66
+ # Make a connection, wrapping `object`
67
+ # @param The collection of results
68
+ # @param Query arguments
69
+ # @param max_page_size [Int] The maximum number of results to return
70
+ def initialize(object, arguments, max_page_size: nil)
66
71
  @object = object
67
72
  @arguments = arguments
73
+ @max_page_size = max_page_size
68
74
  end
69
75
 
70
76
  # Provide easy access to provided arguments:
@@ -27,12 +27,13 @@ module GraphQL
27
27
  # - Merging in the default arguments
28
28
  # - Transforming its resolve function to return a connection object
29
29
  # @param [GraphQL::Field] A field which returns items to be wrapped as a connection
30
+ # @param max_page_size [Integer] The maximum number of items which may be requested (if a larger page is requested, it is limited to this number)
30
31
  # @return [GraphQL::Field] A field which serves a connections
31
- def self.create(underlying_field)
32
+ def self.create(underlying_field, max_page_size: nil)
32
33
  underlying_field.arguments = DEFAULT_ARGUMENTS.merge(underlying_field.arguments)
33
34
  # TODO: make a public API on GraphQL::Field to expose this proc
34
35
  original_resolve = underlying_field.instance_variable_get(:@resolve_proc)
35
- underlying_field.resolve = get_connection_resolve(underlying_field.name, original_resolve)
36
+ underlying_field.resolve = get_connection_resolve(underlying_field.name, original_resolve, max_page_size: max_page_size)
36
37
  underlying_field
37
38
  end
38
39
 
@@ -41,14 +42,14 @@ module GraphQL
41
42
  # Wrap the original resolve proc
42
43
  # so you capture its value, then wrap it in a
43
44
  # connection implementation
44
- def self.get_connection_resolve(field_name, underlying_resolve)
45
+ def self.get_connection_resolve(field_name, underlying_resolve, max_page_size: nil)
45
46
  -> (obj, args, ctx) {
46
47
  items = underlying_resolve.call(obj, args, ctx)
47
48
  if items == GraphQL::Query::DEFAULT_RESOLVE
48
49
  items = obj.public_send(field_name)
49
50
  end
50
51
  connection_class = GraphQL::Relay::BaseConnection.connection_for_items(items)
51
- connection_class.new(items, args)
52
+ connection_class.new(items, args, max_page_size: max_page_size)
52
53
  }
53
54
  end
54
55
  end
@@ -1,8 +1,8 @@
1
1
  class GraphQL::DefinitionHelpers::DefinedByConfig::DefinitionConfig
2
2
  # Wraps a field definition with a ConnectionField
3
- def connection(name, type = nil, desc = nil, property: nil, &block)
3
+ def connection(name, type = nil, desc = nil, property: nil, max_page_size: nil, &block)
4
4
  underlying_field = field(name, type, desc, property: property, &block)
5
- connection_field = GraphQL::Relay::ConnectionField.create(underlying_field)
5
+ connection_field = GraphQL::Relay::ConnectionField.create(underlying_field, max_page_size: max_page_size)
6
6
  fields[name.to_s] = connection_field
7
7
  end
8
8
 
@@ -28,7 +28,7 @@ module GraphQL
28
28
  #
29
29
  # MutationType = GraphQL::ObjectType.define do
30
30
  # # The mutation object exposes a field:
31
- # field :updateName, UpdateNameMutation.field
31
+ # field :updateName, field: UpdateNameMutation.field
32
32
  # end
33
33
  #
34
34
  # # Then query it:
@@ -19,8 +19,9 @@ module GraphQL
19
19
  def paged_nodes
20
20
  @paged_nodes = begin
21
21
  items = sliced_nodes
22
- first && items = items.first(first)
23
- last && items.count > last && items = items.last(last)
22
+ limit = [first, last, max_page_size].compact.min
23
+ first && items = items.first(limit)
24
+ last && items.count > last && items = items.last(limit)
24
25
  items
25
26
  end
26
27
  end
@@ -76,7 +77,9 @@ module GraphQL
76
77
  def create_order_condition(table, column, value, direction_marker)
77
78
  table_name = ActiveRecord::Base.connection.quote_table_name(table)
78
79
  name = ActiveRecord::Base.connection.quote_column_name(column)
79
- if (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR >= 2) || ActiveRecord::VERSION::MAJOR > 4
80
+ if ActiveRecord::VERSION::MAJOR == 5
81
+ casted_value = object.table.able_to_type_cast? ? object.table.type_cast_for_database(column, value) : value
82
+ elsif ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR >= 2
80
83
  casted_value = object.table.engine.columns_hash[column].cast_type.type_cast_from_user(value)
81
84
  else
82
85
  casted_value = object.table.engine.columns_hash[column].type_cast(value)
@@ -1,5 +1,5 @@
1
1
  module GraphQL
2
2
  module Relay
3
- VERSION = '0.6.2'
3
+ VERSION = '0.7.0'
4
4
  end
5
5
  end
@@ -94,6 +94,44 @@ describe GraphQL::Relay::RelationConnection do
94
94
  result = query(query_string, "last" => 1, "nameIncludes" => "ea", "before" => before)
95
95
  assert_equal(["Death Star"], get_names(result))
96
96
  end
97
+
98
+ it "applies the maximum limit for relation connection types" do
99
+ limit_query_string = %|
100
+ query getShips($first: Int){
101
+ empire {
102
+ basesWithMaxLimitRelation(first: $first) {
103
+ edges {
104
+ node {
105
+ name
106
+ }
107
+ }
108
+ }
109
+ }
110
+ }
111
+ |
112
+
113
+ result = query(limit_query_string, "first" => 3)
114
+ assert_equal(2, result["data"]["empire"]["basesWithMaxLimitRelation"]["edges"].size)
115
+ end
116
+
117
+ it "applies the maximum limit for relation connection types" do
118
+ limit_query_string = %|
119
+ query getShips($first: Int){
120
+ empire {
121
+ basesWithMaxLimitArray(first: $first) {
122
+ edges {
123
+ node {
124
+ name
125
+ }
126
+ }
127
+ }
128
+ }
129
+ }
130
+ |
131
+
132
+ result = query(limit_query_string, "first" => 3)
133
+ assert_equal(2, result["data"]["empire"]["basesWithMaxLimitArray"]["edges"].size)
134
+ end
97
135
  end
98
136
 
99
137
  describe "without a block" do
@@ -82,6 +82,14 @@ Faction = GraphQL::ObjectType.define do
82
82
  connection :basesByName, BaseType.connection_type, property: :bases do
83
83
  argument :order, types.String, default_value: "name"
84
84
  end
85
+
86
+ connection :basesWithMaxLimitRelation, BaseType.connection_type, max_page_size: 2 do
87
+ resolve -> (object, args, context) { Base.all }
88
+ end
89
+
90
+ connection :basesWithMaxLimitArray, BaseType.connection_type, max_page_size: 2 do
91
+ resolve -> (object, args, context) { Base.all.to_a }
92
+ end
85
93
  end
86
94
 
87
95
  # Define a mutation. It will also:
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.6.2
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Mosolgo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-11 00:00:00.000000000 Z
11
+ date: 2016-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql