Skip to content

Supports GraphQL custom directives that hook into the field execution.

License

Notifications You must be signed in to change notification settings

prantlf/graphql-resolvable-directive

Repository files navigation

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.