LittleKidogo / mula_spender

Some automated forms to aid in our spending
MIT License
3 stars 0 forks source link

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

Open zacck-zz opened 6 years ago

zacck-zz commented 6 years ago

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

zacck-zz commented 6 years ago

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

defmodule Spender.Schema
  …
  def 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]
  end
  …

 mutation 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 commented 6 years ago

Great! Will try this out.