I am new to REST API design. While designing the API, I got confused with resource relationships and need suggestions.
My API has a workflow and each workflow will have a task. I am not able to decide between keeping task as an independent resource or using it as a sub-resource. More precisely:
I went through some of the stack overflow links. Some say, queryString works as a filter and should be optional favoring the use of subresource while others prefer keeping tasks as an independent resource and making the api url small.
I feel there is yet another way where we can keep the taskId and workflowId mapping in our database and let the user use:
But this seems like a big effort maintaining the mapping.
There is no such thing as a 'subresource' in REST. Everything is its own resource. That said, it's a good idea to come up with urls that are meaningful to the user, and also convenient for yourself.
If tasks are always part of a workflow, there is nothing wrong with:
But this is also completely fine:
This definitely looks unusual to me:
Because what happens if I don't pass a workflowId? Generally query parameters are indeed used for searches. I can't say if this is necessarily unrestful, but it seems like a bad idea.
So between the first two there is no difference in terms of what's objectively better, or more restful. Clients shouldn't guess the url anyway, they should always be discovered.
I would personally go with the deeper structure because I think the urls are more useful and descriptive that way; but this is subjective.