diff --git a/examples/extend-express-app/keystone.ts b/examples/extend-express-app/keystone.ts index f7637800eeb..ab09f989627 100644 --- a/examples/extend-express-app/keystone.ts +++ b/examples/extend-express-app/keystone.ts @@ -1,19 +1,10 @@ import { config } from '@keystone-6/core' -import type { Request, Response } from 'express' import { fixPrismaPath } from '../example-utils' import { lists } from './schema' -import { getTasks } from './routes/tasks' -import { type TypeInfo, type Context } from '.keystone/types' - -function withContext void>( - commonContext: Context, - f: F -) { - return async (req: Request, res: Response) => { - return f(req, res, await commonContext.withRequest(req, res)) - } -} +import { + type TypeInfo, +} from '.keystone/types' export default config({ db: { @@ -24,18 +15,34 @@ export default config({ ...fixPrismaPath, }, server: { - /* - This is the main part of this example. Here we include a function that - takes the express app Keystone created, and does two things: - - Adds a middleware function that will run on requests matching our REST - API routes, to get a keystone context on `req`. This means we don't - need to put our route handlers in a closure and repeat it for each. - - Adds a GET handler for tasks, which will query for tasks in the - Keystone schema and return the results as JSON - */ extendExpressApp: (app, commonContext) => { - app.get('/rest/tasks', withContext(commonContext, getTasks)) - // app.put('/rest/tasks', withContext(commonContext, putTask)); + // this example HTTP GET route retrieves any tasks in the database for your context + // using a request query parameter of `complete=true` or `complete=false` + // returning them as JSON + app.get('/rest/tasks', async (req, res) => { + const context = await commonContext.withRequest(req, res) + + const isComplete = req.query?.complete === 'true' + const tasks = await context.query.Task.findMany({ + where: { + isComplete: { + equals: isComplete + }, + }, + query: ` + id + label + priority + isComplete + assignedTo { + id + name + } + `, + }) + + res.json(tasks) + }) }, }, lists, diff --git a/examples/extend-express-app/routes/tasks.ts b/examples/extend-express-app/routes/tasks.ts deleted file mode 100644 index 67edbf2117e..00000000000 --- a/examples/extend-express-app/routes/tasks.ts +++ /dev/null @@ -1,40 +0,0 @@ -import type { Request, Response } from 'express' -import type { Context } from '.keystone/types' - -/* - This example route handler gets all the tasks in the database and returns - them as JSON data, emulating what you'd normally do in a REST API. - - More sophisticated API routes might accept query params to select fields, - map more params to `where` arguments, add pagination support, etc. - - We're also demonstrating how you can query related data through the schema. -*/ - -export async function getTasks (req: Request, res: Response, context: Context) { - // Let's map the `complete` query param to a where filter - let isComplete - if (req.query.complete === 'true') { - isComplete = { equals: true } - } else if (req.query.complete === 'false') { - isComplete = { equals: false } - } - // Now we can use it to query the Keystone Schema - const tasks = await context.query.Task.findMany({ - where: { - isComplete, - }, - query: ` - id - label - priority - isComplete - assignedTo { - id - name - } - `, - }) - // And return the result as JSON - res.json(tasks) -}