How to design app with JSON API and HTML interface?

Hi, I’m fairly new to Phoenix and have a design question.

I have a project that has two components, a JSON API and an HTML interface. They both share one common database.

One thing I could do is to have an umbrella application with two separate apps in it. I am also considering to have just one application and use two separate controllers.

What is the best practice and how would you organize the code for maintainability?

1 Like

The router file already allows You to separate multiple pipelines. One for the browser, one for api.

  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 "/api", BlahWeb do
    pipe_through :api
    
    scope "/v1" do
      # versioned API routes
    end
  end

  scope "/", BlahWeb do
    pipe_through :browser # Use the default browser stack

    # HTML routes
  end

Then, create controllers/api/v1/… for api

And define HTML controllers as well

Not to mention that all code above could be replaced by one line…

forward “/graphql”, Absinthe.Plug, schema: BlahWeb.Schema

Now that the Absinthe book is out, and GraphQL has been open sourced by FB, 2 reasons to try it :slight_smile:

Further to what Koko has said, I think it depends on your approach to the project - are you following something like the Replaceable Component Architecture? If so you might want to check out PragDave’s course in which he uses two separate (Phoenix) apps/components in the app - one for the html layer and one for web sockets/the spa :slight_smile: