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

@treorisoft/graphql-validator-directive

v1.0.3

Published

Allows creating custom constraint directives to validate queries and inputs. Inspired by [graphql-constraint-directive](https://github.com/confuser/graphql-constraint-directive).

Downloads

356

Readme

@treorisoft/graphql-validator-directive

Allows creating custom constraint directives to validate queries and inputs. Inspired by graphql-constraint-directive.

This is useful for output types as well - since this consolidates directives rather than relying on individual field resolvers which might have the directive run the same validation check multiple times.

Setup

There are multiple parts to setup and use this.

Create Directives

First create the custom directives and ensure they are part of your schema.

Currently the only supported directive locations: FIELD_DEFINITION | INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION

directive @isAuthenticated(message: String) on FIELD | FIELD_DEFINITION | INPUT_FIELD_DEFINITION

Create Rule

Next create the rule that will handle the directive. See the Usage section for further details.

import { ValidationDirectiveRule } from "@treorisoft/graphql-validator-directive";

export default class IsAuthenticatedDirectiveRule extends ValidationDirectiveRule<Record<string, any>, any, unknown> {
  async execute(context: unknown): Promise<void> {
    if (!context.auth) {
      throw new Error('Not authorized: ' + this.args.message);
    }
  }  
}

Register Rule/Transform Schema

Now we need to register the rule with the directive and transform the schema to initialize them.

import { schemaTransformer, registerRules } from '@treorisoft/graphql-validator-directive';
import IsAuthenticatedDirectiveRule from './directives/isAuthenticatedRule';

registerRules({
  isAuthenticated: IsAuthenticatedDirectiveRule,
});

export function mergeDirectives(schema: GraphQLSchema): GraphQLSchema {
  return schemaTransformer(schema);
}

Setup the plugin

import { createApolloValidationPlugin } from '@treorisoft/graphql-validator-directive';

const server = new ApolloServer<ResolverContext>({
  schema,
  plugins: [
    createApolloValidationPlugin(),
  ]
});

Usage

execute is a required abstract method that performs the validation for the given field and receives the same context that would be passed to any field resolver.

There are 3 ways to signal an failure.

Return a boolean false, return a string with the error message or simply throw an error.

Depending on just what kind of validation is taking place the effect of the failure will be different.

  • Input validation, or top-level query/mutation fields will cause an error to be thrown.
  • Type field validation:
    • If your rule throws, it will be thrown before anything is run
    • If you return a failure (false or string), your field will be removed from the selection set and pretend that it wasn't even requested.

Development

Development of this package is done via a larger monorepo where this is a git submodule.