Is it possible to unscope one route or reference in a Phoenix router scope?
Here is the code that is giving me trouble:
scope "/admin", MyApp.Admin, as: :admin do
pipe_through :browser
pipe_through :admin_browser
resources "/rooms", RoomsController, only: [:show, :index, :update]
# ... other resources defined
forward "/graphiql", Absinthe.Plug.GraphiQL, schema: MyApp.Schema
end
I am getting the error: "(UndefinedFunctionError) function MyApp.Admin.Absinthe.Plug.GraphiQL.init/1 is undefined (module MyApp.Admin.Absinthe.Plug.GraphiQL is not available)"
which makes sense given the scope’s alias. Since I have many different controllers under the MyApp.Admin
“namespace” I would like to keep the alias, but “ignore” it for this one absinthe plug. Is that possible? Or do I have to remove the alias completely?
1 Like
Interestingly enough, you can have two admin scopes:
scope "/admin", MyApp.Admin, as: :admin do
pipe_through :browser
pipe_through :admin_browser
resources "/rooms", RoomsController, only: [:show, :index, :update]
# ... other resources defined
end
scope "/admin" do
pipe_through :browser
pipe_through :admin_browser
forward "/graphiql", Absinthe.Plug.GraphiQL, schema: MyApp.Schema
end
2 Likes
Ah, yeah that works. I would prefer to have just one "/admin"
scope so after playing around a bit I am leaning towards the following setup:
scope "/admin", as: :admin do
pipe_through :admin_browser
forward "/graphiql", Absinthe.Plug.GraphiQL, schema: MyApp.Schema
scope "/", MyApp.Admin do
pipe_through :browser
resources "/communities", CommunityController, only: [:show, :index, :update]
end
end
Is there any reason not to go with that approach? (note that the pipe_through :browser
line does not apply to /admin/graphiql
because of CSRF and mime type issues). I do realize that the Phoenix routing guide states:
Although technically scopes can also be nested (just like resources), the use of nested scopes is generally discouraged because it can sometimes make our code confusing and less clear.
I am open to hearing arguments for the completely separate scopes over my current solution if anyone has any.
Also @benwilson512 would you welcome a PR to mention this issue on the Absinthe Plug and Phoenix Setup doc?