Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boilerplate MyApp.Web.PageControllerTest fails with AbsinthePlug + Phoenix 1.3.0-rc.0 #80

Closed
lauraannwilliams opened this issue May 5, 2017 · 2 comments

Comments

@lauraannwilliams
Copy link

lauraannwilliams commented May 5, 2017

This may very well be user error :)

I'm trying to set up a 1.3 project with absinthe. However, as soon as I add the plug, my default Page controller test stops passing.

1) test GET / (MyApp.Web.PageControllerTest)
   test/my_app/web/controllers/page_controller_test.exs:4
   ** (Plug.Conn.AlreadySentError) the response was already sent
   stacktrace:
     (plug) lib/plug/conn.ex:950: Plug.Conn.register_before_send/2
     (my_app) lib/my_app/web/router.ex:4: MyApp.Web.Router.browser/2
     (my_app) lib/my_app/web/router.ex:1: anonymous fn/1 in MyApp.Web.Router.__match_route__/4
     (phoenix) lib/phoenix/router.ex:273: Phoenix.Router.__call__/1
     (my_app) lib/my_app/web/endpoint.ex:1: MyApp.Web.Endpoint.plug_builder_call/2
     (my_app) lib/my_app/web/endpoint.ex:1: MyApp.Web.Endpoint.call/2
     (phoenix) lib/phoenix/test/conn_test.ex:224: Phoenix.ConnTest.dispatch/5
     test/my_app/web/controllers/page_controller_test.exs:5: (test)

It seems like as soon I add the plug to the endpoint, the "/" url starts returning early.
Possibly I've missed a step, but I'm not sure where to start looking. Any help would be appreciated

Steps to Reproduce!

1.) phx.new my_app
2.) add schema file in lib/my_app/web/schema.ex

defmodule MyApp.Web.Schema do
  use Absinthe.Schema

  @fake_db %{
  "foo" => %{id: "foo", name: "Foo", value: 4},
  "bar" => %{id: "bar", name: "Bar", value: 5}
  }

  @desc "A valuable Item"
  object :item do
    field :id, :id

    @desc "The item's name"
    field :name, :string

    field :value, :integer, description: "Recently appraised value"
  end

  query do
    @desc "Get an item by ID"
    field :item, :item do

      @desc "The ID of the item"
      arg :id, type: :id

      resolve fn %{id: id}, _ ->
        {:ok, Map.get(@fake_db, id)}
      end
    end
  end

end
  1. As per absinthe-graphql.org/guides/plug-phoenix/ , update endpoint.ex as follows
plug Plug.Parsers,
  parsers: [:urlencoded, :multipart, :json, Absinthe.Plug.Parser],
  json_decoder: Poison

plug Absinthe.Plug,
  schema: MyApp.Schema

this is the point the tests fail

  1. Update the router to forward to the absinthe plug, router file now looks like
defmodule MyApp.Web.Router do
  use MyApp.Web, :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 "/", MyApp.Web do
      pipe_through :browser # Use the default browser stack

      get "/", PageController, :index
    end

    forward "/api", Absinthe.Plug, schema: MyApp.Web.Schema

  # Other scopes may use custom stacks.
  # scope "/api", MyApp.Web do
  #   pipe_through :api
  # end
end

I tried putting the plug line in a few locations but it made no difference.

@benwilson512
Copy link
Contributor

If you're using a router you don't want to do plug Absinthe.Plug, but rather like any other plug that sends responses you want to use it within your router. You already have this actually, via forward "/api", Absinthe.Plug, schema: MyApp.Web.Schema.

If you want to use it within a phoenix pipeline you can just:

scope "/api" do
  forward "/api", Absinthe.Plug, schema: MyApp.Web.Schema
end

@lauraannwilliams
Copy link
Author

Thanks. Figured it was likely PEBKAC :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants