Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Schema so Middleware is only applied to root objects #120

Open
zacck-zz opened this issue Jul 3, 2018 · 2 comments
Open

Refactor Schema so Middleware is only applied to root objects #120

zacck-zz opened this issue Jul 3, 2018 · 2 comments
Assignees

Comments

@zacck-zz
Copy link
Member

zacck-zz commented Jul 3, 2018

Expected Behavior

All the Mutation objects on our schema require that a user is authenticated for the operation, Instead of applying the Authorization middleware per a field every time we make a new mutation we should use the middleware/3 callback to apply it to all mutation fields see here https://github.com/absinthe-graphql/absinthe/blob/master/guides/middleware-and-plugins.md#2-using-the-middleware3-callback-in-your-schema on how to use it

Actual Behavior

At the moment the Authorization middleware is applied individually to every mutation object.

Acceptance Criteria

The Authorization middleware should be applied using the middleware/3 callback

Steps to Reproduce the Problem

Specifications

  • Version:
@zacck-zz
Copy link
Member Author

zacck-zz commented Aug 7, 2018

@WanjikuMac after some asking around I think I found a way of doing it

defmodule Spender.Schemadef middleware(middleware, _field, %{identifier: :mutation}) do
    type = Absinthe.Schema.lookup_type(Spender.Schema, :mutation)

    auth_meta = Absinthe.Type.meta(type, :auth)
    #=> :any

    [{{Authorize, :call}, [auth_meta]} | middleware]
  endmutation do
    @desc "Links an item to a LogSection"
    field :link_item, :log_section do
      arg :input, non_null(:link_item_input)
      meta auth: :any
      resolve &Resolvers.Planning.link_item/3
    end
end 
end

Notice how now in mutations instead of adding middleware Middleware.Authorize, :any we can now just add meta to the field and use that for auth

@WanjikuMac
Copy link
Contributor

Great! Will try this out.

@zacck-zz zacck-zz removed this from the Milestone 1 milestone Aug 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants