import type { ApolloServerPlugin } from 'apollo-server-plugin-base' ; export function customTracingPlugin ( ) { return { requestDidStart : ( { context } ) => { const hrstart = process . hrtime ( ) ; const tracing = { version : 1 , startTime : new Date ( ) . toISOString ( ) , } as any ; return { willSendResponse ( { response } ) { response . extensions = response . extensions || { } ; tracing . endTime = new Date ( ) . toISOString ( ) ; tracing . duration = durationHrTimeToNanos ( process . hrtime ( hrstart ) ) ; const resolvers = context ?. vendorRequests ?. map ( ( d ) => ( { path : [ d . url ] , parentType : 'Query' , fieldName : d . url , returnType : 'String' , startOffset : durationDiff ( d . hrstart , hrstart ) , duration : durationHrTimeToNanos ( d . hrduration ) , } ) ) ; tracing . execution = { resolvers } ; response . extensions . tracing = tracing ; } , } ; } , } as ApolloServerPlugin ; } type HighResolutionTime = [ number , number ] ; function durationDiff ( hrend , hrstart ) { const hrtime = [ hrend [ 0 ] - hrstart [ 0 ] , hrend [ 1 ] - hrstart [ 1 ] ] as HighResolutionTime ; return durationHrTimeToNanos ( hrtime ) ; } function durationHrTimeToNanos ( hrtime ) { return hrtime [ 0 ] * 1e9 + hrtime [ 1 ] ; }