0

Relationships

  • robot has many brains
  • brain has one robot

Background

How to form the resource URL where we provide a robotId (foreign key) to retrieve its brain?

I could come up with this resource: GET /robots/:robotId/brain

I am not sure if using brain in singular is against REST conventions and practices. However, using GET /robots/:robotId/brains (brain in plurals) implies a collection will be returned but it will always have 1 item only.

Question

Can you advise me on a RESTful way?

2
  • If your endpoint retrieves a robot won't that also get the brain(s) associated with the robot ? Commented Jun 12, 2020 at 14:50
  • @auburg Nope, it will not. I used some model resources. A brain is identified via a foreign key robotId. Commented Jun 12, 2020 at 14:56

4 Answers 4

1

Can you advise me on a RESTful way?

REST doesn't care what spelling conventions you use for your resource identifiers.

Therefore, you should use whatever spellings make sense within your local context. That might mean, for your own convenience, that the spelling conventions that you use for your path segments are similar to those that you use when naming collections/tables in your data store. Or perhaps not - you could equally decide that, because the audiences differ, so too should the spelling conventions.

GET /robots/:robotId/brain GET /robots/:robotId/brains GET /brains/:robotId GET /ee4fcf74-d494-4f90-8964-9e4d65aa61ef 

These are all fine.

Stefan Tilkov's 2014 talk: REST: I don't Think it Means What You Think it Does may be helpful.

Sign up to request clarification or add additional context in comments.

1 Comment

I've picked this answer as correct as it helped me to understand the issue most. I'll go with GET /robots/:robotId/brain because of the one-to-many relationship.
0

For me, you should have only 1 endpoint GET /robots/:robotId/brains, you will get all your collection, but your frontend have to processing your data like he want

2 Comments

Do you suggest that by calling GET /robots/:robotId/brains we always receive an empty collection [ ] or a collection with 1 brain [ {id: 1, robotId: '123', intellect: 100}]?
You receive a collection with all the robot brains, create a new endpoint like GET /brains/:brainId if you want to get a specific brain
0

If you have GET /robots/:robotId/brains this can still return a collection of size 1?

1 Comment

Yeah, it can but I'd like to get the brain directly.
0

You should have a rest end point which gives you the collection of all the brains of a robot. The uri should look like this :

GET /robots/:robotId/brains

Number of items the collection has should not matter.

If the rest end point is GET /robots/:robotId/brain, you are very much ignoring the fact that a robot might have multiple brains in the future and if very much possible if you database supports a one to many relationship.

To get 1 brain of a robot you can always keep scope for the below rest uri: GET /robots/:robotId/brains/:brainId Where brainId is the unique/primary key for a brain.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.