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

graphql-resolvable-directive

v0.0.2

Published

Supports GraphQL custom directives that hook into the field execution.

Downloads

9

Readme

graphql-resolvable-directive

NPM version Build Status Coverage Status Maintainability codebeat badge devDependency Status JavaScript Style Guide

NPM Downloads

Supports GraphQL custom directives that hook into the field execution. It can be used for validation or transformation of the resulting field values.

Synopsis

const {
  GraphQLResolvableDirective, supportResolvableDirectives
} = require('graphql-resolvable-directive')

// Performs the logical negation on the field value.
const notDirective = new GraphQLResolvableDirective({
  name: 'not',
  description: 'Negates the field execution result.',
  locations: [DirectiveLocation.FIELD],
  async resolve (resolve, source, args, context, info) {
    const result = await resolve()
    return !result
  }
})

// Exposes a single field "falsy" returning the `null` value.
// Recognizes the `not` directive.
const schema = new GraphQLSchema({
  directives: [notDirective],
  query: new GraphQLObjectType({
    name: 'Query',
    fields: () => ({
      falsy: { type: GraphQLBoolean }
    })
  })
})
visitFields(schema, field => supportCustomDirectives(field, schema))

// Returns `true` instead of the default `null`.
const { data } = await graphql(schema, '{ falsy @not }')
assert(data.falsy)

Installation

This module can be installed in your project using NPM or Yarn. Make sure, that you use Node.js version 8 or newer.

$ npm i graphql-resolvable-directive -S
$ yarn add graphql-resolvable-directive

Description

GraphQLResolvableDirective

Base class for custom directives with field execution hooks. If they include the resolve method, it will be called instead of the original field resolver. It would usually call the original resolver to inspect or modify its result.

const { GraphQLResolvableDirective } = require('graphql-resolvable-directive')

const isTruthyDirective = new GraphQLResolvableDirective({
  name: 'isTruthy',
  description: 'Checks if the field execution result is truthy.',
  locations: [DirectiveLocation.FIELD],
  async resolve (resolve, source, args, context, info) {
    const result = await resolve()
    if (!result) {
      throw new Error(`The field "${info.fieldName}" was not truthy.`)
    }
    return result
  }
})

const toLowerCaseDirective = new GraphQLResolvableDirective({
  name: 'toLowerCase',
  description: 'Converts a string value to lower-case.',
  locations: [DirectiveLocation.FIELD],
  async resolve (resolve, source, args, context, info) {
    const result = await resolve()
    return result.toLowerCase()
  }
})

If the directives are chained, the results are passed from the first directive to the next one and so on. The last returned value is assigned to the field.

graphql(schema, '{ name @isTruthy @toLowerCase }')

supportCustomDirectives(field, schema)

Enables hooking into the field execution by a custom directive for the specified field. Directives have to be provided in the schema configuration.

  • field has to be a field configuration object
  • schema has to be an object instance of the type GraphQLSchema

Field configurations are usually obtained from a schema by a field visitor like graphql-field-visitor, for example.

const { supportResolvableDirectives } = require('graphql-resolvable-directive')
const { visitFields } = require('graphql-field-visitor')

const schema =  new GraphQLSchema({
  directives: [isTruthyDirective, toLowerCaseDirective],
  query: ...
})

visitFields(schema, field => supportCustomDirectives(field, schema))

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

  • 2019-08-18 v0.0.1 Initial release

License

Copyright (c) 2019 Ferdinand Prantl

Licensed under the MIT license.