1
1
import { config } from '@keystone-6/core'
2
- import { lists } from './schema'
2
+ import { createHash } from 'node:crypto'
3
+ import pino_ from 'pino-http'
4
+
3
5
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
+ } )
4
17
5
18
export default config < TypeInfo > ( {
6
19
db : {
@@ -10,22 +23,44 @@ export default config<TypeInfo>({
10
23
// WARNING: this is only needed for our monorepo examples, dont do this
11
24
prismaClientPath : 'node_modules/myprisma' ,
12
25
} ,
26
+ server : {
27
+ extendExpressApp ( app ) {
28
+ app . use ( pino )
29
+ } ,
30
+ } ,
13
31
graphql : {
14
32
apolloConfig : {
15
- formatError ( formattedError , error ) {
33
+ formatError ( formattedError , __error ) {
16
34
// you can customise the error returned to the client
17
35
return formattedError
18
36
} ,
19
37
plugins : [
20
38
{
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
+
26
42
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
+ )
29
64
} ,
30
65
}
31
66
} ,
0 commit comments