Skip to content

Commit d7882fb

Browse files
authored
Update logging example to show additional Apollo and HTTP request information (#9649)
Co-authored-by: Daniel Cousens <[email protected]>
1 parent 8e9ed7a commit d7882fb

File tree

3 files changed

+149
-10
lines changed

3 files changed

+149
-10
lines changed

examples/logging/keystone.ts

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
import { config } from '@keystone-6/core'
2-
import { lists } from './schema'
2+
import { createHash } from 'node:crypto'
3+
import pino_ from 'pino-http'
4+
35
import type { TypeInfo } from '.keystone/types'
6+
import { lists } from './schema'
7+
8+
function sha256(q: string) {
9+
return createHash('sha256').update(q).digest('hex')
10+
}
11+
12+
const pino = pino_({
13+
// genReqId: (req, res) => req.headers["cf-ray"] ?? '',
14+
// genReqId: (req, res) => req.headers["x-amzn-trace-id"] ?? '',
15+
// genReqId: (req, res) => req.headers["x-request-id"] ?? '',
16+
})
417

518
export default config<TypeInfo>({
619
db: {
@@ -10,22 +23,44 @@ export default config<TypeInfo>({
1023
// WARNING: this is only needed for our monorepo examples, dont do this
1124
prismaClientPath: 'node_modules/myprisma',
1225
},
26+
server: {
27+
extendExpressApp(app) {
28+
app.use(pino)
29+
},
30+
},
1331
graphql: {
1432
apolloConfig: {
15-
formatError(formattedError, error) {
33+
formatError(formattedError, __error) {
1634
// you can customise the error returned to the client
1735
return formattedError
1836
},
1937
plugins: [
2038
{
21-
async requestDidStart(requestContext) {
22-
console.log(
23-
'graphql operation',
24-
requestContext.request.operationName ?? '(unnamed operation)'
25-
)
39+
async requestDidStart() {
40+
const start = Date.now()
41+
2642
return {
27-
async didEncounterErrors(requestContext) {
28-
console.error(...requestContext.errors)
43+
async willSendResponse(requestContext) {
44+
pino.logger.info(
45+
{
46+
req: requestContext.contextValue.req
47+
? { id: requestContext.contextValue.req?.id }
48+
: undefined,
49+
responseTime: Date.now() - start,
50+
graphql: {
51+
type: requestContext.operation?.operation,
52+
name: requestContext.request.operationName,
53+
hash: sha256(requestContext.request.query ?? ''),
54+
// query: requestContext.request.query, // WARNING: may be verbose
55+
errors:
56+
requestContext.errors?.map(e => ({
57+
path: e.path,
58+
message: e.message,
59+
})) || undefined,
60+
},
61+
},
62+
'graphql query completed'
63+
)
2964
},
3065
}
3166
},

examples/logging/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
},
1313
"dependencies": {
1414
"@keystone-6/core": "^6.5.1",
15-
"@prisma/client": "6.5.0"
15+
"@prisma/client": "6.5.0",
16+
"pino-http": "^10.5.0"
1617
},
1718
"devDependencies": {
1819
"prisma": "6.5.0",

pnpm-lock.yaml

Lines changed: 103 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)