|
| 1 | +# graphql-resolvable-directive |
| 2 | + |
| 3 | +[](http://badge.fury.io/js/graphql-resolvable-directive) |
| 4 | +[](https://travis-ci.org/prantlf/graphql-resolvable-directive) |
| 5 | +[](https://coveralls.io/github/prantlf/graphql-resolvable-directive?branch=master) |
| 6 | +[](https://david-dm.org/prantlf/graphql-resolvable-directive#info=devDependencies) |
| 7 | +[](https://standardjs.com) |
| 8 | + |
| 9 | +[](https://www.npmjs.com/package/graphql-resolvable-directive) |
| 10 | + |
| 11 | +Supports GraphQL custom directives that hook into the field execution. It can be used for validation or transformation of the resulting field values. |
| 12 | + |
| 13 | +## Synopsis |
| 14 | + |
| 15 | +```js |
| 16 | +const { |
| 17 | + GraphQLResolvableDirective, supportResolvableDirectives |
| 18 | +} = require('graphql-resolvable-directive') |
| 19 | + |
| 20 | +// Performs the logical negation on the field value. |
| 21 | +const notDirective = new GraphQLResolvableDirective({ |
| 22 | + name: 'not', |
| 23 | + description: 'Negates the field execution result.', |
| 24 | + locations: [DirectiveLocation.FIELD], |
| 25 | + async resolve (resolve, source, args, context, info) { |
| 26 | + const result = await resolve() |
| 27 | + return !result |
| 28 | + } |
| 29 | +}) |
| 30 | + |
| 31 | +// Exposes a single field "falsy" returning the `null` value. |
| 32 | +// Recognizes the `not` directive. |
| 33 | +const schema = new GraphQLSchema({ |
| 34 | + directives: [notDirective], |
| 35 | + query: new GraphQLObjectType({ |
| 36 | + name: 'Query', |
| 37 | + fields: () => ({ |
| 38 | + falsy: { type: GraphQLBoolean } |
| 39 | + }) |
| 40 | + }) |
| 41 | +}) |
| 42 | +visitFields(schema, field => supportCustomDirectives(field, schema)) |
| 43 | + |
| 44 | +// Returns `true` instead of the default `null`. |
| 45 | +const { data } = await graphql(schema, '{ falsy @not }') |
| 46 | +assert(data.falsy) |
| 47 | +``` |
| 48 | + |
| 49 | +## Installation |
| 50 | + |
| 51 | +This module can be installed in your project using [NPM] or [Yarn]. Make sure, that you use [Node.js] version 8 or newer. |
| 52 | + |
| 53 | +```sh |
| 54 | +$ npm i graphql-resolvable-directive -S |
| 55 | +``` |
| 56 | + |
| 57 | +```sh |
| 58 | +$ yarn add graphql-resolvable-directive |
| 59 | +``` |
| 60 | + |
| 61 | +## Description |
| 62 | + |
| 63 | +### GraphQLResolvableDirective |
| 64 | + |
| 65 | +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. |
| 66 | + |
| 67 | +```js |
| 68 | +const { GraphQLResolvableDirective } = require('graphql-resolvable-directive') |
| 69 | + |
| 70 | +const isTruthyDirective = new GraphQLResolvableDirective({ |
| 71 | + name: 'isTruthy', |
| 72 | + description: 'Checks if the field execution result is truthy.', |
| 73 | + locations: [DirectiveLocation.FIELD], |
| 74 | + async resolve (resolve, source, args, context, info) { |
| 75 | + const result = await resolve() |
| 76 | + if (!result) { |
| 77 | + throw new Error(`The field "${info.fieldName}" was not truthy.`) |
| 78 | + } |
| 79 | + return result |
| 80 | + } |
| 81 | +}) |
| 82 | + |
| 83 | +const toLowerCaseDirective = new GraphQLResolvableDirective({ |
| 84 | + name: 'toLowerCase', |
| 85 | + description: 'Converts a string value to lower-case.', |
| 86 | + locations: [DirectiveLocation.FIELD], |
| 87 | + async resolve (resolve, source, args, context, info) { |
| 88 | + const result = await resolve() |
| 89 | + return result.toLowerCase() |
| 90 | + } |
| 91 | +}) |
| 92 | +``` |
| 93 | + |
| 94 | +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. |
| 95 | + |
| 96 | +```js |
| 97 | +graphql(schema, '{ name @isTruthy @toLowerCase }') |
| 98 | +``` |
| 99 | + |
| 100 | +### supportCustomDirectives(field, schema) |
| 101 | + |
| 102 | +Enables hooking into the field execution by a custom directive for the specified field. Directives have to be provided in the schema configuration. |
| 103 | + |
| 104 | +* `field` has to be a field configuration object |
| 105 | +* `schema` has to be an object instance of the type `GraphQLSchema` |
| 106 | + |
| 107 | +Field configurations are usually obtained from a schema by a field visitor like [graphql-field-visitor], for example. |
| 108 | + |
| 109 | +```js |
| 110 | +const { supportResolvableDirectives } = require('graphql-resolvable-directive') |
| 111 | +const { visitFields } = require('graphql-field-visitor') |
| 112 | + |
| 113 | +const schema = new GraphQLSchema({ |
| 114 | + directives: [isTruthyDirective, toLowerCaseDirective], |
| 115 | + query: ... |
| 116 | +}) |
| 117 | + |
| 118 | +visitFields(schema, field => supportCustomDirectives(field, schema)) |
| 119 | +``` |
| 120 | + |
| 121 | +## Contributing |
| 122 | + |
| 123 | +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. |
| 124 | + |
| 125 | +## Release History |
| 126 | + |
| 127 | +* 2019-08-18 v0.0.1 Initial release |
| 128 | + |
| 129 | +## License |
| 130 | + |
| 131 | +Copyright (c) 2019 Ferdinand Prantl |
| 132 | + |
| 133 | +Licensed under the MIT license. |
| 134 | + |
| 135 | +[Node.js]: http://nodejs.org/ |
| 136 | +[NPM]: https://www.npmjs.com/ |
| 137 | +[Yarn]: https://yarnpkg.com/ |
| 138 | +[graphql-field-visitor]: https://github.com/prantlf/graphql-field-visitor |
0 commit comments