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

gql-profiler

v0.1.2

Published

A standalone performance profiler for GraphQL resolvers

Downloads

793

Readme

npm version

GraphQL Profiler

A standalone performance profiler for GraphQL resolvers

About The Apollo Suite

Before considering to use GraphQL Profiler, you might consider Apollo Engine (or just apollo-tracing-js). These tools are more complete, more powerful and they are maintained by the awesome Apollo team.

But GraphQL Profiler has some advantages compared to the Apollo Suite:

  • It's smaller, simpler and easier to hack
  • It fits even non-standard use cases of GraphQL (like SSR, or server-to-server GraphQL communication)
  • It's only about your resolvers, nothing else
  • It's heavily customisable, you can write your own reporter in 10 lines
  • It allows to plug your own storage, no need to pay Apollo to host your data

Getting Started

Installation

Add the profiler to your project dependencies:

# If you like npm
npm install --save gql-profiler
# or if you like yarn
yarn add gql-profiler

Choose a reporter, wrap your resolvers with the profiler, and use the reporter whenever you want:

import express from 'express';
import { makeExecutableSchema } from 'graphql-tools';
import bodyParser from 'body-parser';
import { graphqlExpress, graphiqlExpress } 'apollo-server-express';
import { profileResolvers, htmlReporter } from 'gql-profiler';
import typeDefs from './typeDefs';
import resolvers from './resolvers';

const reporter = htmlReporter();

const schema = makeExecutableSchema({
  typeDefs,
  // wrap your resolvers with the profiler
  resolvers: profileResolvers(resolvers, { reporter }),
});

const app = express();

// add a route to display profiles
app.get('/profiler', (req, res) => {
    res.send(reporter.getHtml());
});

app.use('/graphql', bodyParser.json(), graphqlExpress({ schema }));

app.listen(3000, () => {
    console.log('Server listening at http://localhost:3000');
});

The profiler report will be available at http://localhost:3000/profiler

Configuration

profileResolvers(resolvers, {
    reporter, // A reporter to gather your data (see the list below) - Mandatory
    enabled: true, // If false, the resolvers aren't profiled
    disableInProduction: true, // Force disable the profiler the env is production
    env: process.env.NODE_ENV,
    getResolverName: () => '', // Function to get the resolver name
});

Reporters

Available reporters:

memoryReporter

import { memoryReporter } from 'gql-reporter';
const reporter = memoryReporter();
const events = reporter.getEvents(); // Retrieve each resolver calls
const hierarchy = reporter.getHierarchy(); // Same, but in a dependency tree
reporter.reset(); // Delete all data in the reporter

htmlReporter

import { htmlReporter } from 'gql-reporter';
const reporter = htmlReporter();
const events = reporter.getEvents(); // Retrieve each resolver calls
const hierarchy = reporter.getHierarchy(); // Same, but in a dependency tree
const html = reporter.getHtml(); // Build a nice HTML page with charts
reporter.reset(); // Delete all data in the reporter

nullReporter

import { nullReporter } from 'gql-reporter';
const reporter = nullReporter();

Does literaly nothing, and doesn't have useful API. Used in tests.

More soon?

  • websocketReporter
  • chromeDevtoolsReporter
  • redisReporter
  • traceEventFormatReporter

Writing your own reporter

Writing a reporter is very simple, here is an example of a consoleReporter:

import uuid from 'uuid';

const consoleReporter = () => ({
    // Instanciate a new event that will be passed through the other functions
    newEvent: (resolver, args, name) => {
        return { id: uuid.v4(), name };
    },
    start: (evt) => {
        console.log(`${evt.name}#${evt.id} started at`, new Date());
    },
    end: (evt, result) => {
        console.log(`${evt.name}#${evt.id} ended at`, new Date());
    },
    error: (evt, error) => {
        console.log(`${evt.name}#${evt.id} encountered an error at`, new Date());
        console.error(error);
    },
});

Only the newEvent function is mandatory. But if you don't provide any of these function, the profiler will fail silently without crashing the resolver.

License

GraphQL Profiler is licensed under the MIT License, courtesy of marmelab and ARTE.