DEV Community

Eddie
Eddie

Posted on

How to structure API for creating content with parent/child relationship?

I need some advice on deciding which endpoint to use in order to create an article and have that article be assigned to a publication.

Say I have two API endpoints:

  1. /v1/publications
  2. /v1/articles

If I want to create a new article and have it automatically assigned to a publication, should I:

  1. Make the call to POST /v1/publications/article and have that endpoint create the new article as well as its assignment to the specified publication or
  2. Make the call to POST /v1/articles to create the new article and have that endpoint assign the new article to the publication

Ex:

Publications.createArticle(publicationId);
// creates a new article
// publication id is required and auto assigns the new article to publication id
Enter fullscreen mode Exit fullscreen mode

vs.

Articles.create(publicationId);
// Creates a new article
// publication id is optional. If it exists, assign it to the publication
// If it does not exist, just create the article
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
phlash profile image
Phil Ashby

Critical details, hinted at by Kasey - lifecycles.

Can articles exist by themselves? Is a publication a collection of articles that has it's own lifecycle? Can a publication be "empty" of articles?

Is the relationship / process of "publishing" articles missing?

Answering these will hopefully give you a better "real world" model of what you are trying to achieve, and thus a better API design - fun innit?

Collapse
 
anwar_nairi profile image
Anwar • Edited

I completely agree with Phil and Kasey. Jumping right into it so it would IMHO be modelized like this:

  • 1 publications can contain/regroup/? (find a better word to fit the business need) many articles
  • 1 article is required to be (make it less strict if needed) attached to one publication

Below is the according REST endpoint you could produce (GET, POST).

GET /v1/publication - list of publications 
GET /v1/publication/{publicationId} - detail of the publication 
GET /v1/publication/{publicationId}/article - list of all articles of the publication 

POST /v1/publication/{publicationId}/article - create a new article and attach it to the publication 

Hope it helps?

Collapse
 
kspeakman profile image
Kasey Speakman • Edited

Forgive me -- the linked post is not a direct answer, but I think gets at the heart of the issue.

Things that happen in relationships are the things that matter.

Following the advice there, I would say that the relationship between the article and the publication is an entity itself. If your REST API represents essentially database calls, then it would be two operations: one on article and one on the article-to-publication link entity.

Edit: I suppose with this answer I was also assuming something different than the title. That it is potentially a many-to-many relationship between article and publication. If it is a 1-to-many relationship from publication to article, then it seems fair that article could be a sub-entity of publication.