ruby-on-railsreactjsapiaxiosgrape-api

complex API vs simple API endpoint


I have a backend Rails application that is mainly API endpoints and a front-end application that is mainly being built in React.

Consider the following models:

  1. Panel: is an container element inside a dashboard. It defines dimensions/positions inside the dashboard. It also defines its content using a content_type and a content_id (polymorphic associations).
  2. Chart / SingleValue / Section: these are all models that can be used as content for the panel.

What I would like to do now is letting the user create a "chart panel" in one step.

I've been wondering for a while now if it is better to:

  1. Have all the basic CRUD API end-points and manage the panel+chart creation 100% on the front-end. This would mean more complexity on the front-end but fewer API end-points.
  2. Have an extra API endpoint on the Rails side to create panel+chart/panel+single_value/panel+section (atomic operation). This would mean much less complexity on the front-end but more API end-points.

What would be the ideal approach?


Solution

  • Maybe perform a single POST request to /panels/ for each panel creating with the following request body (Given that every panel needs a content):

    {
      dimensions: '',
      positions: '',
      content: {
        type: 'chart', // singleValue, section
        // content attributes
      }
    }
    

    My suggestion is that Panel should be a centralized component (which parses dimensions and positions) and for each children you render them according content.type. You will be able to use a single endpoint and still keep RESTFul standards.