Optional middleware

Hi,

What I try to do:
All users on my site (guest or logged in) can create a post, when the user is logged in, the user_id will be added to the post, else the user_id is nil.

What actually happens:
If the user is logged in, the post get the user id, but when the user is not logged in, I get the following error:

"# FunctionClauseError at POST /api\n\nException:\n\n    ** (FunctionClauseError) no function clause matching in PollWeb.Resolvers.PollResolver.create_post/3\n    \n    The following arguments were given to PollWeb.Resolvers.PollResolver.create_post/3:\n    \n        # 1\n        %{}\n    \n        # 2\n        %{input: %{auth: false, options: [\"test123\", \"test1234\"], title: \"Test 123\"}}\n    \n        # 3\n        #Absinthe.Resolution<[acc: %{Absinthe.Middleware.Async => false, Absinthe.Middleware.Batch => %{input: [], output: %{}}}, adapter: Absinthe.Adapter.LanguageConventions, arguments: %{input: %{auth: false, options: [\"test123\", \"test1234\"], title: \"Test 123\"}}, context: %{__absinthe_plug__: %{uploads: %{}}, pubsub: PollWeb.Endpoint}, definition: %Absinthe.Blueprint.Document.Field{alias: nil, argument_data: %{input: %{auth: false, options: [\"test123\", \"test1234\"], title: \"Test 123\"}}, arguments: [%Absinthe.Blueprint.Input.Argument{errors: [], flags: %{}, input_value:

Middelware (authorize.ex)

  def call(resolution, role) do

    with %{current_user: current_user} <- resolution.context, true <- correct_role?(current_user, role) do
      resolution
    else
      _ ->
        resolution
        |> Absinthe.Resolution.put_result({:error, "unauthorized"})
    end
  end

  defp correct_role?(%{}, :public), do: true
  defp correct_role?(%{}, :any), do: true
  defp correct_role?(%{role: role}, role), do: true
  defp correct_role?(_, _), do: false

Resolver:

def create_post(_, %{input: input}, %{context: %{current_user: current_user}} \\ []) do
    slug = :crypto.strong_rand_bytes(12) |> Base.url_encode64 |> binary_part(0, 12)
    post_input= Map.merge(input, %{slug: slug, user_id: current_user.id})

    case Blog.create_post(post_input) do
      {:ok, post} -> 
        # [...]
        {:ok, post}
      {:error, changeset} -> {:error, ErrorHelpers.translate(changeset.errors)}
    end
  end

Schema:

    @desc "Create a post"
    field :create_post, type: :post_type do
      arg(:input, non_null(:post_input_type))
      middleware(Middleware.Authorize, :public)
      resolve(&Resolvers.`BlogResolver.create_postl/3)
    end

So: If the middleware has the parameter :public, everyone can use it, if the user is logged in, it has to send the user props, otherways it has to send nothing.

What am I doing wrong?

We need to see the point at which create_poll is called.

The error message says that someone called:

create_poll(<something>, %{}, ...)

But the only function head you’ve given us says that the second argument to create_poll must be a map that includes an input key. (so %{input: <something>}).

Nothing in your code snippets shows the point at which create_poll is being called.

Excuse me: create_poll = create_post