forked from ash-project/ash_json_api_wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: restore support for custom paginators (ash-project#6)
- Loading branch information
1 parent
2c8fe82
commit 4ecdcea
Showing
6 changed files
with
139 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
defmodule AshJsonApiWrapper.CustomPagination.Test do | ||
use ExUnit.Case | ||
require Ash.Query | ||
@moduletag :custom_pagination | ||
|
||
# ── Custom paginator ── | ||
|
||
defmodule CustomPaginator do | ||
use AshJsonApiWrapper.Paginator | ||
|
||
def cursor do | ||
case :ets.whereis(:cursor) do | ||
:undefined -> | ||
:ets.new(:cursor, [:set, :protected, :named_table]) | ||
|> :ets.insert({self(), 1}) | ||
|
||
1 | ||
|
||
_ -> | ||
[{_, value} | _rest] = :ets.lookup(:cursor, self()) | ||
value | ||
end | ||
end | ||
|
||
def increment_cursor do | ||
:ets.insert(:cursor, {self(), cursor() + 1}) | ||
end | ||
|
||
def reset_cursor do | ||
:ets.insert(:cursor, {self(), 1}) | ||
end | ||
|
||
def continue(_response, [], _) do | ||
reset_cursor() | ||
:halt | ||
end | ||
|
||
def continue(_response, _entities, _opts) do | ||
increment_cursor() | ||
{:ok, %{params: %{:p => cursor()}}} | ||
end | ||
end | ||
|
||
# ── Resource ── | ||
|
||
defmodule Users do | ||
use Ash.Resource, | ||
data_layer: AshJsonApiWrapper.DataLayer, | ||
validate_api_inclusion?: false | ||
|
||
json_api_wrapper do | ||
tesla(Tesla) | ||
|
||
endpoints do | ||
base("https://65383945a543859d1bb1528e.mockapi.io/api/v1") | ||
|
||
endpoint :list_users do | ||
path("/users") | ||
limit_with {:param, "l"} | ||
runtime_sort? true | ||
paginator CustomPaginator | ||
end | ||
end | ||
end | ||
|
||
actions do | ||
read(:list_users) do | ||
primary?(true) | ||
|
||
pagination do | ||
offset?(true) | ||
required?(true) | ||
default_limit(50) | ||
end | ||
end | ||
end | ||
|
||
attributes do | ||
attribute :id, :integer do | ||
primary_key?(true) | ||
allow_nil?(false) | ||
end | ||
|
||
attribute(:name, :string) | ||
end | ||
end | ||
|
||
defmodule Api do | ||
use Ash.Api, validate_config_inclusion?: false | ||
|
||
resources do | ||
allow_unregistered?(true) | ||
end | ||
end | ||
|
||
# ── Test it! ── | ||
|
||
test "it works" do | ||
Application.put_env(:ash, :validate_api_resource_inclusion?, false) | ||
Application.put_env(:ash, :validate_api_config_inclusion?, false) | ||
|
||
users = | ||
Users | ||
|> Ash.Query.for_read(:list_users) | ||
|> Api.read!(page: [limit: 99]) | ||
|
||
user_count = users.results |> Enum.count() | ||
|
||
assert(user_count == 99) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters