davidarmstronglewis

davidarmstronglewis

Duplicate scope entries with different pipe_through?

Hey all! Just ran into a small considering that I don’t have a good answer for.

I’m building an application that uses Absinthe and I’d like to expose its GraphiQL interface. One fun aspect of this is that any external traffic to this application’s admin/ routes is denied by default unless you’re on VPN. Because of this I want to expose both the Phoenix dashboard under the /admin scope with…

scope "/admin" do
  import Phoenix.LiveDashboard.Router
  pipe_through [:fetch_session, :protect_from_forgery]

  live_dashboard "/dashboard", metrics: MyApplicationWeb.Telemetry
end

scope "/admin" do
  pipe_through [:api]

  forward "/graphiql", Absinthe.Plug.GraphiQL,
    schema: MyApplicationWeb.Schema,
    interface: :playground,
    context: %{pubsub: MyApplicationWeb.Endpoint}
end  

An issue I’m seeing with this is that I’m duplicating scopes here and don’t understand enough about the Phoenix Router to know if this the right way to expose two different endpoints that require different pipelines under the same /admin scope. I’ve gone ahead and tested out keeping them under the same scope but ran into some funky behavior where depending on how I order things I’ll get broken behavior.

For example, this seems to work:

scope "/admin" do
  pipe_through [:api]
  forward "/graphiql", Absinthe.Plug.GraphiQL,
    schema: MyApplicationWeb.Schema,
    interface: :playground,
    context: %{pubsub: MyApplicationWeb.Endpoint}

  import Phoenix.LiveDashboard.Router
  pipe_through [:fetch_session, :protect_from_forgery]
  live_dashboard "/dashboard", metrics: MyApplicationWeb.Telemetry
end

Whereas this does not work:

scope "/admin" do
  import Phoenix.LiveDashboard.Router
  pipe_through [:fetch_session, :protect_from_forgery]
  live_dashboard "/dashboard", metrics: MyApplicationWeb.Telemetry

  pipe_through [:api]
  forward "/graphiql", Absinthe.Plug.GraphiQL,
    schema: MyApplicationWeb.Schema,
    interface: :playground,
    context: %{pubsub: MyApplicationWeb.Endpoint}
end

Without knowing much more about how this works “under the hood”, I’m really just guessing at which will cause fewer problems in the long run. Curious if people have dealt with this situation before and how you think about :slight_smile:

Most Liked

al2o3cr

al2o3cr

scope is fine to repeat - all it does is push a set of options onto the router’s stack:

The ordering issue you’re seeing is because pipe_through adds the pipes to the scope, but they only apply to calls to route and scope following them in the scope. For instance, here’s where Scope.push grabs the current value of pipes:

As a result, the GraphiQL forward will see just the api pipe in the first ordering, and fetch_session, protect_from_forgery, api in the second.

IMO the clearest way to avoid needing to remember exactly how this ordering thing works is:

  • keep pipe_through calls together at the top of a scope
  • different pipelines? different scope.
davidarmstronglewis

davidarmstronglewis

@al2o3cr thanks for taking the time to respond! The explanation makes sense and thanks for sharing your views on the source. Definitely helpful for building out that mental model that I was missing :slight_smile:

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement