@@ -12,7 +12,7 @@ import {
12
12
} from 'graphql' ;
13
13
import { print } from 'graphql/language/printer' ;
14
14
15
- const defaultOptions : ConsoleLoggerLinkOptions = {
15
+ const defaultOptions : Required < ConsoleLoggerLinkOptions > = {
16
16
colors : {
17
17
[ OperationTypeNode . QUERY ] : {
18
18
request : '#E17E00' ,
@@ -27,7 +27,9 @@ const defaultOptions: ConsoleLoggerLinkOptions = {
27
27
response: '#A68600',
28
28
},*/
29
29
} ,
30
- timings : true ,
30
+ multiline : false ,
31
+ responseSize : true ,
32
+ responseTime : true ,
31
33
} ;
32
34
33
35
interface ConsoleLoggerLinkOptions {
@@ -41,12 +43,14 @@ interface ConsoleLoggerLinkOptions {
41
43
response : string ;
42
44
} ;
43
45
} ;
44
- timings ?: boolean ;
46
+ multiline ?: boolean ;
47
+ responseSize ?: boolean ;
48
+ responseTime ?: boolean ;
45
49
}
46
50
47
51
export class ConsoleLoggerLink extends ApolloLink {
48
52
private logId = 0 ;
49
- private options : ConsoleLoggerLinkOptions ;
53
+ private options : Required < ConsoleLoggerLinkOptions > ;
50
54
51
55
constructor ( options ?: ConsoleLoggerLinkOptions ) {
52
56
super ( ) ;
@@ -60,7 +64,9 @@ export class ConsoleLoggerLink extends ApolloLink {
60
64
options ?. colors ?. [ OperationTypeNode . MUTATION ] ??
61
65
defaultOptions ?. colors ?. [ OperationTypeNode . MUTATION ] ,
62
66
} ,
63
- timings : options ?. timings ?? true ,
67
+ multiline : options ?. multiline ?? defaultOptions . multiline ,
68
+ responseSize : options ?. responseSize ?? defaultOptions . responseSize ,
69
+ responseTime : options ?. responseTime ?? defaultOptions . responseTime ,
64
70
} ;
65
71
}
66
72
@@ -84,7 +90,7 @@ export class ConsoleLoggerLink extends ApolloLink {
84
90
this . _logRequest ( operation , operationAst , operationId ) ;
85
91
}
86
92
87
- const startTime = this . options . timings ? Date . now ( ) : 0 ;
93
+ const startTime = this . options . responseTime ? Date . now ( ) : 0 ;
88
94
89
95
return forward ( operation ) . map ( ( result ) => {
90
96
if (
@@ -115,7 +121,8 @@ export class ConsoleLoggerLink extends ApolloLink {
115
121
operationId +
116
122
' %c %c' +
117
123
operation . operationName +
118
- '%c' ,
124
+ '%c' +
125
+ ( this . options . multiline ? '\n' : '' ) ,
119
126
'background: ' +
120
127
this . options . colors ?. [
121
128
operationAst . operation as
@@ -155,7 +162,15 @@ export class ConsoleLoggerLink extends ApolloLink {
155
162
' %c ' +
156
163
( ! success ? '⚠️ ' : '' ) +
157
164
`%c${ operation . operationName } %c` +
158
- ( this . options . timings ? ` %c${ Date . now ( ) - startTime } ms` : '%c' ) ,
165
+ ( this . options . responseSize
166
+ ? ` %c${
167
+ Math . round ( ( JSON . stringify ( results ) ?. length / 1024 ) * 10 ) / 10
168
+ } kB`
169
+ : '%c' ) +
170
+ '%c' +
171
+ ( this . options . responseTime ? ` %c${ Date . now ( ) - startTime } ms` : '%c' ) +
172
+ '%c' +
173
+ ( this . options . multiline ? '\n' : '' ) ,
159
174
'background: ' +
160
175
this . options . colors ?. [
161
176
operationAst . operation as
@@ -169,9 +184,14 @@ export class ConsoleLoggerLink extends ApolloLink {
169
184
? 'color: #008000;'
170
185
: 'color: #CC0000; text-decoration: underline; text-decoration-style: dotted' ) ,
171
186
undefined ,
172
- this . options . timings
187
+ this . options . responseSize
173
188
? 'background: #e4e4e4; padding: 2px 4px; border-radius: 3px; font-size: 11px;'
174
189
: undefined ,
190
+ undefined ,
191
+ this . options . responseTime
192
+ ? 'background: #e4e4e4; padding: 2px 4px; border-radius: 3px; font-size: 11px;'
193
+ : undefined ,
194
+ undefined ,
175
195
results ,
176
196
) ;
177
197
}
@@ -186,5 +206,5 @@ function isEmpty(value: unknown) {
186
206
return ! Object . keys ( value ) . length ;
187
207
}
188
208
189
- return true ;
209
+ return false ;
190
210
}
0 commit comments