taro 2.3.0 → 2.4.0

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
  SHA256:
3
- metadata.gz: c96d6fd71ad22df31d5ab4cefb09389fd7ed5b41b5dd8821132fa2c5fa322837
4
- data.tar.gz: f56362229e93f2f8c51a5490245d4a37e224015fecd42bb86a35926a01e8d776
3
+ metadata.gz: 9c7750f1403fa65519d22915908d5aeb6cf5bbeec5497d68369bf5a4e37fb0e8
4
+ data.tar.gz: 0b6d6b440c1f4fcdb965af8fb1aaf2d26f2ca3a9d2a2fb924bfe9456cb811090
5
5
  SHA512:
6
- metadata.gz: 34ad2aedee3c3a21973420ca5e1185024c7d6b127ee9ca914b847134939bcd136e70f10130607b8392ce8044dfcb7c8014fa306d00d81b81266d68fcdad19b95
7
- data.tar.gz: 1bec2fbada3a23267f87195323b2b112297c21e78c22281071bd2ec84a7359f78836ed250e85ae3f3d1765f428dd7ff1bdaaf92f5df2bd17883abed11b7a39eb
6
+ metadata.gz: 1f37ffe80f9f14ef49bd3131f0a64f5254219c4851de19ec6523143d508440cb263741c2ca56858eeff917cf0a4d1002b642b4e6f87498e47b62b3ffc5fbc4c8
7
+ data.tar.gz: 59fbfef1126825880c2d5b64b1d2a3799c25502d094f9758fa9181d7e1e3fa44b2b2504ccbf24c1d18925a5e53ceef1e1ca309cd8980f8950d385d1f3197aadb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [2.4.0] - 2025-03-11
4
+
5
+ ### Added
6
+
7
+ - `PageWithTotalCountType` for paginated responses with a total count
8
+
3
9
  ## [2.3.0] - 2025-02-24
4
10
 
5
11
  ### Added
data/README.md CHANGED
@@ -65,7 +65,7 @@ class BikesController < ApplicationController
65
65
 
66
66
  # Support for arrays and paginated lists is built-in.
67
67
  api 'List all bikes'
68
- returns code: :ok, array_of: 'BikeType', desc: 'list of bikes'
68
+ returns code: :ok, array_of: 'BikeType', desc: 'List of bikes'
69
69
  def index
70
70
  render json: BikeType.array.render(Bike.all)
71
71
  end
@@ -89,9 +89,6 @@ class BikeType < ObjectType
89
89
  # Fields can reference other types and arrays of values
90
90
  field :users, array_of: 'UserType', null: false
91
91
 
92
- # Pagination is built-in for big lists
93
- field :parts, page_of: 'PartType', null: false
94
-
95
92
  # Custom methods can be chosen to resolve fields
96
93
  field :has_brand, type: 'Boolean', null: false, method: :brand?
97
94
 
@@ -273,6 +270,32 @@ class BikeType < ObjectType
273
270
  end
274
271
  ```
275
272
 
273
+ ### Pagination
274
+
275
+ Use `page_of:` to declare a paginated response. Call `page.render` on a type to render a page of records.
276
+
277
+ ```ruby
278
+ api 'List all bikes'
279
+ param :cursor, type: 'String', null: true, desc: 'Show bikes after this cursor'
280
+ returns code: :ok, page_of: 'BikeType', desc: 'A page of bikes'
281
+ def index
282
+ render json: BikeType.page.render(Bike.all, after: params[:cursor])
283
+ end
284
+ ```
285
+
286
+ By default, the response does not include a total count. To include it, use `page_with_total_count`:
287
+
288
+ ```ruby
289
+ api 'List all bikes'
290
+ param :cursor, type: 'String', null: true, desc: 'Show bikes after this cursor'
291
+ returns code: :ok, page_with_total_count_of: 'BikeType', desc: 'A page of bikes'
292
+ def index
293
+ render json: BikeType.page_with_total_count.render(Bike.all, after: params[:cursor])
294
+ end
295
+ ```
296
+
297
+ See also: [Derived types](#derived-types).
298
+
276
299
  ## FAQ
277
300
 
278
301
  ### How do I render API docs?
@@ -343,6 +366,7 @@ Why e.g. `field :id, type: 'UUID'` instead of `field :id, type: UUID`?
343
366
 
344
367
  The purpose of this is to reduce unnecessary autoloading of the whole type dependency tree in dev and test environments.
345
368
 
369
+ <a name="derived-types"></a>
346
370
  ### Can I define my own derived types like `page_of` or `array_of`?
347
371
 
348
372
  Yes.
@@ -13,14 +13,16 @@ class Taro::Types::ObjectTypes::PageType < Taro::Types::ResponseType
13
13
  field(:page_info, type: 'Taro::Types::ObjectTypes::PageInfoType', null: false)
14
14
  end
15
15
 
16
- def self.render(relation, after:, limit: 20, order_by: nil, order: nil)
16
+ def self.render(relation, **)
17
+ super(paginate(relation, **))
18
+ end
19
+
20
+ def self.paginate(relation, after:, limit: 20, order_by: nil, order: nil)
17
21
  result = RailsCursorPagination::Paginator.new(
18
22
  relation, limit:, order_by:, order:, after:
19
23
  ).fetch
20
-
21
24
  result[:page].map! { |el| el.fetch(:data) }
22
-
23
- super(result)
25
+ result
24
26
  end
25
27
 
26
28
  def self.default_openapi_name
@@ -0,0 +1,26 @@
1
+ # This is an expanded `PageType` that adds a `total_count` field
2
+ # to show the total number of records in the paginated relation.
3
+ # It is not recommended for very large relations where counting might be slow.
4
+ #
5
+ # Usage:
6
+ # - `returns code: :ok, page_with_total_count_of: 'UserType'`
7
+ # - `UserType.page_with_total_count.render(User.all, after: params[:cursor])`
8
+ #
9
+ # The gem rails_cursor_pagination must be installed to use this.
10
+ #
11
+ class Taro::Types::ObjectTypes::PageWithTotalCountType < Taro::Types::ObjectTypes::PageType
12
+ def self.derive_from(from_type)
13
+ super
14
+ field(:total_count, type: 'Integer', null: false)
15
+ end
16
+
17
+ def self.paginate(relation, **)
18
+ super.merge(total_count: relation.count)
19
+ end
20
+
21
+ def self.default_openapi_name
22
+ "#{item_type.openapi_name}_PageWithTotalCount"
23
+ end
24
+
25
+ define_derived_type :page_with_total_count, 'Taro::Types::ObjectTypes::PageWithTotalCountType'
26
+ end
data/lib/taro/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # :nocov:
2
2
  module Taro
3
- VERSION = "2.3.0"
3
+ VERSION = "2.4.0"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: taro
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janosch Müller
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2025-02-24 00:00:00.000000000 Z
12
+ date: 2025-03-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -89,6 +89,7 @@ files:
89
89
  - lib/taro/types/object_types/no_content_type.rb
90
90
  - lib/taro/types/object_types/page_info_type.rb
91
91
  - lib/taro/types/object_types/page_type.rb
92
+ - lib/taro/types/object_types/page_with_total_count_type.rb
92
93
  - lib/taro/types/rails_params_type.rb
93
94
  - lib/taro/types/response_type.rb
94
95
  - lib/taro/types/scalar/boolean_type.rb