安装absinthe_plug后,我收到以下错误:
= Compilation error in file lib/kerrigan_api_web/router.ex ==
** (UndefinedFunctionError) function KerriganApiWeb.Absinthe.Plug.init/1 is undefined (module KerriganApiWeb.Absinthe.Plug is not available)
这是我的部门
{:phoenix, "~> 1.3.0"},
{:phoenix_pubsub, "~> 1.0"},
{:phoenix_ecto, "~> 3.2"},
{:postgrex, ">= 0.0.0"},
{:phoenix_html, "~> 2.10"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.11"},
{:cowboy, "~> 1.0"},
{:poison, "~> 3.1"},
{:absinthe, "~> 1.3.0"},
{:absinthe_plug, "~> 1.3.0"},
{:absinthe_ecto, git: "https://github.com/absinthe-graphql/absinthe_ecto.git"},
{:faker, "~> 0.7"},
据我所知,我不需要添加任何其他内容。 我按照这里的简单步骤操作:
absinthe_slug
编辑:我的路由器
defmodule KerriganApiWeb.Router do
use KerriganApiWeb, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/", KerriganApiWeb do
pipe_through :browser # Use the default browser stack
get "/", PageController, :index
resources "/hotsdata_user", UserController, except: [:new, :edit]
resources "/battletag_toonhandle_lookup", PlayerController, except: [:new, :edit]
forward "/graph", Absinthe.Plug, schema: KerriganApi.Schema
forward "/graphiql", Absinthe.Plug.GraphiQL, schema: KerriganApi.Schema
end
end
我添加了需要苦艾酒.Plug,但这不起作用
你正在向scope
传递一个alias
(KerriganApiWeb
(,这会将别名附加到传递给内部路由声明函数的所有模块。这会在调用forward
中将Absinthe.Plug
转换为KerriganApiWeb.Absinthe.Plug
,这不是您想要的。您希望模块Absinthe.Plug
.有两种方法可以解决此问题:
-
删除
alias
参数,并在所有需要它的路由声明函数中显式使用KerriganApiWeb
。scope "/" do pipe_through :browser # Use the default browser stack get "/", KerriganApiWeb.PageController, :index resources "/hotsdata_user", KerriganApiWeb.UserController, except: [:new, :edit] resources "/battletag_toonhandle_lookup", KerriganApiWeb.PlayerController, except: [:new, :edit] forward "/graph", Absinthe.Plug, schema: KerriganApi.Schema forward "/graphiql", Absinthe.Plug.GraphiQL, schema: KerriganApi.Schema end
-
创建一个具有相同路径和管道的新
scope
,并在其中声明forward
路由:scope "/", KerriganApiWeb do pipe_through :browser # Use the default browser stack get "/", PageController, :index resources "/hotsdata_user", UserController, except: [:new, :edit] resources "/battletag_toonhandle_lookup", PlayerController, except: [:new, :edit] end scope "/" do forward "/graph", Absinthe.Plug, schema: KerriganApi.Schema forward "/graphiql", Absinthe.Plug.GraphiQL, schema: KerriganApi.Schema end
Phoenix 文档说第一个会增加应用程序的编译时间。即使不是这种情况,我也会选择第二个,因为我发现它更具可读性。
您可以将两个端点 "/graph" 和 "graphiql" 移出范围 "/"。
defmodule KerriganApiWeb.Router do
use KerriganApiWeb, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/", KerriganApiWeb do
pipe_through :browser # Use the default browser stack
get "/", PageController, :index
resources "/hotsdata_user", UserController, except: [:new, :edit]
resources "/battletag_toonhandle_lookup", PlayerController, except: [:new, :edit]
end
forward "/graph", Absinthe.Plug, schema: KerriganApi.Schema
forward "/graphiql", Absinthe.Plug.GraphiQL, schema: KerriganApi.Schema, interface: :advanced
end