Skip to content

Commit

Permalink
handle not found case
Browse files Browse the repository at this point in the history
  • Loading branch information
barnabasJ committed Aug 5, 2024
1 parent adafc43 commit 9db3a74
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule AshGraphql.Endpoint do
defp is_forbidden(%{errors: errors}) do
errors
|> List.wrap()
|> Enum.any?(fn error -> Map.get(error, :code) == "forbidden" end)
|> Enum.any?(fn error -> Map.get(error, :code) in ["forbidden", "not_found"] end)
end

defp is_forbidden(_), do: false
Expand Down
11 changes: 9 additions & 2 deletions lib/graphql/resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ defmodule AshGraphql.Graphql.Resolver do
query =
AshGraphql.Subscription.query_for_subscription(
resource
|> Ash.Query.new(),
|> Ash.Query.for_read(:read, %{}, actor: Map.get(context, :actor)),
domain,
resolution
)
Expand All @@ -525,7 +525,14 @@ defmodule AshGraphql.Graphql.Resolver do
Ash.Query.filter(query, ^ref(key) == ^value)
end)

case query |> domain.read_one(actor: resolution.context[:current_user]) do
case query |> domain.read_one() do
# should only happen if a resource is created/updated and the subscribed user is not allowed to see it
{:ok, nil} ->
resolution
|> Absinthe.Resolution.put_result(
{:error, to_errors([Ash.Error.Query.NotFound.exception()], context, domain)}
)

{:ok, result} ->
resolution
|> Absinthe.Resolution.put_result({:ok, result})
Expand Down
5 changes: 3 additions & 2 deletions lib/resource/subscription/default_config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ defmodule AshGraphql.Resource.Subscription.DefaultConfig do
# context_id is exposed to the client so we might need to encrypt it
# or save it in ets or something and send generate a hash or something
# as the context_id
{:ok, topic: "*", context_id: Base.encode64(:erlang.term_to_binary(filter))}
dbg(filter)
{:ok, topic: "*", context_id: dbg(Base.encode64(:erlang.term_to_binary(filter)))}

_ ->
e ->
{:error, "unauthorized"}
end
end
Expand Down

0 comments on commit 9db3a74

Please sign in to comment.