mercurius-explain-graphiql-plugin

This is the graphiql plugin for mercurius-explain plugin for mercurius.


Keywords
mercurius, hacktoberfest
License
ISC
Install
npm install mercurius-explain-graphiql-plugin@1.3.5

Documentation

Mercurius Explain Graphiql Plugin

A GraphiQL extension to show the output generated by the mercurius-explain plugin, a plugin that exports execution info related to a graphql query.

Profiler

Expose the execution time of the resolvers.

alt text

Implements a waterfall chart to have a clear view of the resolvers.

alt text

Profiler Details

Calculations

Data from the Mercurius Explain goes through some calculations to display accurately the behaviour of a query or mutation. Time, as an exemple, is defined by the temporal cost of the resolver operation of a specific type or property, from start to end. This can be misleading as one property may have to call other resolvers to retrieve the needed data. Therefore, we append totals that take in consideration the end of the furthest child node that composes its resolution. This is visible in the first image of this README with the root path, Users. It takes a minimum amount of time to resolve Users but the total time is always going to be the highest, because all the other types and properties should be resolved for us to consider it complete.

Thresholds

The results of the profiler are coloured to demonstrate the relative duration of each property of a path in relation to the maximum value of the corresponding property, i.e. Time, Total Time. We are going to support custom intervals settings, however, at the moment we are displaying the data according to the percentage groups:

  • 0% to 49% - White (default)
  • 50% to 69% - Light Yellow
  • 70% to 89% - Yellow
  • 90% - 98% - Orange
  • +99% - Red

Resolvers calls counter

Expose the number of calls for each resolver in the specified query.

alt text

Check the mercurius-explain Github Repo for detailed information.

Quick start

This plugin is deployed as UMD to unpkg.com and is available without a direct install in the GraphiQL mercurius integration.

import Fastify from 'fastify'
import mercurius from 'mercurius'
import explain, { explainGraphiQLPlugin } from 'mercurius-explain'

const app = Fastify({ logger: true })

const schema = `type Query { add(x: Int, y: Int): Int }`

const resolvers = {
  Query: {
    async add(_, { x, y }) { return x + y }
  }
}

app.register(mercurius, {
  schema,
  resolvers,
  graphiql: {
    enabled: true,                      // Enable GraphiQL
    plugins: [explainGraphiQLPlugin()]  // Add Mercurius Explain Graphiql Plugin
  }
})

app.register(explain, {})
app.listen({ port: 3000 })

Install in a custom GraphiQL App

The plugin can be installed also in a custom GraphiQL app.

CLone and run a sample Graphql server

git clone https://github.com/nearform/mercurius-explain.git
cd mercurius-explain
npm install
npm run example

A sample server runs on http://localhost:3001.

Test it with:

curl http://localhost:3001

> {"status":"OK"}

Create the basic GraphiQL app

Create the app using Create React App and install the required modules.

npx create-react-app custom-graphiql
cd custom-graphiql
npm i graphql graphql-ws
npm i graphiql @graphiql/toolkit @graphiql/react

Replace the App.jsx with the following content:

import React from 'react'
import { GraphiQL } from 'graphiql'
import { createGraphiQLFetcher } from '@graphiql/toolkit'

import 'graphiql/graphiql.css'
import '@graphiql/react/dist/style.css'

function App() {
  const fetcher = createGraphiQLFetcher({
    url: 'http://localhost:3001/graphql'  
  })

  return (
    <div
      style={{
        height: '100vh',
        minWidth: '1080px',
        width: '100vw',
        overflow: 'scroll'
      }}
    >
      <GraphiQL fetcher={fetcher} />
    </div>
  )
}

export default App

Run the app

npm start

and test it with the query:

{
  users {
    name
    status { enabled }
    addresses { zip }
  }
}

Add the plugin

npm i mercurius-explain-graphiql-plugin

add the plugin to the code:

Import the plugin

...
import { fetcherReturnToPromise } from '@graphiql/toolkit'
import { graphiqlExplainPlugin, parseFetchResponse } from 'mercurius-explain-graphiql-plugin'
...

Add a fetchWrapper function

const fetcherWrapper = (fetcher, cbs = []) => {
  return async (gqlp, fetchOpt) => {
    const fetchResponse = await fetcher(gqlp, fetchOpt)
    const result = await fetcherReturnToPromise(fetchResponse)
    let cbsResult = { ...result }
    for (const cb of cbs) cbsResult = cb(cbsResult)
    return cbsResult
  }
}

and wrap the fetcher before add it to the GraphiQL component

const fetcher = fetcherWrapper(createGraphiQLFetcher({
  url: 'http://localhost:3001/graphql'
}), [parseFetchResponse])

NOTE: This operation is required because GraphiQL does not provide an easy access to the result of the query, then the result is got directly from the fetch action.

add the plugin to the GraphiQL component

...
  <GraphiQL fetcher={fetch} plugins={[graphiqlExplainPlugin()]} />
...

The final version of App.jsx

import React from 'react'
import { GraphiQL } from 'graphiql'
import { createGraphiQLFetcher, fetcherReturnToPromise } from '@graphiql/toolkit'

import 'graphiql/graphiql.css'
import '@graphiql/react/dist/style.css'

import { graphiqlExplainPlugin, parseFetchResponse } from 'mercurius-explain-graphiql-plugin'

export const fetcherWrapper = (fetcher, cbs = []) => {
  return async (gqlp, fetchOpt) => {
    const fetchResponse = await fetcher(gqlp, fetchOpt)
    const result = await fetcherReturnToPromise(fetchResponse)
    let cbsResult = { ...result }
    for (const cb of cbs) cbsResult = cb(cbsResult)
    return cbsResult
  }
}

function App() {
  const fetcher = fetcherWrapper(createGraphiQLFetcher({
    url: 'http://localhost:3001/graphql'
  }), [parseFetchResponse])

  return (
    <div
      style={{
        height: '100vh',
        minWidth: '1080px',
        width: '100vw',
        overflow: 'scroll'
      }}
    >
      <GraphiQL fetcher={fetcher} plugins={[graphiqlExplainPlugin()]}/>
    </div>
  )
}

export default App

API

graphiqlExplainPlugin

The plugin component should be added to the GraphiQL component in the plugins list

<GraphiQL fetcher={fetcher} plugins={[graphiqlExplainPlugin()]}/>

parseFetchResponse

A function that extract the explain data from the query response. It should be passed to the fetcherWrapper.

export const fetcherWrapper = (fetcher, cbs = []) => {
  return async (gqlp, fetchOpt) => {
    const fetchResponse = await fetcher(gqlp, fetchOpt)
    const result = await fetcherReturnToPromise(fetchResponse)
    let cbsResult = { ...result }
    for (const cb of cbs) cbsResult = cb(cbsResult)
    return cbsResult
  }
}

const fetcher = fetcherWrapper(createGraphiQLFetcher({
  url: 'http://localhost:3001/graphql'
}), [parseFetchResponse])