moxley

moxley

I’m running into a policy and AshGraphql problem, and it seems like a bug.

Here’s my GraphQL query:

query GetEvent2($id: ID!) {
  getEvent2(id: $id) {
    id
  }
}

It’s returning the following response:

%{"data" => %{"getEvent2" => nil}}

Here’s the policy debug log:

[debug] Successful authorization: GF.Events.Event2.read

Generated Filter:
invites.member_id == "BKDK97081LAQXMUJR"
Policy Breakdown
  member2: %{id: "BKDK97081LAQXMUJR"}

  Policy | 🔎:

    condition: action == :read

    authorize if: invites.member_id == "BKDK97081LAQXMUJR" | ✓ | 🔎

And here’s the policy definition:

    policy action :read do
      # authorize_if expr(is_public)
      # authorize_if expr(^actor(:status) == :active)
      authorize_if expr(invites.member_id == ^actor(:id))
    end

The primary resource is Event2, which has_many Invites. Each Invite has a member_id. The actor is a Member whose ID matches member_id.

If I use straight Ash.get!(Event2, event_id, actor: actor), that returns Event2 data.

If I uncomment those two other authorize_if lines, then modify the test setup to use either of them, the query returns the expected response data. It’s just that last authorize_if that seems to be tripping things up. What is different about it? Is it because it goes throug a has_many relationship?

I also tried opening up the :read policy on the Invite resource, to allow anyone to read that, and that didn’t help.

Showing Posts 1 to 9

zachdaniel

zachdaniel

Creator of Ash

Try using

authorize_if expr(exists(invites, member_id == ^actor(:id)))

EDIT: not sure if its your issue or not, but if it is, the context for what may be happening is here:

In the section on “using exists” (which I frustratingly cannot anchor link you directly to)

moxley

moxley OP

I tried that exists() expression earlier today, after reading that documentation. I just tried it again now, with the same result. Here’s the SQL from the logs, if you’re interested:

SELECT e0."id"
FROM "public"."events" AS e0
WHERE (e0."id" = $1)
  AND (e0."group_id" = $2)
  AND (
    EXISTS (
      SELECT 1
      FROM "events"."invites" AS si0
      WHERE (si0."member_id" = $3)
        AND (si0."group_id" = $4)
        AND (e0."id" = si0."event_id")
    )
  )

-- [469677, 970674, "V1KJW49URLTGJNFI8", 970674]
zachdaniel

zachdaniel

Creator of Ash

Hmm…that seems right? In terms of how the exists query should be run. Do you know where the group_id = $2 part of the query comes from?

moxley

moxley OP

The group_id is the tenant. That’s added automatically. The query looks correct. The policy passes (according to the logs). It’s just that the data returned from the GraphQL is empty.

zachdaniel

zachdaniel

Creator of Ash

:thinking: Is it possible to reproduce this w/o using AshGraphql? By calling an equivalent action under the hood? I’d be really surprised if somehow only the GraphQL was affected by policy filters in some way.

moxley

moxley OP

LIke this?

Yeah, it returned Event2 data, using the same test setup as the GraphQL test.

I’ll return to this tomorrow, and re-check the test, just to be sure.

zachdaniel

zachdaniel

Creator of Ash

It definitely seems like a bug somewhere, but I think its at the point where I’ll need to be able to reproduce it as nothing is really jumping out at me :cry:

moxley

moxley OP

I extracted the problem away from AshGraphql:

      result =
        GF.Events.Event2
        |> Ash.Query.for_read(:read)
        |> Ash.Query.filter(id == ^event.id)
        |> Ash.Query.select([:id])
        |> Ash.Query.set_tenant(ctx.group.id)
        |> Ash.read_first(actor: ctx.session_member)

      dbg(result)

This produces {:ok, nil}.

However, if I remove |> Ash.Query.set_tenant(ctx.group.id) from the expression, it returns the Event2 struct instead of nil.

moxley

moxley OP

Found the problem. The Invite didn’t have a group_id (tenant). It works now!

Thanks for working through this problem with me @zachdaniel!

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews