Skip to content

Commit

Permalink
fix: properly support endpoints being lists (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterhartman authored Oct 22, 2023
1 parent 135c5ed commit 517a90d
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 31 deletions.
1 change: 1 addition & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ spark_locals_without_parens = [
paginator: 1,
path: 1,
runtime_sort?: 1,
tesla: 1,
write_entity_path: 1,
write_path: 1
]
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ ash_json_api_wrapper-*.tar

# Temporary files, for example, from tests.
/tmp/

# OS X folder metadata
.DS_Store
8 changes: 4 additions & 4 deletions lib/data_layer/data_layer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -650,10 +650,10 @@ defmodule AshJsonApiWrapper.DataLayer do
end

defp runtime_filter({:ok, results}, query) do
if not is_nil(query.runtime_filter) do
Ash.Filter.Runtime.filter_matches(query.api, results, query.runtime_filter)
else
if is_nil(query.runtime_filter) do
{:ok, results}
else
Ash.Filter.Runtime.filter_matches(query.api, results, query.runtime_filter)
end
end

Expand Down Expand Up @@ -707,7 +707,7 @@ defmodule AshJsonApiWrapper.DataLayer do

defp make_request(resource, query) do
# log_send(path, query)
IO.inspect(query.query_params)
# IO.inspect(query.query_params)

AshJsonApiWrapper.DataLayer.Info.tesla(resource).get(query.path,
body: query.body,
Expand Down
4 changes: 2 additions & 2 deletions lib/data_layer/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule AshJsonApiWrapper.DataLayer.Info do
resource
|> Extension.get_entities([:json_api_wrapper, :endpoints])
|> Enum.reject(& &1.get_for)
|> Enum.find(&(&1.action == action))
|> Enum.find(&Enum.member?(&1.action, action))
|> case do
nil ->
default_endpoint
Expand All @@ -69,7 +69,7 @@ defmodule AshJsonApiWrapper.DataLayer.Info do
resource
|> Extension.get_entities([:json_api_wrapper, :endpoints])
|> Enum.find(fn endpoint ->
endpoint.action == action && endpoint.get_for == get_for
Enum.member?(endpoint.action, action) && endpoint.get_for == get_for
end)
|> case do
nil ->
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ defmodule AshJsonApiWrapper.MixProject do
{:ex_check, "~> 0.12.0", only: :dev},
{:credo, ">= 0.0.0", only: :dev, runtime: false},
{:dialyxir, ">= 0.0.0", only: :dev, runtime: false},
{:sobelow, ">= 0.0.0", only: :dev, runtime: false},
{:sobelow, "~> 0.13", only: :dev, runtime: false},
{:git_ops, "~> 2.5", only: :dev},
{:excoveralls, "~> 0.13.0", only: [:dev, :test]},
{:mix_test_watch, "~> 1.0", only: :dev, runtime: false},
Expand Down
40 changes: 16 additions & 24 deletions mix.lock

Large diffs are not rendered by default.

95 changes: 95 additions & 0 deletions test/petstore_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
defmodule AshJsonApiWrapper.Petstore.Test do
use ExUnit.Case
require Ash.Query

@moduletag :petstore

defmodule TestingTesla do
use Tesla
# plug Tesla.Middleware.Logger
end

defmodule Petstore.Order do
use Ash.Resource, data_layer: AshJsonApiWrapper.DataLayer

json_api_wrapper do
tesla(TestingTesla)

endpoints do
base("https://petstore3.swagger.io/api/v3")

endpoint [:find_pets_by_status, :fpbs] do
path("/pet/findByStatus")

field :status do
filter_handler(:simple)
end
end

get_endpoint :pet, :id do
path("/pet/:id")
end
end

fields do
end
end

actions do
read(:find_pets_by_status) do
primary? true
end

read(:fpbs) do
primary? false
end

read(:pet) do
primary? false
end
end

attributes do
attribute :id, :integer do
primary_key?(true)
allow_nil?(false)
end

# attribute(:category, :string)
attribute(:name, :string)
attribute(:photo_urls, :string)

attribute :status, :atom do
constraints(one_of: [:available, :pending, :sold])
end

# attribute(:tags, :string)
end
end

defmodule Api do
@moduledoc false
use Ash.Api

resources do
allow_unregistered?(true)
end
end

test "it works" do
Petstore.Order
|> Ash.Query.for_read(:find_pets_by_status)
|> Ash.Query.filter(status == "pending")
|> Api.read!()

Petstore.Order
|> Ash.Query.for_read(:fpbs)
|> Ash.Query.filter(status == "available")
|> Api.read!()

Petstore.Order
|> Ash.Query.for_read(:pet)
|> Ash.Query.filter(id == 1)
|> Api.read!()
end
end

0 comments on commit 517a90d

Please sign in to comment.