Skip to content

Commit fc4ee46

Browse files
committed
feat: pass res to context
1 parent 2abd73d commit fc4ee46

File tree

3 files changed

+233
-11
lines changed

3 files changed

+233
-11
lines changed

src/runtime/app.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as Server from './server'
88
import * as singletonChecks from './singleton-checks'
99
import * as BackingTypes from '../lib/backing-types'
1010
import { write, extractAndWrite } from '../lib/backing-types'
11+
import { Response } from 'express'
1112

1213
const log = Logger.create({ name: 'app' })
1314

@@ -19,7 +20,10 @@ export type Request = HTTP.IncomingMessage & { log: Logger.Logger }
1920
// all places in the framework where the req object is referenced should be
2021
// actually referencing the typegen version, so that it reflects the req +
2122
// plugin augmentations type
22-
type ContextContributor<Req> = (req: Req) => Record<string, unknown>
23+
type ContextContributor<Req, Res> = (
24+
req: Req,
25+
res: Res
26+
) => Record<string, unknown>
2327

2428
export type App = {
2529
/**
@@ -51,8 +55,8 @@ export type App = {
5155
/**
5256
* todo
5357
*/
54-
addToContext: <Req extends any = Request>(
55-
contextContributor: ContextContributor<Req>
58+
addToContext: <Req extends any = Request, Res extends any = Response>(
59+
contextContributor: ContextContributor<Req, Res>
5660
) => void
5761
}
5862
}
@@ -98,7 +102,7 @@ export function create(): App {
98102
// the schema module imports system.
99103
plugins.push(...Plugin.loadAllRuntimePluginsFromPackageJsonSync())
100104

101-
const contextContributors: ContextContributor<any>[] = []
105+
const contextContributors: ContextContributor<any, any>[] = []
102106

103107
const server = Server.create()
104108
const schemaComponent = Schema.create({ plugins })

src/runtime/server.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import createExpress, { Express } from 'express'
1+
import createExpress, { Express, Response } from 'express'
22
import * as ExpressGraphQL from 'express-graphql'
33
import * as GraphQL from 'graphql'
44
import * as HTTP from 'http'
@@ -15,7 +15,7 @@ import * as singletonChecks from './singleton-checks'
1515
const createExpressGraphql = ExpressGraphQL.default
1616

1717
type Request = HTTP.IncomingMessage & { log: Logger.Logger }
18-
type ContextContributor<T extends {}> = (req: Request) => T
18+
type ContextContributor<T extends {}> = (req: Request, res: Response) => T
1919

2020
const log = Logger.create({ name: 'server' })
2121
const resolverLogger = log.child('graphql')
@@ -135,10 +135,10 @@ function setupExpress(
135135

136136
express.use(
137137
'/graphql',
138-
createExpressGraphql(req => {
138+
createExpressGraphql((req, res) => {
139139
return {
140140
...opts,
141-
context: settingsGiven.context(req),
141+
context: settingsGiven.context(req, res),
142142
customFormatErrorFn: error => {
143143
const colorlessMessage = stripAnsi(error.message)
144144

@@ -367,20 +367,22 @@ export function create(): ServerFactory {
367367
}
368368

369369
type AnonymousRequest = Record<string, any>
370+
type AnonymousResponse = Record<string, any>
370371
type AnonymousContext = Record<string, any>
371372

372373
interface ContextCreator<
373374
Req extends AnonymousRequest = AnonymousRequest,
375+
Res extends AnonymousResponse = AnonymousResponse,
374376
Context extends AnonymousContext = AnonymousContext
375377
> {
376-
(req: Req): Context
378+
(req: Req, res: Res): Context
377379
}
378380

379381
function contextFactory(
380382
contextContributors: ContextContributor<any>[],
381383
plugins: Plugin.RuntimeContributions[]
382384
): ContextCreator {
383-
const createContext: ContextCreator = req => {
385+
const createContext: ContextCreator = (req, res) => {
384386
let context: Record<string, any> = {}
385387

386388
// TODO HACK
@@ -399,7 +401,10 @@ function contextFactory(
399401
for (const contextContributor of contextContributors) {
400402
// HACK see req mutation at this func body start
401403
Object.assign(context, {
402-
...contextContributor((req as unknown) as Request),
404+
...contextContributor(
405+
(req as unknown) as Request,
406+
(res as unknown) as Response
407+
),
403408
log: ((req as unknown) as Request).log,
404409
})
405410
}

stable.json

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
{
2+
"kind": "ok",
3+
"type": "dry_run",
4+
"data": {
5+
"report": {
6+
"stops": [],
7+
"errors": [],
8+
"passes": [
9+
{
10+
"details": {},
11+
"summary": "You must have npm auth setup to publish to the registrty",
12+
"code": "npm_auth_not_setup"
13+
},
14+
{
15+
"details": {},
16+
"summary": "You must be on the trunk branch",
17+
"code": "must_be_on_trunk"
18+
},
19+
{
20+
"details": {},
21+
"summary": "Your branch must be synced with the remote",
22+
"code": "branch_not_synced_with_remote"
23+
},
24+
{
25+
"details": {},
26+
"summary": "A stable release requires the commit to have no existing stable release",
27+
"code": "commit_already_has_stable_release"
28+
},
29+
{
30+
"details": {},
31+
"summary": "A stable release must have at least one semantic commit",
32+
"code": "series_only_has_meaningless_commits"
33+
}
34+
]
35+
},
36+
"release": {
37+
"bumpType": "minor",
38+
"version": {
39+
"major": 0,
40+
"minor": 12,
41+
"patch": 0,
42+
"vprefix": false,
43+
"version": "0.12.0"
44+
}
45+
},
46+
"commits": [
47+
{
48+
"message": {
49+
"raw": "feat(cli): remove generate cmd (#495)\n\ncloses #493\r\n\r\n- Unclear use-case\r\n- Leaks low-level details to the user\r\n- Exotic/Esoteric feature given the same standing as something used 99.9999% more e.g. `nexus dev` and `nexus build`\r\n- Requires internal maintenance\r\n- Lacks test coverage today\r\n- If user needs it, we'd like to know about the use-case actually",
50+
"parsed": {
51+
"typeKind": "feat",
52+
"type": "feat",
53+
"scope": "cli",
54+
"description": "remove generate cmd (#495)",
55+
"body": "closes #493\n\n- Unclear use-case\r\n- Leaks low-level details to the user\r\n- Exotic/Esoteric feature given the same standing as something used 99.9999% more e.g. `nexus dev` and `nexus build`\r\n- Requires internal maintenance\r\n- Lacks test coverage today\r\n- If user needs it, we'd like to know about the use-case actually",
56+
"footers": [],
57+
"breakingChange": null,
58+
"completesInitialDevelopment": false
59+
}
60+
},
61+
"sha": "2abd73da23fd4564822bdf8c53c7d16b4b758aae",
62+
"nonReleaseTags": [],
63+
"releases": {
64+
"stable": null,
65+
"preview": {
66+
"version": "0.12.0-next.5",
67+
"vprefix": false,
68+
"major": 0,
69+
"minor": 12,
70+
"patch": 0,
71+
"preRelease": { "identifier": "next", "buildNum": 5 }
72+
}
73+
}
74+
},
75+
{
76+
"message": { "raw": "Update issue templates", "parsed": null },
77+
"sha": "2197f8306404e8af97d45ef43c83324b600fe01f",
78+
"nonReleaseTags": [],
79+
"releases": { "stable": null, "preview": null }
80+
},
81+
{
82+
"message": {
83+
"raw": "chore(website): update diagrams",
84+
"parsed": {
85+
"typeKind": "chore",
86+
"type": "chore",
87+
"scope": "website",
88+
"description": "update diagrams",
89+
"body": null,
90+
"footers": [],
91+
"breakingChange": null,
92+
"completesInitialDevelopment": false
93+
}
94+
},
95+
"sha": "47da0b522262876ba34f77404266f2b0a14b3777",
96+
"nonReleaseTags": [],
97+
"releases": { "stable": null, "preview": null }
98+
},
99+
{
100+
"message": {
101+
"raw": "feat(schema): by default nullable outputs (#494)\n\ncloses #484\r\n\r\nBREAKING CHANGE:\r\n\r\n- outputs will be nullable by default now",
102+
"parsed": {
103+
"typeKind": "feat",
104+
"type": "feat",
105+
"scope": "schema",
106+
"description": "by default nullable outputs (#494)",
107+
"body": "closes #484",
108+
"footers": [],
109+
"breakingChange": "- outputs will be nullable by default now",
110+
"completesInitialDevelopment": false
111+
}
112+
},
113+
"sha": "50d3e24de6c8d5cf85319f81d768eb8352bafacd",
114+
"nonReleaseTags": [],
115+
"releases": {
116+
"stable": null,
117+
"preview": {
118+
"version": "0.12.0-next.4",
119+
"vprefix": false,
120+
"major": 0,
121+
"minor": 12,
122+
"patch": 0,
123+
"preRelease": { "identifier": "next", "buildNum": 4 }
124+
}
125+
}
126+
},
127+
{
128+
"message": {
129+
"raw": "feat: run backing types typegen in background (#491)",
130+
"parsed": {
131+
"typeKind": "feat",
132+
"type": "feat",
133+
"scope": null,
134+
"description": "run backing types typegen in background (#491)",
135+
"body": null,
136+
"footers": [],
137+
"breakingChange": null,
138+
"completesInitialDevelopment": false
139+
}
140+
},
141+
"sha": "6d3272a70933cd97005b0b0c02b93db9f36835f2",
142+
"nonReleaseTags": [],
143+
"releases": {
144+
"stable": null,
145+
"preview": {
146+
"version": "0.12.0-next.3",
147+
"vprefix": false,
148+
"major": 0,
149+
"minor": 12,
150+
"patch": 0,
151+
"preRelease": { "identifier": "next", "buildNum": 3 }
152+
}
153+
}
154+
},
155+
{
156+
"message": {
157+
"raw": "refactor: isolate addToContext typegen constant (#492)",
158+
"parsed": {
159+
"typeKind": "other",
160+
"type": "refactor",
161+
"scope": null,
162+
"description": "isolate addToContext typegen constant (#492)",
163+
"body": null,
164+
"footers": [],
165+
"breakingChange": null,
166+
"completesInitialDevelopment": false
167+
}
168+
},
169+
"sha": "fb00bfccf2c4a191e6cb4b167afa385f7cd5d680",
170+
"nonReleaseTags": [],
171+
"releases": {
172+
"stable": null,
173+
"preview": {
174+
"version": "0.11.3-next.2",
175+
"vprefix": false,
176+
"major": 0,
177+
"minor": 11,
178+
"patch": 3,
179+
"preRelease": { "identifier": "next", "buildNum": 2 }
180+
}
181+
}
182+
},
183+
{
184+
"message": {
185+
"raw": "tests: system test built app (#490)\n\ncloses #489",
186+
"parsed": {
187+
"typeKind": "other",
188+
"type": "tests",
189+
"scope": null,
190+
"description": "system test built app (#490)",
191+
"body": "closes #489",
192+
"footers": [],
193+
"breakingChange": null,
194+
"completesInitialDevelopment": false
195+
}
196+
},
197+
"sha": "ea5769aeac22c857352c66264ccf513c8f8cd9ad",
198+
"nonReleaseTags": [],
199+
"releases": {
200+
"stable": null,
201+
"preview": {
202+
"version": "0.11.3-next.1",
203+
"vprefix": false,
204+
"major": 0,
205+
"minor": 11,
206+
"patch": 3,
207+
"preRelease": { "identifier": "next", "buildNum": 1 }
208+
}
209+
}
210+
}
211+
]
212+
}
213+
}

0 commit comments

Comments
 (0)