Absinthe.Plug不可用

Jay*_*ley 12 elixir phoenix-framework

安装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)
Run Code Online (Sandbox Code Playgroud)

这是我的代表

{: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"},
Run Code Online (Sandbox Code Playgroud)

据我所知,我不需要添加任何其他东西.我按照这里的简单步骤:

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
Run Code Online (Sandbox Code Playgroud)

我添加了要求Absinthe.Plug,但这不起作用

Dog*_*ert 26

您正在传递一个alias(KerriganApiWeb)scope,它将别名预先传递给传递给路径声明函数内部的所有模块.这转换Absinthe.PlugKerriganApiWeb.Absinthe.Plug调用forward,这不是你想要的.你想要这个模块Absinthe.Plug.有两种方法可以解决这个问题:

  1. 删除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
    
    Run Code Online (Sandbox Code Playgroud)
  2. 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
    
    Run Code Online (Sandbox Code Playgroud)

Phoenix文档第一个会增加你的应用程序的编译时间.即使不是这种情况,我会选择第二个,因为我发现它更具可读性.