Skip to content

Commit 3240bd5

Browse files
authored
Reduce complexity of extend-express-app example (#9068)
1 parent 1b55b41 commit 3240bd5

File tree

8 files changed

+119
-316
lines changed

8 files changed

+119
-316
lines changed

examples/custom-session/keystone.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const sillySessionStrategy = {
3535
export default config<TypeInfo>({
3636
db: {
3737
provider: 'sqlite',
38-
url: process.env.DATABASE_URL || 'file:./keystone-example.db',
38+
url: process.env.DATABASE_URL ?? 'file:./keystone-example.db',
3939

4040
// WARNING: this is only needed for our monorepo examples, dont do this
4141
...fixPrismaPath,

examples/extend-express-app/keystone.ts

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
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'
8+
9+
// WARNING: this example is for demonstration purposes only
10+
// as with each of our examples, it has not been vetted
11+
// or tested for any particular usage
1712

1813
export default config<TypeInfo>({
1914
db: {
@@ -24,18 +19,64 @@ export default config<TypeInfo>({
2419
...fixPrismaPath,
2520
},
2621
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-
*/
3622
extendExpressApp: (app, commonContext) => {
37-
app.get('/rest/tasks', withContext(commonContext, getTasks))
38-
// app.put('/rest/tasks', withContext(commonContext, putTask));
23+
// this example HTTP GET handler retrieves any posts in the database for your context
24+
// with an optional request query parameter of `draft=1`
25+
// returning them as JSON
26+
//
27+
// e.g
28+
// http://localhost:3000/rest/posts
29+
// http://localhost:3000/rest/posts?draft=1
30+
//
31+
app.get('/rest/posts', async (req, res) => {
32+
const context = await commonContext.withRequest(req, res)
33+
// if (!context.session) return res.status(401).end()
34+
35+
const isDraft = req.query?.draft === '1'
36+
const tasks = await context.query.Post.findMany({
37+
where: {
38+
draft: {
39+
equals: isDraft
40+
},
41+
},
42+
query: `
43+
id
44+
title
45+
content
46+
`,
47+
})
48+
49+
res.json(tasks)
50+
})
51+
},
52+
53+
extendHttpServer: (server, commonContext) => {
54+
// e.g
55+
// http://localhost:3000/rest/posts/clu7x6ch90002a89s6l63bjb5
56+
//
57+
server.on('request', async (req, res) => {
58+
if (!req.url?.startsWith('/rest/posts/')) return
59+
60+
// this example HTTP GET handler retrieves a post in the database for your context
61+
// returning it as JSON
62+
const context = await commonContext.withRequest(req, res)
63+
// if (!context.session) return res.status(401).end()
64+
65+
const task = await context.query.Post.findOne({
66+
where: {
67+
id: req.url.slice('/rest/posts/'.length)
68+
},
69+
query: `
70+
id
71+
title
72+
content
73+
draft
74+
`,
75+
})
76+
77+
if (!task) return res.writeHead(404).end()
78+
res.writeHead(200).end(JSON.stringify(task))
79+
})
3980
},
4081
},
4182
lists,

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

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

examples/extend-express-app/schema.graphql

Lines changed: 36 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,25 @@
11
# This file is automatically generated by Keystone, do not modify it manually.
22
# Modify your Keystone config when you want to change this.
33

4-
type Task {
4+
type Post {
55
id: ID!
6-
label: String
7-
priority: TaskPriorityType
8-
isComplete: Boolean
9-
assignedTo: Person
10-
finishBy: DateTime
6+
title: String
7+
content: String
8+
draft: Boolean
119
}
1210

13-
enum TaskPriorityType {
14-
low
15-
medium
16-
high
17-
}
18-
19-
scalar DateTime @specifiedBy(url: "https://datatracker.ietf.org/doc/html/rfc3339#section-5.6")
20-
21-
input TaskWhereUniqueInput {
11+
input PostWhereUniqueInput {
2212
id: ID
2313
}
2414

25-
input TaskWhereInput {
26-
AND: [TaskWhereInput!]
27-
OR: [TaskWhereInput!]
28-
NOT: [TaskWhereInput!]
15+
input PostWhereInput {
16+
AND: [PostWhereInput!]
17+
OR: [PostWhereInput!]
18+
NOT: [PostWhereInput!]
2919
id: IDFilter
30-
label: StringFilter
31-
priority: TaskPriorityTypeNullableFilter
32-
isComplete: BooleanFilter
33-
assignedTo: PersonWhereInput
34-
finishBy: DateTimeNullableFilter
20+
title: StringFilter
21+
content: StringFilter
22+
draft: BooleanFilter
3523
}
3624

3725
input IDFilter {
@@ -73,131 +61,38 @@ input NestedStringFilter {
7361
not: NestedStringFilter
7462
}
7563

76-
input TaskPriorityTypeNullableFilter {
77-
equals: TaskPriorityType
78-
in: [TaskPriorityType!]
79-
notIn: [TaskPriorityType!]
80-
not: TaskPriorityTypeNullableFilter
81-
}
82-
8364
input BooleanFilter {
8465
equals: Boolean
8566
not: BooleanFilter
8667
}
8768

88-
input DateTimeNullableFilter {
89-
equals: DateTime
90-
in: [DateTime!]
91-
notIn: [DateTime!]
92-
lt: DateTime
93-
lte: DateTime
94-
gt: DateTime
95-
gte: DateTime
96-
not: DateTimeNullableFilter
97-
}
98-
99-
input TaskOrderByInput {
69+
input PostOrderByInput {
10070
id: OrderDirection
101-
label: OrderDirection
102-
priority: OrderDirection
103-
isComplete: OrderDirection
104-
finishBy: OrderDirection
71+
title: OrderDirection
72+
content: OrderDirection
73+
draft: OrderDirection
10574
}
10675

10776
enum OrderDirection {
10877
asc
10978
desc
11079
}
11180

112-
input TaskUpdateInput {
113-
label: String
114-
priority: TaskPriorityType
115-
isComplete: Boolean
116-
assignedTo: PersonRelateToOneForUpdateInput
117-
finishBy: DateTime
118-
}
119-
120-
input PersonRelateToOneForUpdateInput {
121-
create: PersonCreateInput
122-
connect: PersonWhereUniqueInput
123-
disconnect: Boolean
124-
}
125-
126-
input TaskUpdateArgs {
127-
where: TaskWhereUniqueInput!
128-
data: TaskUpdateInput!
129-
}
130-
131-
input TaskCreateInput {
132-
label: String
133-
priority: TaskPriorityType
134-
isComplete: Boolean
135-
assignedTo: PersonRelateToOneForCreateInput
136-
finishBy: DateTime
137-
}
138-
139-
input PersonRelateToOneForCreateInput {
140-
create: PersonCreateInput
141-
connect: PersonWhereUniqueInput
142-
}
143-
144-
type Person {
145-
id: ID!
146-
name: String
147-
tasks(where: TaskWhereInput! = {}, orderBy: [TaskOrderByInput!]! = [], take: Int, skip: Int! = 0, cursor: TaskWhereUniqueInput): [Task!]
148-
tasksCount(where: TaskWhereInput! = {}): Int
149-
}
150-
151-
input PersonWhereUniqueInput {
152-
id: ID
153-
name: String
154-
}
155-
156-
input PersonWhereInput {
157-
AND: [PersonWhereInput!]
158-
OR: [PersonWhereInput!]
159-
NOT: [PersonWhereInput!]
160-
id: IDFilter
161-
name: StringFilter
162-
tasks: TaskManyRelationFilter
163-
}
164-
165-
input TaskManyRelationFilter {
166-
every: TaskWhereInput
167-
some: TaskWhereInput
168-
none: TaskWhereInput
169-
}
170-
171-
input PersonOrderByInput {
172-
id: OrderDirection
173-
name: OrderDirection
174-
}
175-
176-
input PersonUpdateInput {
177-
name: String
178-
tasks: TaskRelateToManyForUpdateInput
179-
}
180-
181-
input TaskRelateToManyForUpdateInput {
182-
disconnect: [TaskWhereUniqueInput!]
183-
set: [TaskWhereUniqueInput!]
184-
create: [TaskCreateInput!]
185-
connect: [TaskWhereUniqueInput!]
186-
}
187-
188-
input PersonUpdateArgs {
189-
where: PersonWhereUniqueInput!
190-
data: PersonUpdateInput!
81+
input PostUpdateInput {
82+
title: String
83+
content: String
84+
draft: Boolean
19185
}
19286

193-
input PersonCreateInput {
194-
name: String
195-
tasks: TaskRelateToManyForCreateInput
87+
input PostUpdateArgs {
88+
where: PostWhereUniqueInput!
89+
data: PostUpdateInput!
19690
}
19791

198-
input TaskRelateToManyForCreateInput {
199-
create: [TaskCreateInput!]
200-
connect: [TaskWhereUniqueInput!]
92+
input PostCreateInput {
93+
title: String
94+
content: String
95+
draft: Boolean
20196
}
20297

20398
"""
@@ -206,27 +101,18 @@ The `JSON` scalar type represents JSON values as specified by [ECMA-404](http://
206101
scalar JSON @specifiedBy(url: "http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf")
207102

208103
type Mutation {
209-
createTask(data: TaskCreateInput!): Task
210-
createTasks(data: [TaskCreateInput!]!): [Task]
211-
updateTask(where: TaskWhereUniqueInput!, data: TaskUpdateInput!): Task
212-
updateTasks(data: [TaskUpdateArgs!]!): [Task]
213-
deleteTask(where: TaskWhereUniqueInput!): Task
214-
deleteTasks(where: [TaskWhereUniqueInput!]!): [Task]
215-
createPerson(data: PersonCreateInput!): Person
216-
createPeople(data: [PersonCreateInput!]!): [Person]
217-
updatePerson(where: PersonWhereUniqueInput!, data: PersonUpdateInput!): Person
218-
updatePeople(data: [PersonUpdateArgs!]!): [Person]
219-
deletePerson(where: PersonWhereUniqueInput!): Person
220-
deletePeople(where: [PersonWhereUniqueInput!]!): [Person]
104+
createPost(data: PostCreateInput!): Post
105+
createPosts(data: [PostCreateInput!]!): [Post]
106+
updatePost(where: PostWhereUniqueInput!, data: PostUpdateInput!): Post
107+
updatePosts(data: [PostUpdateArgs!]!): [Post]
108+
deletePost(where: PostWhereUniqueInput!): Post
109+
deletePosts(where: [PostWhereUniqueInput!]!): [Post]
221110
}
222111

223112
type Query {
224-
tasks(where: TaskWhereInput! = {}, orderBy: [TaskOrderByInput!]! = [], take: Int, skip: Int! = 0, cursor: TaskWhereUniqueInput): [Task!]
225-
task(where: TaskWhereUniqueInput!): Task
226-
tasksCount(where: TaskWhereInput! = {}): Int
227-
people(where: PersonWhereInput! = {}, orderBy: [PersonOrderByInput!]! = [], take: Int, skip: Int! = 0, cursor: PersonWhereUniqueInput): [Person!]
228-
person(where: PersonWhereUniqueInput!): Person
229-
peopleCount(where: PersonWhereInput! = {}): Int
113+
posts(where: PostWhereInput! = {}, orderBy: [PostOrderByInput!]! = [], take: Int, skip: Int! = 0, cursor: PostWhereUniqueInput): [Post!]
114+
post(where: PostWhereUniqueInput!): Post
115+
postsCount(where: PostWhereInput! = {}): Int
230116
keystone: KeystoneMeta!
231117
}
232118

0 commit comments

Comments
 (0)