npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mercurius-explain-graphiql-plugin

v2.2.4

Published

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

Downloads

34

Readme

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])