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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@graphjson/plugins

v0.0.1

Published

> Plugin system type definitions for GraphJSON

Downloads

123

Readme

@graphjson/plugins

Plugin system type definitions for GraphJSON

npm version License: MIT TypeScript

Type definitions and interfaces for creating GraphJSON plugins that can transform GraphQL documents.

Why Use This?

  • 🔌 Plugin Interface - Standard interface for creating plugins
  • 🎯 Type-Safe - Full TypeScript support
  • 🏗️ Extensible - Build custom transformations
  • 📚 Well-Defined - Clear plugin lifecycle
  • 🔧 Flexible - Transform documents or fields

Installation

npm install @graphjson/plugins

Quick Start

import type { GraphJsonPlugin } from '@graphjson/plugins';
import { Kind } from 'graphql';

export function myPlugin(): GraphJsonPlugin {
  return {
    onDocument(document) {
      // Transform entire document
      return document;
    },
    onField(field, context) {
      // Transform individual fields
      return field;
    }
  };
}

Plugin Interface

interface GraphJsonPlugin {
  onDocument?: (document: DocumentNode) => DocumentNode | void;
  onField?: (field: FieldNode, context: PluginContext) => FieldNode | void;
}

interface PluginContext {
  path: string[];
}

Creating Plugins

Document-Level Plugin

Transforms the entire GraphQL document:

export function addOperationName(name: string): GraphJsonPlugin {
  return {
    onDocument(document) {
      return {
        ...document,
        definitions: document.definitions.map(def => {
          if (def.kind === Kind.OPERATION_DEFINITION) {
            return {
              ...def,
              name: { kind: Kind.NAME, value: name }
            };
          }
          return def;
        })
      };
    }
  };
}

Field-Level Plugin

Transforms individual fields:

export function addTimestampField(): GraphJsonPlugin {
  return {
    onField(field) {
      if (!field.selectionSet) return field;
      
      return {
        ...field,
        selectionSet: {
          ...field.selectionSet,
          selections: [
            ...field.selectionSet.selections,
            {
              kind: Kind.FIELD,
              name: { kind: Kind.NAME, value: 'timestamp' }
            }
          ]
        }
      };
    }
  };
}

Context-Aware Plugin

Use context to make decisions:

export function depthLimiter(maxDepth: number): GraphJsonPlugin {
  return {
    onField(field, context) {
      if (context.path.length > maxDepth) {
        // Remove selections if too deep
        return {
          ...field,
          selectionSet: undefined
        };
      }
      return field;
    }
  };
}

Plugin Examples

Relay Pagination Plugin

export function relayPagination(): GraphJsonPlugin {
  return {
    onField(field) {
      if (!field.selectionSet) return;

      const hasEdges = field.selectionSet.selections.some(
        s => s.kind === Kind.FIELD && s.name.value === 'edges'
      );

      if (hasEdges) return;

      return {
        ...field,
        selectionSet: {
          kind: Kind.SELECTION_SET,
          selections: [
            {
              kind: Kind.FIELD,
              name: { kind: Kind.NAME, value: 'edges' },
              selectionSet: {
                kind: Kind.SELECTION_SET,
                selections: [
                  {
                    kind: Kind.FIELD,
                    name: { kind: Kind.NAME, value: 'node' },
                    selectionSet: field.selectionSet
                  }
                ]
              }
            },
            {
              kind: Kind.FIELD,
              name: { kind: Kind.NAME, value: 'pageInfo' },
              selectionSet: {
                kind: Kind.SELECTION_SET,
                selections: [
                  { kind: Kind.FIELD, name: { kind: Kind.NAME, value: 'hasNextPage' } },
                  { kind: Kind.FIELD, name: { kind: Kind.NAME, value: 'endCursor' } }
                ]
              }
            }
          ]
        }
      };
    }
  };
}

Auto-Include ID Plugin

export function autoIncludeId(): GraphJsonPlugin {
  return {
    onField(field) {
      if (!field.selectionSet) return field;
      
      const hasId = field.selectionSet.selections.some(
        s => s.kind === Kind.FIELD && s.name.value === 'id'
      );
      
      if (hasId) return field;
      
      return {
        ...field,
        selectionSet: {
          ...field.selectionSet,
          selections: [
            {
              kind: Kind.FIELD,
              name: { kind: Kind.NAME, value: 'id' }
            },
            ...field.selectionSet.selections
          ]
        }
      };
    }
  };
}

Using Plugins

Single Plugin

import { applyPlugins } from '@graphjson/core';
import { relayPagination } from '@graphjson/presets';

const transformed = applyPlugins(document, [relayPagination()]);

Multiple Plugins

import { applyPlugins } from '@graphjson/core';

const transformed = applyPlugins(document, [
  plugin1(),
  plugin2(),
  plugin3()
]);

Plugins are applied in order.

TypeScript Support

import type { GraphJsonPlugin, PluginContext } from '@graphjson/plugins';
import type { DocumentNode, FieldNode } from 'graphql';

export function typedPlugin(): GraphJsonPlugin {
  return {
    onDocument(document: DocumentNode): DocumentNode {
      return document;
    },
    onField(field: FieldNode, context: PluginContext): FieldNode {
      return field;
    }
  };
}

GraphJSON Ecosystem

| Package | Description | NPM | |---------|-------------|-----| | @graphjson/core | Core library (uses plugins) | npm | | @graphjson/presets | Pre-built plugins | npm |

Contributing

Contributions are welcome! Please see CONTRIBUTING.md.

License

MIT © NexaLeaf