loopbackio / loopback-next

LoopBack makes it easy to build modern API applications that require complex integrations.
https://loopback.io
Other
4.95k stars 1.07k forks source link

[EPIC] From relation definition to REST API with auto-generated repository/controller classes #2483

Closed bajtos closed 3 years ago

bajtos commented 5 years ago

In LoopBack 3, it is very easy to get a fully-featured relational REST API with very little code: a model definition describing model relations + a model configuration specifying which datasource to use.

Let's enhance the solution provided by #2036 to include support for relations too.

Acceptance criteria

jannyHou commented 5 years ago

This story may need a spike to figure out how to map the relational paths to REST endpoints.

Reference story: Hierarchical resource locator: https://github.com/strongloop/loopback-next/issues/802

/Users/{id}/orders

Proposal: Delegate the url resolve to controller, instead of defining the path pattern in relation controller

jannyHou commented 5 years ago

@jannyHou will create a spike.

dhmlau commented 5 years ago

@jannyHou, could you please create a spike? (I thought I've created one but might've mixed it up with something else). Thanks.

jannyHou commented 5 years ago

Spike created in https://github.com/strongloop/loopback-next/issues/2855 πŸ™‚

bajtos commented 5 years ago

We should be using the same approach we already have in place for relations, where a new controller class is created for every relation. E.g. UserOrdersController to handle all endpoints in /Users/{id}/orders path space.

dhmlau commented 5 years ago

Per the discussion during the estimation meeting yesterday, @raymondfeng suggested to make this task as an epic and start off with one relation first.
@bajtos, if you agree with this approach, @agnes512 can make this as epic and create a task with hasMany or belongsTo as the first one to tackle.

cc @agnes512

bajtos commented 5 years ago

+1 to convert this task into an epic.

Let's start with a spike tasks first. Based on the work done so far, I am not sure at all about the final design, I am anticipating several obstacles that we will have to research a solution for.

dhmlau commented 5 years ago

@agnes512, could you please take care of that? ^^ Thanks!

emonddr commented 5 years ago

Added one spike task #3896 [SPIKE] HasMany relation definition to REST API with auto-generated repository/controller classes to get the ball rolling.

Once we figure out what needs to be done with the hasMany relation, it should give us a pretty good understanding of the work required for the other relations.

emonddr commented 5 years ago

@dhmlau , @bajtos . Please let me know your thoughts. thx.

jannyHou commented 5 years ago

@emonddr for #2855 and #3896 I think we only need one spike, yours focuses on picking one relation type as the starting point, mine focuses on figuring out a solution for the path resolving. Let me merge mine to #3896, then discuss and find a solution for the "hasMany" relational endpoints.

bajtos commented 5 years ago

Added one spike task #3896 [SPIKE] HasMany relation definition to REST API with auto-generated repository/controller classes to get the ball rolling.

Once we figure out what needs to be done with the hasMany relation, it should give us a pretty good understanding of the work required for the other relations.

πŸ‘ πŸ’―

bajtos commented 3 years ago

Ping back from https://stackoverflow.com/a/64859564/69868, cross-posting my answer:

Essentially, there are two parts of the problem:

  1. Create repository artifacts for navigating to related models.

    This is IMO the easier part, since all you need to do is take the class constructor created by defineCrudRepositoryClass for the model you are trying to expose via REST API and create a subclass that will call methods like createHasManyRepositoryFactoryFor and registerInclusionResolver from the constructor.

  2. Define a controller class implementing relation-specific REST API endpoints.

    This is slightly more involved as you need to implement function that will define a new controller class with dynamically generated OpenAPI metadata for each model relation you want to expose via REST API. I would start with copy and paste of defineCrudRestController function and then reworking individual endpoints to provide navigational API for your relation. You should probably create multiple define{relation}Controller functions, one for each relation type (e.g. defineHasManyController).

stale[bot] commented 3 years ago

This issue has been marked stale because it has not seen activity within six months. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository. This issue will be closed within 30 days of being stale.

stale[bot] commented 3 years ago

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.