44 ExecutionResult ,
55 DocumentNode ,
66 parse ,
7- print ,
87 validate ,
98 execute ,
109 ExecutionArgs ,
@@ -29,7 +28,7 @@ import {
2928 SyntaxError ,
3029} from './errors' ;
3130
32- import { LogStep , LogAction , LogFunction } from './logging' ;
31+ import { LogFunction , LogFunctionExtension } from './logging' ;
3332
3433export interface GraphQLResponse {
3534 data ?: object ;
@@ -86,20 +85,14 @@ function doRunQuery(options: QueryOptions): Promise<GraphQLResponse> {
8685 throw new Error ( 'Must supply one of queryString and parsedQuery' ) ;
8786 }
8887
89- const logFunction =
90- options . logFunction ||
91- function ( ) {
92- return null ;
93- } ;
9488 const debugDefault =
9589 process . env . NODE_ENV !== 'production' && process . env . NODE_ENV !== 'test' ;
9690 const debug = options . debug !== undefined ? options . debug : debugDefault ;
9791
98- logFunction ( { action : LogAction . request , step : LogStep . start } ) ;
99-
10092 const context = options . context || { } ;
10193
102- // If custom extension factories were provided, create per-request extension objects.
94+ // If custom extension factories were provided, create per-request extension
95+ // objects.
10396 const extensions = options . extensions ? options . extensions . map ( f => f ( ) ) : [ ] ;
10497
10598 // Legacy hard-coded extension factories. The ApolloServer class doesn't use
@@ -112,54 +105,44 @@ function doRunQuery(options: QueryOptions): Promise<GraphQLResponse> {
112105 } else if ( options . cacheControl ) {
113106 extensions . push ( new CacheControlExtension ( options . cacheControl ) ) ;
114107 }
108+ if ( options . logFunction ) {
109+ extensions . push ( new LogFunctionExtension ( options . logFunction ) ) ;
110+ }
115111
116112 const extensionStack = new GraphQLExtensionStack ( extensions ) ;
117113
118- // We unconditionally create an extensionStack (so that we don't have to
119- // litter the rest of this function with `if (extensionStack)`, but we don't
120- // instrument the schema unless there actually are extensions.
114+ // We unconditionally create an extensionStack, even if there are no
115+ // extensions (so that we don't have to litter the rest of this function with
116+ // `if (extensionStack)`, but we don't instrument the schema unless there
117+ // actually are extensions. We do unconditionally put the stack on the
118+ // context, because if some other call had extensions and the schema is
119+ // already instrumented, that's the only way to get a custom fieldResolver to
120+ // work.
121121 if ( extensions . length > 0 ) {
122- context . _extensionStack = extensionStack ;
123122 enableGraphQLExtensions ( options . schema ) ;
124123 }
124+ context . _extensionStack = extensionStack ;
125125
126126 const requestDidEnd = extensionStack . requestDidStart ( {
127127 // Since the Request interfacess are not the same between node-fetch and
128128 // typescript's lib dom, we should limit the fields that need to be passed
129129 // into requestDidStart to only the ones we need, currently just the
130130 // headers, method, and url
131131 request : options . request as any ,
132+ queryString : options . queryString ,
133+ parsedQuery : options . parsedQuery ,
134+ operationName : options . operationName ,
135+ variables : options . variables ,
132136 } ) ;
133137 return Promise . resolve ( )
134138 . then ( ( ) => {
135- const loggedQuery = options . queryString || print ( options . parsedQuery ) ;
136- logFunction ( {
137- action : LogAction . request ,
138- step : LogStep . status ,
139- key : 'query' ,
140- data : loggedQuery ,
141- } ) ;
142- logFunction ( {
143- action : LogAction . request ,
144- step : LogStep . status ,
145- key : 'variables' ,
146- data : options . variables ,
147- } ) ;
148- logFunction ( {
149- action : LogAction . request ,
150- step : LogStep . status ,
151- key : 'operationName' ,
152- data : options . operationName ,
153- } ) ;
154-
155139 // Parse the document.
156140 let documentAST : DocumentNode ;
157141 if ( options . parsedQuery ) {
158142 documentAST = options . parsedQuery ;
159143 } else if ( ! options . queryString ) {
160144 throw new Error ( 'Must supply one of queryString and parsedQuery' ) ;
161145 } else {
162- logFunction ( { action : LogAction . parse , step : LogStep . start } ) ;
163146 const parsingDidEnd = extensionStack . parsingDidStart ( {
164147 queryString : options . queryString ,
165148 } ) ;
@@ -180,7 +163,6 @@ function doRunQuery(options: QueryOptions): Promise<GraphQLResponse> {
180163 ) ;
181164 } finally {
182165 parsingDidEnd ( ...( graphqlParseErrors || [ ] ) ) ;
183- logFunction ( { action : LogAction . parse , step : LogStep . end } ) ;
184166 if ( graphqlParseErrors ) {
185167 return Promise . resolve ( { errors : graphqlParseErrors } ) ;
186168 }
@@ -200,7 +182,6 @@ function doRunQuery(options: QueryOptions): Promise<GraphQLResponse> {
200182 if ( options . validationRules ) {
201183 rules = rules . concat ( options . validationRules ) ;
202184 }
203- logFunction ( { action : LogAction . validation , step : LogStep . start } ) ;
204185 const validationDidEnd = extensionStack . validationDidStart ( ) ;
205186 let validationErrors ;
206187 try {
@@ -217,14 +198,13 @@ function doRunQuery(options: QueryOptions): Promise<GraphQLResponse> {
217198 ) ,
218199 {
219200 formatter : options . formatError ,
220- logFunction,
201+ logFunction : options . logFunction ,
221202 debug,
222203 } ,
223204 ) ;
224205 }
225206 } finally {
226207 validationDidEnd ( ...( validationErrors || [ ] ) ) ;
227- logFunction ( { action : LogAction . validation , step : LogStep . end } ) ;
228208
229209 if ( validationErrors && validationErrors . length ) {
230210 return Promise . resolve ( {
@@ -243,7 +223,6 @@ function doRunQuery(options: QueryOptions): Promise<GraphQLResponse> {
243223 operationName : options . operationName ,
244224 fieldResolver : options . fieldResolver ,
245225 } ;
246- logFunction ( { action : LogAction . execute , step : LogStep . start } ) ;
247226 const executionDidEnd = extensionStack . executionDidStart ( {
248227 executionArgs,
249228 } ) ;
@@ -271,13 +250,12 @@ function doRunQuery(options: QueryOptions): Promise<GraphQLResponse> {
271250 if ( result . errors ) {
272251 response . errors = formatApolloErrors ( [ ...result . errors ] , {
273252 formatter : options . formatError ,
274- logFunction,
253+ logFunction : options . logFunction ,
275254 debug,
276255 } ) ;
277256 }
278257
279258 executionDidEnd ( ...result . errors ) ;
280- logFunction ( { action : LogAction . execute , step : LogStep . end } ) ;
281259
282260 const formattedExtensions = extensionStack . format ( ) ;
283261 if ( Object . keys ( formattedExtensions ) . length > 0 ) {
@@ -296,18 +274,11 @@ function doRunQuery(options: QueryOptions): Promise<GraphQLResponse> {
296274 // we're not returning a GraphQL response so we don't call
297275 // willSendResponse.
298276 requestDidEnd ( err ) ;
299- logFunction ( { action : LogAction . request , step : LogStep . end } ) ;
300277 throw err ;
301278 } )
302279 . then ( graphqlResponse => {
303280 extensionStack . willSendResponse ( { graphqlResponse } ) ;
304281 requestDidEnd ( ) ;
305- logFunction ( {
306- action : LogAction . request ,
307- step : LogStep . end ,
308- key : 'response' ,
309- data : graphqlResponse ,
310- } ) ;
311282 return graphqlResponse ;
312283 } ) ;
313284}
0 commit comments