graphql/express-graphql

How to measure time taken of a GraphQL Query?

jchariot opened this issue · 1 comments

From here:

extensions: An optional function for adding additional metadata to the GraphQL response as a key-value object. The result will be added to the "extensions" field in the resulting JSON. This is often a useful place to add development time metadata such as the runtime of a query or the amount of resources consumed. This may be an async function. The function is given one object as an argument: { document, variables, operationName, result, context }.

So I could already get the GraphQL query and GraphQL JSON result and store them for logging/debugging purpose already:

app.use(myURL, 
  graphqlHTTP({
    schema: mySchema,
    extensions({ result, context }) {
     
	  JSONSResult = JSON.stringify(result,null,4)
	  log(context.body.query)
	  log(JSONSResult)
	  
    },

But I want to know how long a query takes. From the bolded statement above, how could this be accomplish? Which variable must I go for?

@jchariot You can use this code:

app.use(myURL, 
  graphqlHTTP(() => {
    // start timer
    return {
      schema: mySchema,
      extensions({ result, context }) {
  
	  JSONSResult = JSON.stringify(result,null,4)
	  log(context.body.query)
	  log(JSONSResult)
	  
      // calculate time delta
    };
  ),
)