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

Remove unsafe types from examples/extend-express-app example #8839

Merged
merged 1 commit into from
Sep 26, 2023

Conversation

dcousens
Copy link
Member

@dcousens dcousens commented Sep 26, 2023

This pull request removes the (req as any).context types from the extend-express-app example, and instead prefers to pass a refined Context directly to the route functions.

This new pattern reduces the number of req as casts, while still keeping your root express router definition as minimal as possible.

@dcousens dcousens requested a review from borisno2 September 26, 2023 04:50
@dcousens dcousens self-assigned this Sep 26, 2023
@@ -23,18 +34,8 @@ export default config<TypeInfo>({
Keystone schema and return the results as JSON
*/
extendExpressApp: (app, commonContext) => {
app.use('/rest', async (req, res, next) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

We don't want to call withRequest for other HTTP requests, except for GET /rest/tasks, thereby we shouldn't use app.use.

@@ -11,10 +11,7 @@ import type { Context } from '.keystone/types';
We're also demonstrating how you can query related data through the schema.
*/

export async function getTasks(req: Request, res: Response) {
// This was added by the context middleware in ../keystone.ts
const { context } = req as typeof req & { context: Context };
Copy link
Member Author

Choose a reason for hiding this comment

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

We don't want to encourage as in user code, the preferred approach is to use a Context parameter directly

@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit f5d0199:

Sandbox Source
@keystone-6/sandbox Configuration

@dcousens
Copy link
Member Author

dcousens commented Sep 26, 2023

In the future, we might recommend removal of extendExpressApp, and instead, for anyone who wants to use express themselves, they could opt for an approach similar to how we recommend using astro or nextjs, with getContext directly.

That needs feedback from the community though, and would probably be quite the controversial breaking change.

@dcousens dcousens enabled auto-merge (squash) September 26, 2023 05:02
@dcousens dcousens merged commit 204c1df into main Sep 26, 2023
57 checks passed
@dcousens dcousens deleted the extend-express-app branch September 26, 2023 05:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants