Error when adding Absinthe.Subscription to supervisor tree

When adding Absinthe.Subscription to supervisor tree I got the following error.

Elixir Version: 1.7.3
Absinthe: 1.4.13
Phoenix Version: 1.4.0-dev

application.ex

  def start(_type, _args) do
    # List all child processes to be supervised
    children = [
      # Start the Ecto repository
      MyApp.Repo,
      # Start the endpoint when the application starts
      MyAppWeb.Endpoint,
      # Starts a worker by calling: VotingApp.Worker.start_link(arg)
      # {VotingApp.Worker, arg},
      {Absinthe.Subscription, [MyWeb.Endpoint]}
    ]

The error produced

** (Mix) Could not start application voting_app: exited in: VotingApp.Application.start(:normal, [])
    ** (EXIT) an exception was raised:
        ** (ArgumentError) The module Absinthe.Subscription was given as a child to a supervisor
but it does not implement child_spec/1.

If you own the given module, please define a child_spec/1 function
that receives an argument and returns a child specification as a map.
For example:

    def child_spec(opts) do
      %{
        id: __MODULE__,
        start: {__MODULE__, :start_link, [opts]},
        type: :worker,
        restart: :permanent,
        shutdown: 500
      }
    end

Note that "use Agent", "use GenServer" and so on automatically define
this function for you.

However, if you don't own the given module and it doesn't implement
child_spec/1, instead of passing the module name directly as a supervisor
child, you will have to pass a child specification as a map:

    %{
      id: Absinthe.Subscription,
      start: {Absinthe.Subscription, :start_link, [arg1, arg2]}
    }

See the Supervisor documentation for more information.

            (elixir) lib/supervisor.ex:639: Supervisor.init_child/1
            (elixir) lib/enum.ex:1314: Enum."-map/2-lists^map/1-0-"/2
            (elixir) lib/enum.ex:1314: Enum."-map/2-lists^map/1-0-"/2
            (elixir) lib/supervisor.ex:625: Supervisor.init/2
            (elixir) lib/supervisor.ex:576: Supervisor.start_link/2
            (kernel) application_master.erl:277: :application_master.start_it_old/4

The reason is right in the error message:

** (ArgumentError) The module Absinthe.Subscription was given as a child to a supervisor but it does not implement child_spec/1.

So Absinthe.Subscription hasn’t been updated to implement child_spec (a change which I think around the Elixir 1.6 timeframe?). You will have to provide your own child spec.

To do that, create a function:

  def absinthe_subscriptions(name) do
    %{
      type: :supervisor,
      id: Absinthe.Subscription,
      start: {Absinthe.Subscription, :start_link, [name]}
    }
  end

And then in your supervisor’s child array:

    children = [
      {
      … other children here …
      absinthe_subscriptions(MyWeb.Endpoint)
    ]
5 Likes

This will be fixed shortly, apologies!