How to deal best with relationship in contained list item


#1

Hi,

I have a design question related to a contained list within a resource.

The root resource is a recipe resource containing a list of ingredients.

In a traditional json representation this could look like:

{ 
  "title": "Baked potatoes",
  "ingredients: ["potatoes", "salt"]
}

But extending on this we are creating s specific ingredient resource to re-use.
And we want to add quantity of the ingredient to the recipe, something like:

{ 
  "title": "Baked potatoes",
  "ingredients: [
    { "name": "potatoe", "quantity": "1 kg" },
    { "name": "salt", "quantity": "10 gr" }
  ]
}

The ingredients collection within the recipe is not a resource as on it’s own it does not exist.
Technically in the datastore there will be something like recipe_ingredient but not in the resource model.

How would this look like with jsonapi?
Basically I want to have a relationship for each recipe ingredient to an actual ingredient resource.


#2

Take a look here where there is a great answer. Do note my follow up post just after.