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

@pothos/plugin-directives

v4.2.0

Published

Directive plugin for Pothos, enables using graphql-tools based directives with Pothos

Downloads

37,978

Readme

Directives Plugin

A plugin for using schema directives with schemas generated by Pothos.

Schema Directives are not intended to be used with code first schemas, but there is a large existing community with several very useful directives based

Usage

Install

yarn add @pothos/plugin-directives

Setup

import DirectivePlugin from '@pothos/plugin-directives';
import { rateLimitDirective } from 'graphql-rate-limit-directive';

const builder = new SchemaBuilder<{
  Directives: {
    rateLimit: {
      locations: 'OBJECT' | 'FIELD_DEFINITION';
      args: { limit: number, duration: number };
    };
  };
}>({
  plugins: [DirectivePlugin],
  directives: {
    useGraphQLToolsUnorderedDirectives: true,
  }
});

builder.queryType({
  directives: {
    rateLimit: { limit: 5, duration: 60 },
  },
  fields: (t) => ({
    hello: t.string({ resolve: () => 'world' });
  });
});

const { rateLimitDirectiveTransformer } = rateLimitDirective();
const schema = rateLimitDirectiveTransformer(builder.toSchema());

The directives plugin allows you to define types for the directives your schema will use the SchemaTypes parameter. Each directive can define a set of locations the directive can appear, and an object type representing the arguments the directive accepts.

The valid locations for directives are:

  • ARGUMENT_DEFINITION
  • ENUM_VALUE
  • ENUM
  • FIELD_DEFINITION
  • INPUT_FIELD_DEFINITION
  • INPUT_OBJECT
  • INTERFACE
  • OBJECT
  • SCALAR
  • SCHEMA
  • UNION

Pothos does not apply the directives itself, this plugin simply adds directive information to the extensions property of the underlying GraphQL type so that it can be consumed by other tools like graphql-tools.

By default this plugin uses the format that Gatsby uses (described here). This format was not supported by older versions of graphql-tools. To support older versions of graphql-tools or directives that provide a schema visitor based on an older graphql-tools version like the rate-limit directive from the example above you can set the useGraphQLToolsUnorderedDirectives option. This option does not preserve the order that directives are defined in. This will be okay for most cases, but may cause issues if your directives need to be applied in a specific order.

To define directives on your fields or types, you can add a directives property in any of the supported locations using one of the following 2 formats:

{
  directives: [
    {
      name: "validation",
      args: {
        regex: "/abc+/"
      }
    },
    {
      name: "required",
      args: {},
    }
  ],
  // or
  directives: {
    validation: {
      regex: "/abc+/"
    },
    required: {}
  }
}

Each of these applies the same 2 directives. The first format is preferred, especially when using directives that are sensitive to ordering, or can be repeated multiple times for the same location.

Applying directives

For most locations (On fields and types) the options object for the field or type will have a directives option which can be used to define directives.

To apply SCHEMA directives, you can use the schemaDirectives option on the toSchema method. directives on toSchema is reserved for the Directive implementations.