Skip to content

Commit 07bd259

Browse files
committed
reduce complexity of extend-express-app example
1 parent 1b55b41 commit 07bd259

File tree

2 files changed

+30
-63
lines changed

2 files changed

+30
-63
lines changed

examples/extend-express-app/keystone.ts

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
import { config } from '@keystone-6/core'
2-
import type { Request, Response } from 'express'
32

43
import { fixPrismaPath } from '../example-utils'
54
import { lists } from './schema'
6-
import { getTasks } from './routes/tasks'
7-
import { type TypeInfo, type Context } from '.keystone/types'
8-
9-
function withContext<F extends (req: Request, res: Response, context: Context) => void>(
10-
commonContext: Context,
11-
f: F
12-
) {
13-
return async (req: Request, res: Response) => {
14-
return f(req, res, await commonContext.withRequest(req, res))
15-
}
16-
}
5+
import {
6+
type TypeInfo,
7+
} from '.keystone/types'
178

189
export default config<TypeInfo>({
1910
db: {
@@ -24,18 +15,34 @@ export default config<TypeInfo>({
2415
...fixPrismaPath,
2516
},
2617
server: {
27-
/*
28-
This is the main part of this example. Here we include a function that
29-
takes the express app Keystone created, and does two things:
30-
- Adds a middleware function that will run on requests matching our REST
31-
API routes, to get a keystone context on `req`. This means we don't
32-
need to put our route handlers in a closure and repeat it for each.
33-
- Adds a GET handler for tasks, which will query for tasks in the
34-
Keystone schema and return the results as JSON
35-
*/
3618
extendExpressApp: (app, commonContext) => {
37-
app.get('/rest/tasks', withContext(commonContext, getTasks))
38-
// app.put('/rest/tasks', withContext(commonContext, putTask));
19+
// this example HTTP GET route retrieves any tasks in the database for your context
20+
// using a request query parameter of `complete=true` or `complete=false`
21+
// returning them as JSON
22+
app.get('/rest/tasks', async (req, res) => {
23+
const context = await commonContext.withRequest(req, res)
24+
25+
const isComplete = req.query?.complete === 'true'
26+
const tasks = await context.query.Task.findMany({
27+
where: {
28+
isComplete: {
29+
equals: isComplete
30+
},
31+
},
32+
query: `
33+
id
34+
label
35+
priority
36+
isComplete
37+
assignedTo {
38+
id
39+
name
40+
}
41+
`,
42+
})
43+
44+
res.json(tasks)
45+
})
3946
},
4047
},
4148
lists,

examples/extend-express-app/routes/tasks.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)