Hi,
Is it possible to use Absinthe without Phoenix?
Thus far, I have:
defmodule Service.Http.ApiServer do
use Plug.Router
plug Absinthe.Plug,
schema: Service.GraphQL.Schema
def start_link() do
{:ok, _} = Plug.Adapters.Cowboy.http(Service.Http.ApiServer, [])
end
end
But it moans with:
== Compilation error on file lib/http/api_server.ex ==
** (CompileError) lib/plug/router.ex:211: undefined function do_match/4
(stdlib) lists.erl:1338: :lists.foreach/2
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
Hey there. Neither Absinthe nor Absinthe.Plug require phoenix, Absinthe doesn't even know about HTTP. The problem you're running into is simply a plug problem. From the looks of the plug docs, a plug router needs a match macro https://hexdocs.pm/plug/Plug.Router.html
From your own docs:
To use, simply plug Absinthe.Plug in your pipeline.
plug Absinthe.Plug,
schema: MyApp.Schema
So this obviously isn't enough. Perhaps we can look at improving the documentation to avoid problems for others?
I understand why Plug requires a match, but when my only endpoint is Absinthe.Plug, shouldn't this be fulfilling that requirement?
The instructions are for use in a pipeline of plugs. If you aren't using a router it works as written. There's no point in reproducing the plug docs within Absinthe. Absinthe.Plug works exactly like every other plug.
Your router would have exactly the same error no matter what plug exists there.
defmodule Service.Http.ApiServer do
use Plug.Router
plug :foo
def foo(conn, _), do: conn
def start_link() do
{:ok, _} = Plug.Adapters.Cowboy.http(Service.Http.ApiServer, [])
end
end
** (CompileError) lib/plug/router.ex:211: undefined function do_match/4
(stdlib) lists.erl:1338: :lists.foreach/2
The error has nothing to do with Absinthe.
To elaborate: Unlike Phoenix, there isn't a single common structure to people's plug apps. Some use routers, some don't, and some use multiple. The point of writing it the way we did in the documentation is to indicate that it works exactly like every other plug. With that knowledge the reader is equipped to integrate it into their plug stack however they like.
Most helpful comment
To elaborate: Unlike Phoenix, there isn't a single common structure to people's plug apps. Some use routers, some don't, and some use multiple. The point of writing it the way we did in the documentation is to indicate that it works exactly like every other plug. With that knowledge the reader is equipped to integrate it into their plug stack however they like.