cloudcreativity / laravel-json-api

JSON API (jsonapi.org) package for Laravel applications.
http://laravel-json-api.readthedocs.io/en/latest/
Apache License 2.0
780 stars 109 forks source link

Is type really recommended in plural? #454

Closed iwasherefirst2 closed 4 years ago

iwasherefirst2 commented 4 years ago

In the docs and in the tutorial blog it seems that the schema type is always set in plural. Also if you use php artisan make:json-api:schema dish it will create type dishes.

This causes every single item to be of a plural type. Like here, every single dish is of type dishes which does not sound convenient to me:


{
  "data": [
    {
      "type": "dishes",
      "id": "1",
      "attributes": {
        "name": "Volcano Roll",
        "rating": 3,
        "created-at": "2019-06-05T11:44:24+00:00",
        "updated-at": "2019-06-05T11:44:24+00:00"
      },
      "relationships": {
        "restaurant": {
          "links": {
            "self": "http://opinion-ate.test/api/v1/dishes/1/relationships/restaurant",
            "related": "http://opinion-ate.test/api/v1/dishes/1/restaurant"
          }
        }
      },
      "links": {
        "self": "http://opinion-ate.test/api/v1/dishes/1"
      }
    },
    {
      "type": "dishes",
      "id": "2",
      "attributes": {
        "name": "Salmon Nigiri",
        "rating": 4,
        "created-at": "2019-06-05T11:44:24+00:00",
        "updated-at": "2019-06-05T11:44:24+00:00"
      },
      "relationships": {
        "restaurant": {
          "links": {
            "self": "http://opinion-ate.test/api/v1/dishes/2/relationships/restaurant",
            "related": "http://opinion-ate.test/api/v1/dishes/2/restaurant"
          }
        }
      },
      "links": {
        "self": "http://opinion-ate.test/api/v1/dishes/2"
      }
    }
  ]
}

In this Laravel JSON:API client package the type is put in singular.

Is there any special reason behind setting the type in plural?

lindyhopchris commented 4 years ago

It's easy to rename the type to singular - i.e. the package fully works if you opt to use singular. It's subjective whether people want to use plural or singular. The logic for using plural is it makes more sense in the URL: /api/dishes/1 vs /api/dish/1.

We opted to use plural as default because:

  1. the JSON API docs use plural throughout, so our docs make sense in conjunction with reading the spec docs;
  2. we always work with Ember.js on the frontend, which also expects plural as the default.