{"id":22423322,"url":"https://github.com/prantlf/graphql-resolvable-directive","last_synced_at":"2025-08-12T06:33:56.963Z","repository":{"id":57253588,"uuid":"203014576","full_name":"prantlf/graphql-resolvable-directive","owner":"prantlf","description":"Supports GraphQL custom directives that hook into the field execution.","archived":false,"fork":false,"pushed_at":"2019-12-29T20:00:15.000Z","size":132,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-26T00:53:49.598Z","etag":null,"topics":["directive","field","graphql","graphql-directive","resolve","schema"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/prantlf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-18T14:19:54.000Z","updated_at":"2020-12-01T09:03:46.000Z","dependencies_parsed_at":"2022-08-31T22:20:40.093Z","dependency_job_id":null,"html_url":"https://github.com/prantlf/graphql-resolvable-directive","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/prantlf/graphql-resolvable-directive","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prantlf%2Fgraphql-resolvable-directive","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prantlf%2Fgraphql-resolvable-directive/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prantlf%2Fgraphql-resolvable-directive/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prantlf%2Fgraphql-resolvable-directive/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prantlf","download_url":"https://codeload.github.com/prantlf/graphql-resolvable-directive/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prantlf%2Fgraphql-resolvable-directive/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270014338,"owners_count":24512629,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-12T02:00:09.011Z","response_time":80,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["directive","field","graphql","graphql-directive","resolve","schema"],"created_at":"2024-12-05T18:09:59.593Z","updated_at":"2025-08-12T06:33:56.465Z","avatar_url":"https://github.com/prantlf.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# graphql-resolvable-directive\n\n[![NPM version](https://badge.fury.io/js/graphql-resolvable-directive.png)](http://badge.fury.io/js/graphql-resolvable-directive)\n[![Build Status](https://travis-ci.org/prantlf/graphql-resolvable-directive.png)](https://travis-ci.org/prantlf/graphql-resolvable-directive)\n[![Coverage Status](https://coveralls.io/repos/github/prantlf/graphql-resolvable-directive/badge.svg?branch=master)](https://coveralls.io/github/prantlf/graphql-resolvable-directive?branch=master)\n[![Maintainability](https://api.codeclimate.com/v1/badges/c4391fcc43853219c914/maintainability)](https://codeclimate.com/github/prantlf/graphql-resolvable-directive/maintainability)\n[![codebeat badge](https://codebeat.co/badges/9dc77208-2d0e-4b53-a749-c77a6e44b1e0)](https://codebeat.co/projects/github-com-prantlf-graphql-resolvable-directive-master)\n[![devDependency Status](https://david-dm.org/prantlf/graphql-resolvable-directive/dev-status.svg)](https://david-dm.org/prantlf/graphql-resolvable-directive#info=devDependencies)\n[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n\n[![NPM Downloads](https://nodei.co/npm/graphql-resolvable-directive.png?downloads=true\u0026stars=true)](https://www.npmjs.com/package/graphql-resolvable-directive)\n\nSupports GraphQL custom directives that hook into the field execution. It can be used for validation or transformation of the resulting field values.\n\n## Synopsis\n\n```js\nconst {\n  GraphQLResolvableDirective, supportResolvableDirectives\n} = require('graphql-resolvable-directive')\n\n// Performs the logical negation on the field value.\nconst notDirective = new GraphQLResolvableDirective({\n  name: 'not',\n  description: 'Negates the field execution result.',\n  locations: [DirectiveLocation.FIELD],\n  async resolve (resolve, source, args, context, info) {\n    const result = await resolve()\n    return !result\n  }\n})\n\n// Exposes a single field \"falsy\" returning the `null` value.\n// Recognizes the `not` directive.\nconst schema = new GraphQLSchema({\n  directives: [notDirective],\n  query: new GraphQLObjectType({\n    name: 'Query',\n    fields: () =\u003e ({\n      falsy: { type: GraphQLBoolean }\n    })\n  })\n})\nvisitFields(schema, field =\u003e supportCustomDirectives(field, schema))\n\n// Returns `true` instead of the default `null`.\nconst { data } = await graphql(schema, '{ falsy @not }')\nassert(data.falsy)\n```\n\n## Installation\n\nThis module can be installed in your project using [NPM] or [Yarn]. Make sure, that you use [Node.js] version 8 or newer.\n\n```sh\n$ npm i graphql-resolvable-directive -S\n```\n\n```sh\n$ yarn add graphql-resolvable-directive\n```\n\n## Description\n\n### GraphQLResolvableDirective\n\nBase 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.\n\n```js\nconst { GraphQLResolvableDirective } = require('graphql-resolvable-directive')\n\nconst isTruthyDirective = new GraphQLResolvableDirective({\n  name: 'isTruthy',\n  description: 'Checks if the field execution result is truthy.',\n  locations: [DirectiveLocation.FIELD],\n  async resolve (resolve, source, args, context, info) {\n    const result = await resolve()\n    if (!result) {\n      throw new Error(`The field \"${info.fieldName}\" was not truthy.`)\n    }\n    return result\n  }\n})\n\nconst toLowerCaseDirective = new GraphQLResolvableDirective({\n  name: 'toLowerCase',\n  description: 'Converts a string value to lower-case.',\n  locations: [DirectiveLocation.FIELD],\n  async resolve (resolve, source, args, context, info) {\n    const result = await resolve()\n    return result.toLowerCase()\n  }\n})\n```\n\nIf 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.\n\n```js\ngraphql(schema, '{ name @isTruthy @toLowerCase }')\n```\n\n### supportCustomDirectives(field, schema)\n\nEnables hooking into the field execution by a custom directive for the specified field. Directives have to be provided in the schema configuration.\n\n* `field` has to be a field configuration object\n* `schema` has to be an object instance of the type `GraphQLSchema`\n\nField configurations are usually obtained from a schema by a field visitor like [graphql-field-visitor], for example.\n\n```js\nconst { supportResolvableDirectives } = require('graphql-resolvable-directive')\nconst { visitFields } = require('graphql-field-visitor')\n\nconst schema =  new GraphQLSchema({\n  directives: [isTruthyDirective, toLowerCaseDirective],\n  query: ...\n})\n\nvisitFields(schema, field =\u003e supportCustomDirectives(field, schema))\n```\n\n## Contributing\n\nIn 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.\n\n## Release History\n\n* 2019-08-18   v0.0.1   Initial release\n\n## License\n\nCopyright (c) 2019 Ferdinand Prantl\n\nLicensed under the MIT license.\n\n[Node.js]: http://nodejs.org/\n[NPM]: https://www.npmjs.com/\n[Yarn]: https://yarnpkg.com/\n[graphql-field-visitor]: https://github.com/prantlf/graphql-field-visitor\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprantlf%2Fgraphql-resolvable-directive","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprantlf%2Fgraphql-resolvable-directive","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprantlf%2Fgraphql-resolvable-directive/lists"}