AshGraphql generated mutations are bypassing schema middleware
I have this middleware in my GraphQL schema to check if the user is authenticated:
All my custom made queries/mutations will reach that middleware function without a problem.
Now, I have this in one of my resources:
If I call that mutation, it will not call my
middleware
function at all.
If I create an equivalent for that mutation by hand, then the middleware
is called as expected.36 Replies
๐ค strange
can I see how you're adding the middleware to the schema?
This might be a question we need to ask the absinthe folks about
I just added the
middleware
function to the schema:
As far as I know, it should be called for every query/mutation/subscription used in the schema
Unless the way ash adds its queries/mutations are different than the way I'm adding my custom onesThats the only thing I can think of really
but it would be a pain for that to work that way TBH
looking into it now
I was wondering, can't we change this (see image) to allow the user to add custom middlewares per query/mutation? That way it will be able to set a middleware per query/mutation no?

We could ๐
but to some degree it gets into how much configurability is too much configurability. Ash should provide the tools to do these kinds of things in a way that works for graphql or json api or other transports.
I've just pushed an experimental option up to
main
Hmm, but I think that would also be problematic no? I mean, I need to check the
obj
and field
variables to see if I will apply the EnsureAuthenticated middleware or not (since I also do sign-up/in using graphql).
I'm not sure if I have that information inside the middleware itselfYeah, you'd have to put this logic:
inside the middleware. Is that bad?
FWIW if you want to have your special error when a user is not present you could just put that in the action
Hmm, let me check the middleware documentation again, I'm not sure I have these variables inside the middleware
but the action middleware option that I provided only runs at the top level on queries/mutations build by Ash
So if you want a user present for all actions run through Ash, that would be the way to do it. It doesn't happen for fields
I think I can add the logic to the middleware using
resolution.definition.schema_node
with will contain : place_offer
in this caseSo you do only want this for specific actions?
or for all actions?
If its for specific actions, why not this?
Ah, I think I got the whole thing wrong
So, it is not like the
middleware
function is not being called, it is. It is called when the system starts.
For some reason, it is not called when I run the query/mutation itself (it is called when I run my custom ones). But the middleware will be called anyway.
The odd thing is that the middleware will be called, it will set the result but the call will never reach AshGraphQL.Graphql.Resolver
it will go straight to the policies and fail there
I guess the question is, why it does that, I would expect that it will first call resolve
inside the AshGraphql.Graphql.Resolver
module no?๐ค okay so I get what you're saying about the middleware callback firing when we build the schema not every time.
But I'm not sure what you mean about the rest
resolve
runs the action which runs the policiesI added this to the
Resolver
module:
And recompiled that module.
Then, I run my mutation with playground, it will never reach that resolve
function, instead it will fail in one of my policies
I will run it with dbg to see if I can pry thereum...are you doing
mix deps.compile ash_graphql
after changing the dependency?
I think this might be off topicYep
Lets clarify one thing:
Do you want:
1. to require a user for every single action that calls your apis, except your custom authentication mutations/queries.
2. to require a user for specific actions
I want to require a user for a list of queries/mutations regardless if they are custom or created with Ash.
Right now, using
middleware
in my schema, I can achieve this partially since it works fine with any custom query that I'm using.
The problem is that the middleware is not working with AshGraphql generated queries/mutations. To be more specific, I can see that the middleware is being called, it is just that the AshGraphql.Graphql.Resolver
resolve
function is never called before it reaches the policies
, so I will never get the middleware errorOkay, I'm on board with everything up until the last part, I'm not convinced that is the error.
You can't get a policy failure without calling the resolver
Are you on the latest ash_graphql from github?
I added a catch all function to the resolver, it even complains that I will not reach the other
resolve
functions because of that when I compile it:
And still it doesn't reaches that function
Yes, I'm using the main branch to test that action_middleware change you madehuh
weird
I will rollback to the latest version and see if I get the same problem
Is there a stacktrace or anything for the policy error?
I made my policy authorize all now just to see if it would reach the resolver, but it doesn't, it will go straight to the action
Here is the stacktrace:
The KeyError is because the actor is nil btw
can you put this in your start function of your application
:erlang.system_flag(:backtrace_depth, 100)
so we can see a longer backtrace
I just can't think of any way that we would be running a resource action without calling the resolve
function
thats like...where it all happensOh, wow, thanks for that, I didn't know about that system_flag, it will help me a lot for debug from now on ๐
There it is
mutate/2
is what we're calling
not resolve/2
๐Ahรก!
let me test that, one sec
Yep, that was it hahah
def mutate(%Absinthe.Resolution{state: :resolved} = resolution, _),
do: resolution
Adding this to the resolver.ex file will fix the issue
ohhhhhhh
okay
good catch
Do you know what function absinthe will use in case of subscriptions? I'm not using it yet, but if it is another function, then probably we need the same change there also
pushed
Um...subscriptions don't really work quite the same
I'll add it to the other resolvers though,
resolve_calc
and resolve_assoc
Working like a charm!
I don't think we need the
action_middlewares
btwYeah, probably not. It doesn't hurt to leave it there though. Like if you want something specific to happen for all ash actions done via graphql
Do you plan to push a new release to ash_graphql shortly or should I just stay in main branch for now?
new release is going out soon, once CI is done