Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: document the request-response cycle #4914

Merged
merged 1 commit into from
Mar 25, 2020
Merged

Conversation

hacksparrow
Copy link
Contributor

@hacksparrow hacksparrow commented Mar 18, 2020

Document the LB4 request-response cycle.
Closes #4836

Signed-off-by: Yaapa Hage [email protected]

Checklist

👉 Read and sign the CLA (Contributor License Agreement) 👈

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

👉 Check out how to submit a PR 👈

@hacksparrow hacksparrow self-assigned this Mar 18, 2020
@hacksparrow hacksparrow marked this pull request as ready for review March 20, 2020 14:42
@hacksparrow hacksparrow force-pushed the docs/req-res-cycle branch 2 times, most recently from cad2d74 to a2ced75 Compare March 20, 2020 15:58
@raymondfeng
Copy link
Contributor

raymondfeng commented Mar 20, 2020

@hacksparrow It's a great start.

I would like to see more information, such as:

  1. Which tier of the request/response trip is extensible/configurable?

  2. How to register an Express middleware? Is it possible?

  3. How to add extensions to various tiers for different use cases? For example:

    • authentication is enforced by an action in the sequence
    • authorization is performed inside interceptors
    • request body parsers
    • ajv validation
    • what about caching, rate-limiting, metrics, logging?
    • error handling
  4. More zoom-ins to various tiers to provide details.

@raymondfeng
Copy link
Contributor

This diagram can be useful too - https://loopback.io/pages/en/lb4/imgs/loopback-overview.png

@raymondfeng
Copy link
Contributor

A few other things:

  • How to access request/response objects
  • How to propagate contextual information via DI

Copy link
Contributor

@agnes512 agnes512 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice writeup. I like the walk through part. And the diagram helps to understand the cycle. Just have a question about the non-controller endpoint part.

docs/site/Req-res-cycle.md Outdated Show resolved Hide resolved
docs/site/Req-res-cycle.md Outdated Show resolved Hide resolved
docs/site/Req-res-cycle.md Outdated Show resolved Hide resolved
Comment on lines 213 to 278
Requests to non-controller endpoints are also intercepted by the app's sequence,
but the control is handed over to the underlying Express middleware from the
`InvokeMethod` phase, and never make it to `Send` unless the middleware calls
`next()`. Unhandled error from Express middleware still reach `Reject`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how we show the differences between these two endpoints. But I am a bit confused about services and the interceptor part. From my understanding, the structure we show above is:

             / ==> controller endpoint -> Services, Interceptors -> repositories
Sequence ---
             \ ==> non-controller endpoint

But IIUC I think the interceptors can also be use with non-controller endpoints. Maybe we make it more clear here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you point out where this structure is contradicted?

             / ==> controller endpoint -> Services, Interceptors -> repositories
Sequence ---
             \ ==> non-controller endpoint

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the interceptors cannot be used in non-controller endpoints. Is it true?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interceptors work only on Controllers and Repositories, so non-controller endpoint handlers can't be intercepted.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
I think I misunderstood the hander in the diagram as the non-controller endpoints.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works on Services too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, global interceptors also intercept non-controller routes.

@hacksparrow hacksparrow force-pushed the docs/req-res-cycle branch 5 times, most recently from 3218a8b to 4d24da2 Compare March 23, 2020 14:15
@dhmlau
Copy link
Member

dhmlau commented Mar 24, 2020

@hacksparrow , would this PR closes #1833 instead of Closes #4836, if this PR is only about LB4 req/res cycle?

@dhmlau dhmlau added this to the Mar 2020 milestone Mar 24, 2020
### The request-response cycle

The request-response cycle involves many components, especially if the request
is to a controller-endpoint which interacts with a model.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the request is to a controller-endpoint -> the request is coming from a controller-endpoint?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made it "interacts with the database".

@hacksparrow
Copy link
Contributor Author

@dhmlau only LB4 req-res.

@hacksparrow
Copy link
Contributor Author

@raymondfeng I have updated the PR with your inputs. However, since this PR is only for the flow of the request and the response, I have only mentioned your points from a high-level perspective.

Your points should be covered in another doc, maybe titled "Writing extensions for the request-response cycle".

@@ -0,0 +1,278 @@
---
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: let's name the file as Request-response-cycle.md.

title: 'Request-response cycle'
keywords: LoopBack 4.0, LoopBack 4
sidebar: lb4_sidebar
permalink: /doc/en/lb4/Application.html
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong html link. It should be /doc/en/lb4/Request-response-cycle.html.

taken by a request, to see how it makes its way through the various parts of the
framework to return a result.

### Setting up the request-responses infrastruture
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request-responses -> request/response

permalink: /doc/en/lb4/Application.html
---

## The LoopBack 4 request-response cycle
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe request/response?


#### The sequence

The sequence is a simple class with five injected helper methods. These five
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with five injected helper methods -> with five injected helper methods by default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made it five injected helper methods in the constructor.

Now this is a step-wise sequence of what happens when a request is made to
`http://localhost:4000/ping`.

1. The request is intercepted by `MySequence`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is intercepted by -> 'is dispatched to MySequence by the http server`?

`http://localhost:4000/ping`.

1. The request is intercepted by `MySequence`.
2. The route is then identified by `FindRoute`. There is a handler for this request.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on the verb and path defined by OpenAPI decorators?

2. The route is then identified by `FindRoute`. There is a handler for this request.
3. `ParseParams` then tries to parses any parameters might have been submitted.
There is none.
4. `InvokeMethod` then invokes the handler with empty parameters. It returns
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty parameters -> resolved parameters which is empty in this case?

4. `InvokeMethod` then invokes the handler with empty parameters. It returns
whatever the handler returns, in this case, it is JSON object with `greeting`,
`date`, `url`, and `headers` properties.
5. `Send` then sends this JSON object back to the client.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's mention error handling here for Reject.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added: 6. Reject is in place to catch any errors that might be thrown in any of the steps above.

@hacksparrow
Copy link
Contributor Author

@raymondfeng applied all your feedback. PTAL.

@raymondfeng
Copy link
Contributor

@hacksparrow Please fix the formatting error with npm run prettier:fix.

Document the LB4 request-response cycle.

Signed-off-by: Yaapa Hage <[email protected]>
@dhmlau dhmlau merged commit 413c2a9 into master Mar 25, 2020
@dhmlau dhmlau deleted the docs/req-res-cycle branch March 25, 2020 18:38
@dhmlau
Copy link
Member

dhmlau commented Mar 25, 2020

Merging the PR per @hacksparrow's request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Migration Guide: Request-response cycle
4 participants