How to apply directives on fields in Absinthe?

How are directives used?
How can I specify them on fields in my schema for example?
(so server side, not client side)

@i-n-g-m-a-r Absinthe does not use directives for schema manipulation at this time. When we support SDL then directives will be available for that. For macro based schemas there are other mechanisms for manipulating your schema. what are you trying to accomplish?

1 Like

I am figuring out how to restrict access to fields.
Something like @hasrole("some role").
I would like access control to be transparent to the client.
So that’s why I was looking for something other than null.

That sounds like a good job for middleware: https://hexdocs.pm/absinthe/Absinthe.Middleware.html

Using it would look something like this:

    field :topics, list_of(:topic) do
      middleware(Middleware.AuthorizePermission, :required_role_of_admin)
      resolve &MyResolver.resolve/3
    end
1 Like

What should the middleware return?
An error like :unauthorized is not a valid return type for a field right?
So in that case the returned value should be null,
which would not denote the access restriction.

One could just add an error to the resolution with a message like "forbidden":

resolution
|> Resolution.put_result({:error, "forbidden"})