Can't get policies to work with AshGraphql
I have an action,
update_customer_registration
, which requires a Customer
actor.
My Customer
policy looks like this:
This works as expected when using the application's Ash API directly:
Setting actor
to nil
, raises an policy error as expected.
However, it doesn't seem to work when going through AshGraphql, when the actor is present:
I know the actor is being set correctly with Ash.PlugHelpers.set_actor()
However, when I change the policy to match on any action (policy always() do
), it works. There seems to be something specific with specifying the action in the policy that doesn't work with AshGraphql.13 Replies
Do you have
AshGraphql.Plug
in your :api pipeline?Yes, I have it in the pipeline
And I'm calling
Ash.PlugHelpers.set_actor(conn, session_resource)
,
And I inspected session_resource
, and it's the Customer struct.My pipeline looks like this:
Assuming you have something similar set up, my next steps would be to set the following to see if it offers any more clues.
Yes, my pipeline looks similar to that.
Yes, I've set up logging.
The policy logging doesn't seem to work when going through AshGraphql through
When I enable policy logging, it works correctly when I go direct through my Ash API, but when going through GraphQL, this is the only thing that is added to the log output:
The basic policy integration seems to be working. It just doesn't work when the policy is specific about which action it applies to.
From memory,
ash_graphql
does need to have permission to also :read
the thing it's updating, could that be it?Maybe...
That was it!
I needed to add the
:read
action to the policyGreat! Glad you got it working. Also I just ran one of my unit tests that uses GQL, and when I turn on the policy logging it does log all the policy logs with breakdown, so i'm not sure why that doesn't work for you.
Yeah, I don't know. That's strange.
The other problem I need to solve with this GraphQL query is that it needs to get the Customer from the current actor.
I was temporarily allowing the request to pass in the Customer ID, but the requirement is that the session token is the only identifying information that will be passed in.
I'm not sure how to do that.
Does your actor have a relationship to the customer already?
The actor is the customer
Do I need to create a new read action to pass the actor along to the
update_customer_registration
action?You can create a new read action that gets the current actor from session. Here is what I have on my
actor
resource:
Then in your graphql definition:
Nice! I'll try that.
Wow, it worked!
Thanks @Alan Heywood !
You're welcome!